Writing Java programs that access databases also requires several important classes and interfaces.
DriverManager class
The DriverManager class handles the loading of drivers and establishing new database connections. DriverManager is a class in the java.sql package used to manage database drivers. Usually, applications only use the getConnection() static method of the DriverManager class to establish a connection with the database and return a Connection object:
static Connection getConnection(String url,String username,String password)
Specify the URL username and password of the data to create a database connection object. The syntax format of url is:
jdbc:<database connection mechanism>:<ODBC database name>.
Connection class
The Connection class is a class in the java.sql package that handles connections to a specific database. The Connection object is an object used to represent a database connection. Java programs operate on this kind of object. The main methods of the Connection class are:
Statement class
The Statement class is a class in the java.sql package used to process SQL statements in a 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 to connect to the database, and let the object connect to the database. Call the static method getConnection() of the class DriverManager to obtain the Connection object to connect the program to the database. Then, use the Statement class to declare the SQL statement object, and call the createStatement() method of the Connection object to create the SQL statement object. For example, the following code creates the statement object sql:
Statement sql = null; try{ sql = con.createStatement(); }catch(SQLException e){}
ResultSet class
After you have the SQL statement object, call the executeQuery() method of the statement object 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 score table and stores it in the rs object:
ResultSet rs = sql.executeQuery("SELECT * FROM ksInfo");
The ResultSet object is actually a table containing query result data. It is a tubular data set composed of data rows in a unified form, and one row corresponds to one query record. There is a cursor implicit in the ResultSet object. Only the data row currently pointed to by the cursor can be obtained at a time. Use the next method to get the next data row. Use the field (column) name or position index (starting from 1) of the data row to call the getXXX() method to obtain the field field of the record. The following are some methods of the ResultSet object:
columnIndex in the above method is the position index, used to specify the field, and columnName is the field name.
The user needs to browse the query result set, or move forward and backward, or display the specified records of the result set. This is called a scrollable result set. To obtain a scrollable result set, the program only needs to add two parameters of the specified result set when obtaining the SQL statement object. For example, the following code:
Statement stmt = con.createStatement(type,concurrency); ResultSet rs = stmt.executeQuery(SQL statement)
The SQL query of the statement object stmt can obtain the result set of the corresponding type.
The int type parameter type determines the scrolling method of the scrollable set:
The int type parameter concurrency determines whether the database is updated synchronously with the scrollable set:
For example, the following code uses the connection object connect to create the Statement object stmt, specifies that the result set can be scrolled, and reads the database in read-only mode:
stmt = connect.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
Some other commonly used methods on scrollable sets are as follows: