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:
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());
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)); }
- Provides information about the columns in a
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
:
Method | Description |
---|---|
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 Type | Java Type | JDBC Constant |
---|---|---|
VARCHAR | String | Types.VARCHAR |
INT | int | Types.INTEGER |
DOUBLE | double | Types.DOUBLE |
DATE | java.sql.Date | Types.DATE |
BOOLEAN | boolean | Types.BOOLEAN |
DECIMAL | BigDecimal | Types.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:
Exception | Description |
---|---|
SQLException | General JDBC exception for DB access errors |
SQLSyntaxErrorException | For SQL syntax errors |
SQLTimeoutException | Query timeout |
BatchUpdateException | Error during batch operations |
SQLIntegrityConstraintViolationException | For 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
Concept | Purpose |
---|---|
Metadata | Info about database and structure (DatabaseMetaData , ResultSetMetaData ) |
ResultSet Metadata | Column-specific info from a result set |
Data Types | Mappings between SQL and Java types |
Exceptions | Handles errors during JDBC operations using SQLException and subtypes |