JDBC learning notes (four)
Thinkersky [yanghuangming@rongji.com]
Last night, I dreamed of my poor chief, that I had to respect and hateful sister. I don't know why, until now I still feel that is just a dream. If it is a dream, I hope that my sister can be happy in the dream. I heard that people didn't have pain in heaven. I believe.
......
Well, pick up yesterday's stuff.
Second, access the database via PreparedStatement object
In front, we discussed the use of the connection object Connection to generate a Statement object, and then interact with the database management system. The Statement object passes the statement to the database every time the SQL statement is executed. This is less efficient when performing the same statement multiple times. The solution to this problem is to use the PreparedStateMent object. If the database supports precompilation, you can pass the SQL statement to the database when you create a PreparedStatement object, and you can improve much when this SQL statement is executed. If the database does not support precompilation, it is transmitted to the database when the statement is executed. This is completely transparent for users.
The SQL statement of the PreparedStatement object can also accept parameters. In the statement, it is pointed out that you need to accept those parameters and then prepare the precompilation. At each execution, different parameters can be passed to the SQL statement, which greatly improves the efficiency and flexibility of the program. Under normal circumstances, all PREPAREDSTATEMENT objects are used with input parameters.
For better understanding, please see the following example:
Package com.rongji.deemo;
Import java.sql.connection;
Import java.sql.driverManager;
Import java.sql.statement;
Import java.sql.databaseMetadata;
Import java.sql.preparedStatement;
Public class dataconn {
Public dataconn () {
}
Public static void main (String [] args) {
Try
{
// Load the driver
// The following code is to load the JDBD-ODBC driver
Class.Forname ("Oracle.jdbc.driver.OracleDriver");
//establish connection
// Second step is to connect to DBMS with an appropriate driver, look at the following code [You can modify the database related information you are connected]:
String Url = "JDBC: Oracle: Thin: @ 192.168.4.45: 1521: Oemrep";
String user = "ums1";
String password = "rongji";
// Create a connection with URL
Connection con = DRIVERMANAGER.GETCONNECTION (URL, User, Password);
// There are several fields in the current table: ID, Name, Password, Text, Note
PreparedStatement InsertStatement = con.preparestatement (
"INSERT INTO RBAC_Application Values (?,?,?,?");
INSERTSTATEMENT.SETINT (1, 10);
INSERTSTATEMENT.SETSTRING (2, "Thinkersky");
INSERTSTATEMENT.STSTSTRING (3, "88888");
INSERTSTATEMENT.SETSTRING (4, "This is a test application"); InsertStatement.setstring (5, "Remarks");
Int results = insertstatement.executeUpdate ();
System.out.println ("The Result IS" Result);
C. close ();
}
Catch (Exception E)
{
// Output exception information
System.err.println ("SQLException:" E.GetMessage ());
E.PrintStackTrace ();
}
}
}
I believe this example, the application of PREPAREDSTATEMENT should have a preliminary understanding. Well, let me introduce this example :)
1. Create a preparedStatement object
The PreparedStatement class is a subclass of the Statement class. Like the StateMetn class, the object of the PreparedStatement class is also based on the Connection object. As follows:
PreparedStatement InsertStatement = con.preparestatement (
"INSERT INTO RBAC_Application Values (?,?,?,?");
When you create this preparedStateMent object, the corresponding plug-in recorded SQL statement has been passed to the database management system for precompilation.
2, provide parameters for preparedStatement objects
If a PreparedStatement object is created in the form of a SQL statement with input parameters (most cases in most cases). Before the SQL statement is executed correctly by the database management system, it must be parameters (which is where the SQL statement is '?') Is initialized. The initialization method is to call a series of setxxxx () methods for the PreparedStatement class. If the data type of the input parameter is an INT type, call the setint () method; if the input parameter is a String type, call the setString () method. Generally speaking, the simple and composite data types provided in Java can find the corresponding setxxx () method.
Now let's look back at the initialization of several parameters in our example:
INSERTSTATEMENT.SETINT (1, 10);
INSERTSTATEMENT.SETSTRING (2, "Thinkersky");
INSERTSTATEMENT.STSTSTRING (3, "88888");
INSERTSTATEMENT.SETSTRING (4, "This is a test application");
INSERTSTATEMENT.SETSTRING (5, "Remarks");
Here, the setxxx () method typically has two parameters, and the first parameter is an INT type, which indicates that the second parameter of the JDBC PreparedStatement object will be initialized. The value of the second parameter is the parameter of PreparedStateTn will be initialized, and the data type is naturally the same. Here, it will be described here that the value of the parameter remains unchanged after the parameters of an object of PreparedStatement, until he is again assigned.
3, call the ExecuteUpdate () method of the preparedStatement object
Here, to clear the ExecuteUpdate () method of the PreparedStatement object is different from the executeUpdate () method of the Statement object, the former is without parameters, and the parameters of the SQL statement model they need are already provided when instanting the object. In addition, the executeUpdate () method will return an integer that represents the number of rows recorded in the updated database in which the executeUpdate () method is executed. After the above example is performed, the result is returned to 1. So what is the case? Actually, when the return value of the ExecuteUpdate () method of the PreparedStatement object is 0. There are two possibilities: (1) The executed SQL statement is to operate the record of the database management system; and no record is updated
(2) The executed SQL statement is a DDL language that operates on the table, view of the database management system, and no data record is directly modified.
OK, late, I have to go to work tomorrow, go to bed early, I hope to see my sister in my dreams.