JDBC Classes and Interfaces in Java
Core JDBC Interfaces
1. Driver: Interface for loading and registering database drivers. Implemented by database vendors.
2. Connection: Establishes a connection between Java and the database.
3. Statement: Used to execute static SQL queries like SELECT, INSERT.
4. PreparedStatement: Subclass of Statement. Used for parameterized/precompiled queries.
5. CallableStatement: Used to execute stored procedures.
6. ResultSet: Represents the result of a SQL query.
7. ResultSetMetaData: Provides info about ResultSet like column names, types.
8. DatabaseMetaData: Gives metadata about the database (e.g., DB version, table names).
9. SQLException: Exception to handle database access errors.
Common JDBC Classes
1. DriverManager: Manages database drivers and creates DB connections.
2. Types: Contains constants representing SQL data types.
3. Date, Time, Timestamp: Represent SQL date/time values.
Typical JDBC Program Flow
import [Link].*;
class JdbcExample {
public static void main(String[] args) {
try {
[Link]("[Link]");
Connection con = [Link](
"jdbc:mysql://localhost:3306/testdb", "root", "password");
Statement stmt = [Link]();
ResultSet rs = [Link]("SELECT * FROM users");
while ([Link]()) {
[Link]([Link](1) + " " + [Link](2));
}
[Link]();
JDBC Classes and Interfaces in Java
} catch (Exception e) {
[Link](e);
}
}
}