Java database connectivity (JDBC)

Java Database Connectivity (JDBC) is an application programming interface (API) that allows Java programs to interact with relational databases. It provides a set of classes and methods to establish a connection to the database, execute SQL queries, and retrieve the results.

JDBC is a standard Java API and is included in the Java Development Kit (JDK). It supports all major databases, including Oracle, MySQL, Microsoft SQL Server, and PostgreSQL.

Connecting to a Database

To connect to a database using JDBC, you need to follow these steps:

  1. Load the JDBC driver for your database.
    Class.forName("com.mysql.jdbc.Driver");
    
  2. Establish a connection to the database.
    String url = "jdbc:mysql://localhost:3306/mydatabase";
    String username = "user";
    String password = "password";
       
    Connection connection = DriverManager.getConnection(url, username, password);
    
  3. Create a Statement or PreparedStatement object for executing SQL queries.
    Statement statement = connection.createStatement();
    
  4. Execute SQL queries and retrieve the results.
    ResultSet resultSet = statement.executeQuery("SELECT * FROM users");
       
    while (resultSet.next()) {
        String name = resultSet.getString("name");
        int age = resultSet.getInt("age");
        System.out.println("Name: " + name + ", Age: " + age);
    }
    
  5. Close the connection and release any resources.
    resultSet.close();
    statement.close();
    connection.close();
    

Handling Exceptions

When working with JDBC, it is important to handle exceptions properly. JDBC throws several SQLException subclasses that indicate different types of database-related errors (e.g., connection failure, query execution error).

A common approach to exception handling is to use try-catch-finally blocks to catch and handle exceptions, and ensure that resources are properly released in the finally block.

try {
    // JDBC code here
} catch (SQLException e) {
    e.printStackTrace();
} finally {
    // Close resources here
}

Conclusion

JDBC is a powerful API that enables Java programs to interact with relational databases. By following the steps outlined in this article, you can establish connections, execute queries, and retrieve results from a database. Proper exception handling is essential to ensure robustness and reliability in your JDBC code. With JDBC, Java developers can easily integrate their applications with various databases to store and retrieve data efficiently.

#Java #JDBC