Discuss the following with respect to JDBCi) Metadata ii) ResultSet Metadata iii) Data Types iv) Exceptions

Discuss the following with respect to JDBC
i) Metadata ii) ResultSet Metadata iii) Data Types iv) Exceptions

Answer:-

JDBC Concepts Overview

Java Database Connectivity (JDBC) is a Java API that enables applications to interact with relational databases. It provides methods to connect, execute queries, and retrieve results from databases.

Below is an explanation of the key topics:


i) Metadata

Metadata provides information about the database and its structure, such as tables, columns, and their types.

Types of Metadata in JDBC:

  1. DatabaseMetaData
    • Describes the database as a whole.
    • Obtained via: DatabaseMetaData dbmd = connection.getMetaData();
    • Example: System.out.println("DB Product: " + dbmd.getDatabaseProductName()); System.out.println("DB Version: " + dbmd.getDatabaseProductVersion());
  2. ResultSetMetaData
    • Provides information about the columns in a ResultSet.
    • Obtained via: ResultSetMetaData rsmd = resultSet.getMetaData();
    • Example: int columnCount = rsmd.getColumnCount(); for (int i = 1; i <= columnCount; i++) { System.out.println("Column " + i + ": " + rsmd.getColumnName(i)); }

ii) ResultSet Metadata

  • A ResultSet is a table of data returned by a SQL query.
  • ResultSetMetaData gives structural info about that table (like number of columns, data types, etc.).

Common Methods of ResultSetMetaData:

MethodDescription
getColumnCount()Returns number of columns
getColumnName(int column)Returns column name
getColumnType(int column)Returns SQL type of column
getColumnTypeName(int column)Returns database-specific type name
isNullable(int column)Checks if the column allows nulls

iii) Data Types in JDBC

JDBC maps SQL data types to Java data types. This is defined in java.sql.Types.

Common SQL to Java Data Type Mappings:

SQL TypeJava TypeJDBC Constant
VARCHARStringTypes.VARCHAR
INTintTypes.INTEGER
DOUBLEdoubleTypes.DOUBLE
DATEjava.sql.DateTypes.DATE
BOOLEANbooleanTypes.BOOLEAN
DECIMALBigDecimalTypes.DECIMAL

Example: Using PreparedStatement with types

PreparedStatement ps = con.prepareStatement("INSERT INTO students VALUES (?, ?, ?)");
ps.setInt(1, 101);
ps.setString(2, "Kushal");
ps.setDouble(3, 87.5);
ps.executeUpdate();

iv) Exceptions in JDBC

All JDBC operations may throw checked exceptions that must be handled.

Common Exceptions:

ExceptionDescription
SQLExceptionGeneral JDBC exception for DB access errors
SQLSyntaxErrorExceptionFor SQL syntax errors
SQLTimeoutExceptionQuery timeout
BatchUpdateExceptionError during batch operations
SQLIntegrityConstraintViolationExceptionFor constraint violations like primary key conflicts

Basic Exception Handling:

try {
    Connection con = DriverManager.getConnection(...);
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * FROM users");
} catch (SQLException e) {
    System.out.println("Error: " + e.getMessage());
    e.printStackTrace();
}

Summary Table

ConceptPurpose
MetadataInfo about database and structure (DatabaseMetaData, ResultSetMetaData)
ResultSet MetadataColumn-specific info from a result set
Data TypesMappings between SQL and Java types
ExceptionsHandles errors during JDBC operations using SQLException and subtypes

Leave a Reply

Your email address will not be published. Required fields are marked *