JDBC  «Prev 

Connection Object versus statement object in JDBC

Question: What is the difference between a connection object and statement object in JDBC?
JDBC (Java Database Connectivity) is an API (Application Programming Interface) used to interact with databases in Java applications. It provides a set of interfaces and classes that allow Java developers to establish database connections, execute SQL queries, and process the results. Two fundamental components in JDBC are the Connection object and the Statement object, each having a distinct role:
  1. Connection object: A Connection object represents a connection to a specific database. It acts as a conduit between the Java application and the database server. Using a Connection object, you can perform tasks such as establishing and closing connections, managing transactions, and creating Statement objects to execute SQL queries. To obtain a Connection object, you typically use the DriverManager class's getConnection() method. The method takes a database URL, along with optional username and password parameters, depending on the database server's authentication requirements.
    Example:
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myDatabase", "username", "password");
    
  2. Statement object: A Statement object is used to execute SQL queries and commands against the database. It is created from an existing Connection object using the createStatement() method. There are three main types of Statement objects in JDBC:
    1. Statement: Used for executing simple SQL queries without parameters.
    2. PreparedStatement: Used for executing parameterized SQL queries, providing better performance and security compared to the regular Statement object. It is especially useful for preventing SQL injection attacks.
    3. CallableStatement: Used for executing stored procedures and functions in the database.
    After creating a Statement object, you can execute SQL queries using methods like executeQuery() (for SELECT queries) or executeUpdate() (for INSERT, UPDATE, or DELETE queries). Once executed, you can process the results using ResultSet objects.
    Example:
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * FROM users");
    
In summary, the Connection object in JDBC represents the connection to a database, while the Statement object is used to execute SQL queries and commands. The Connection object is responsible for creating Statement objects and managing the connection to the database, while the Statement object handles SQL execution and processing the results

JDBC Connection and Statement Object Diagram

1) A Connection object linked to a database is returned to the JDBC application.

2) A statement object is returned to the JDBC program by the Connection object.

3) The Statement object executes a query on the database, returning a ResultSet to the application.

4) The cursor is moved to the fourth row of the ResultSet.


5) The hospital column of row four is changed to a "A new Name" in the ResultSet.

6) The changes are applied to the database.