JDBC Connection interface.

Which type of Statement object to create?

  • With a java.sql.Connection object, you can create three alternate Statement objects. A Statement object is used for the further dialogue with the database.
    1. A Connection object has alternative methods to create a java.sql.Statement object, which can be used to perform a full written SQL statement to a database for a desired pre-selected action.
    2. A Connection object has also alternative methods to create a java.sql.PreparedStatement object, which can be used to perform a parameter based SQL statement to a database for a desired pre-selected action.
    3. A Connection object has also alternative methods to create a java.sql.CallableStatement object, which is mainly used to send a SQL statement to execute a Stored Procedure in a database.
  • A java.sql.Connection object has many methods to control transactions. How to handle the transaction will be explained later in this JDBC learning.
  • A java.sql.Connection object contains a method to retrieve metadata from the database:
    Method Description
    DatabaseMetaData getMetaData() Retrieves a DatabaseMetaData object that contains metadata about the database to which this Connection object represents a connection.

    The metadata includes information about the database's tables, its supported SQL grammar, its stored procedures, the capabilities of this connection, and so on.
    Example using the method getMetaData()
    import java.sql.Connection;
    import java.sql.DatabaseMetaData;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class JDBClClient {
    
     public static void main(String[] args) {
      Connection conn = null;
      try {
       // Register a driver for the MySQL database
       Class.forName("com.mysql.jdbc.Driver");
       // Create a url for accessing the MySQL
       // on local host, port 3306 and database CarDB
       String url = "jdbc:mysql://localhost:3306/CarDB";
       // user and passord to access the database
       String username = "root";
       String password = "root";
       // User the DriverManager to get a Connection to the database
       conn = DriverManager.getConnection(url, username, password);
       DatabaseMetaData dbmd = conn.getMetaData();
       System.out.println("DB name is .....  " + dbmd.getDatabaseProductName());
       System.out.println("DB version is ..  " + dbmd.getDatabaseProductVersion());
       System.out.println("DB driver name    " + dbmd.getDriverName());
       System.out.println("DB driver version " + dbmd.getDriverVersion());
       System.out.println("URL is .........  " + dbmd.getURL());
       System.out.println("User name is ...  " + dbmd.getUserName());
       ResultSet rs = dbmd.getTables(null, null, "%", null);
        while (rs.next()) {
          System.out.println("DB Table name: "+rs.getString("TABLE_NAME"));
        }
      } catch (ClassNotFoundException ex) {
       ex.printStackTrace();
      } catch (SQLException e) {
       e.printStackTrace();
      } finally {
       try {
        conn.close();
       } catch (Exception xe) {
        xe.printStackTrace();
       }
      }
     }
    }
    The result of executing this programm could be:
    DB name is .....  MySQL
    DB version is ..  5.1.68-community
    DB driver name    MySQL-AB JDBC Driver
    DB driver version mysql-connector-java-5.1.5 ( Revision: ${svn.Revision} )
    URL is .........  jdbc:mysql://localhost:3306/CarDB
    User name is ...  root@localhost
    DB Table name: carpark
    DB Table name: carrental
    You can download this example here (needed tools can be found in the right menu on this page).
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.