Introduction to common classes and interfaces for Java programs to connect to databases

  • 2020-04-01 04:14:33
  • OfStack

Writing a Java program that accesses a database also requires several important classes and interfaces.
DriverManager class

The DriverManager class handles driver loading and new database connections. DriverManager is a class in the java.sql package used to manage database drivers. Typically, applications use only the drivermanager-like getConnection() static method to establish a Connection to the database and return a Connection object:


  static Connection getConnection(String url,String username,String password)


Specifies the URL username and password for the data to create the database connection object. The syntax of the url is:
      JDBC: < Database connection mechanism > : < ODBC database name > .
The Connection class

The Connection class is the class in the java.sql package that handles connections to a particular database. A Connection object is an object used to represent a database Connection on which a Java program operates. The main methods of the Connection class are:

Statement createStatement() : creates a Statement object. Statement createStatement(int resultSetType,int resultSetConcurrency) : creates a Statement object that generates a result set with a specific type. Void commit() : commits a change to the database and releases the lock on the currently held database. Void rollback() : rolls back all changes in the current transaction and releases the lock on the database currently held by the connection. String getCatalog() : gets the current directory of the connection object. Boolean isClose() : determines whether the connection is closed. Boolean isReadOnly() : determines whether the connection is in read-only mode. Void setReadOnly() : sets the connection to read-only mode. Void close() : releases the database and JDBC resources for the connection object.

The Statement class

The Statement class is the class in the java.sql package used to process SQL statements in the specified connection. The point of database programming is to embed SQL commands in the program. The program needs to declare and create a Connection object that connects to the database and have that object connect to the database. The Connection object is obtained by calling the static method getConnection() of class DriverManager to connect the program to the database. Then, the SQL Statement object is declared with the Statement class and the createStatement() method of the Connection object is called to create the SQL Statement object. For example, the following code creates a statement object SQL:


  Statement sql = null;
  try{
    sql = con.createStatement();
  }catch(SQLException e){}


The ResultSet class

Once you have the SQL statement object, the method executeQuery() of the statement object is called to execute the SQL query and store the query results in an object declared with the ResultSet class. For example, the following code reads the student achievement table and stores it in the rs object:


  ResultSet rs = sql.executeQuery( " SELECT * FROM ksInfo " );


A ResultSet object is actually a table of query result data. It is a tubular data set, consisting of a uniform form of data rows corresponding to a query record. Implicit in the ResultSet object is a cursor that can only get the row the cursor currently refers to at a time, and the next row can be taken using the next method. The field plantation of the record is obtained by calling the getXXX() method with the field (column) name or location index of the data row (starting with 1). Here are some of the methods for a ResultSet object:

Byte getByte(int columnIndex) : returns the byte value of the specified field. Date getDate(int columnIndex) : returns the Date value of the specified field. Float getFloat(int columnIndex) : returns the floating-point value of the specified field. Int getInt(int columnIndex) : returns the integer value of the specified field. String getString(int columnIndex) : returns the String value of the specified field. Double getDouble(String columnName) : returns the double value of the specified field. Long getLong(String columnName) : returns the long integer value of the specified field. Boolean next() : returns whether there is a next field.

The columnIndex in the above method is a positional index used to specify the field, and columnName is the field name.

Users need to browse, move back and forth, or display a specified record of the query result set, which is called a scrollable result set. To obtain a scrollable result set, the program simply adds two parameters to the specified result set when obtaining the statement object of the SQL. For example, the following code:


  Statement stmt = con.createStatement(type,concurrency);
  ResultSet rs = stmt.executeQuery(SQL statements )


The SQL query of the statement object STMT results in a result set of the corresponding type.
Int parameter type determines the scrolling mode of scrollable set:

Resultset.type_forword_only, the ResultSet cursor can only scroll down. Resultset.type_scroll_insensitive, the cursor can be moved up and down, and the current ResultSet does not change when the database changes. Resultset.type_scroll_sensitive, the cursor can be moved up and down, and the current ResultSet changes synchronously when the database changes.

The int parameter concurrency determines whether the database is updated in sync with the scrollable set:

Resultset.concur_read_only, cannot update a table in the database with a ResultSet. Resultset.concur_updatetable, which updates tables in the database with result sets.

For example, the following code USES the connection object connect to create a Statement object STMT, specify that the result set is scrollable, and read the database read-only:


  stmt = connect.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
  ResultSet.CONCUR_READ_ONLY);


Other commonly used methods on scrollable sets are:

Boolean previous() : moves the cursor up to return false when moved to the first row of the result set. Void beforeFirst() : moves the cursor before the first row of the result set. Void afterLast() : moves the cursor after the last line in the result set. Void first() : moves the cursor to the first row. Void last() : moves the cursor to the last line. Boolean isAfterLast() : determines whether the cursor is after the last line. Boolean isBeforeFirst() : determines whether the cursor precedes the first line. Boolean isLast() : determines whether the cursor is on the last line. Boolean isFirst() : determines whether the cursor is on the first row. Int getRow() : gets the currently referenced line (the line number is numbered from 1, the result set is empty, and returns 0). Boolean absolute(int row) : moves the cursor to the row row.

Related articles: