Statement Resultset
The Connection object is an online connection between Java and the database. Let's perform SQL, you must get a statement object, which replaces us to perform SQL narrative and obtain the results after the execution, we can use Connection's CreateStatement () to create a Statement object:
Connection conn = drivermanager.getConnection
URL, User, Password;
Statement Stmt = conn.createstatement ();
After obtaining the Statement object, we can use ExecuteUpdate (), ExecuteQuery () to perform SQL, and executeUpdate () is primarily used to perform CREATE TABLE, INSERT, DROP TABLE, ALTER TABLE, etc. SQL, such as:
Stmt.executeUpdate ("INSERT INTO MESSAGE VALUES ('Michael Chen',"
"'CATERPILLAR@mail.com', 'message",' 2005-2-4 ', "
"'welcome')");
The executeQuery () method is SQL for SELECT and other query databases, executeUpdate () and executeQuery () will return the resultSet object, represent the result of change or query, the result of the query is a profile, we use Next () To move to the next information, it will return TRUE or FALSE to indicate whether there is a next information, then we can use getxxx () to get information, such as getString (), getfloat (), getdouble () and other methods The corresponding field type information is obtained separately, and the getxxx () method provides information that has been obtained according to a field name, or a method of obtaining information according to the order of field, one example, we specify the field name to get information:
ResultSet Result =
Stmt.executeQuery ("SELECT * from message");
While (result.next ()) {
System.out.print (Result.getstring ("Name") "/ t");
System.out.print (Result.getstring ("email") "/ t");
System.out.print (Result.getstring ("Subject") "/ t");
System.out.print (Result.getstring ("Time") "/ T");
System.out.println (Result.getstring ("MEMO") "/ t");
}
The way the result is displayed as follows:
ResultSet Result =
Stmt.executeQuery ("SELECT * from message");
While (result.next ()) {
System.out.print (Result.getstring (1) "/ t");
System.out.print (Result.getstring (2) "/T" ";system.out.print(Result.getstring (3) " / t ");
System.out.print (Result.getstring (4) "/ t");
System.out.println (Result.getstring (5) "/ t");
}
Statement's execute () can also be used to perform SQL, and can be used to test the executed SQL is performing an update or query, if you return to True, indicating that SQL execution will pass back more information, that is, execute query action, if FALSE, indicating that the SQL execution result will pass the number of rows that are affected, that is, the update action, if you can't know that the query or update is in advance, you can use Execute ().
The following program is a complete example. Note that after the query is over, you can use the Statement's Close () method to release the Statement's database resource with JDBC resources, and finally use the connection's close () to turn off the online:
Dbtest.java
Package online.
Import java.sql. *;
Public class dbtest {
Public static void main (String [] args) {
String driver = "com.mysql.jdbc.driver";
String url = "JDBC: mysql: // localhost: 3306 / Guestbook?"
"Useunicode = true & characterencoding = BIG5";
String user = "myname";
String Password = "123456";
Try {
Class.Forname (driver);
Connection conn = drivermanager.getConnection
URL, User, Password;
Statement Stmt = conn.createstatement ();
Stmt.execute ("INSERT INTO Message VALUES ('Michael Chen"
"',' blog.9cbs.net/ybugchen ',' message,"
"2005-2-4 ',' Welcome ')");
ResultSet Result = Stmt.executeQuery
"SELECT * from message");
While (result.next ()) {
System.out.print (Result.getstring (1) "/ t");
System.out.print (Result.getstring (2) "/ t");
System.out.print (Result.getstring (3) "/ t");
System.out.print (Result.getstring (4) "/ t");
System.out.println (Result.getstring (5) "/ t");
}
Stmt.close (); conn.Close ();
}
Catch (classnotfoundexception e) {
System.out.println ("Can't find driver");
E.PrintStackTrace ();
}
Catch (SQLException E) {
E.PrintStackTrace ();
}
}
}
Finally, it is noted that the Connection object is preset to automatic "commit", that is, STATEMENT executive SQL narrative, the database is immediately changed, and if you want to make SQL to be executed, the SQL wants to be executed, you can use SetAutocommit (false) will automatically recognize the cancellation. After executing SQL, call the CONNECTION's commit () method to be approved, using the connection GetAutoCommit () can test whether it is set to automatically recognize.
However, whether there is a commit () method, as long as SQL is not wrong, the recognition action will be performed before shutting down Statement or Connection, and the database is changed.