a: the working principle of JDBC
1. The JDBC API
Credit: Sun
Content: used for programmers call interfaces and classes, integrated in the Java. SQL and javax.mail SQL packages, such as:
DriverManager class
Connection interface
The Statement interface
The ResultSet interface
2. The DriverManager
Credit: Sun
Role: manage all kinds of different JDBC driver
3. JDBC driver
Credit: database vendors
Role: responsible for connecting a variety of different database
2: JDBC API
JDBC API main functions: with the database connection is established, and executing SQL statements and process the results
DriverManager: on the basis of the different database, managing JDBC driver
Connection: connect to the database and the task of transmitting data
Statement: is produced by the Connection, is responsible for executing SQL statements
After the Statement execution ResultSet: responsible for the query results generated by the
code:
Connection conn=null; open/close the database using the
Statement sta=null; //interface operation used SQL statement
ResultSet rs=null; //interface query Sql statements used
try {
//the first step:
Class. Class.forname (". Com. Mysql. JDBC Driver ");
//code set to solve the code? UseEncoding=true& CharacterEncoding=utf8
//the second step: open links (links database)
String url="JDBC: mysql://localhost: 3306/dog? UseEncoding=true& CharacterEncoding=utf8 "; //connection myschool database path
String the user "root"; //login database user name
String PWD="zz1314"; //login database password
conn=DriverManager. GetConnection (url, the user, the PWD).
System. The out. Println (" connect to the database success ");
//the third step: executing SQL statements
=conn. Sta createStatement ();
//add the Sql statement
String SQL="INSERT INTO the master (` name `, ` money `) VALUES (' Leo ', 200), (' city press, 500)";
System. The out. Println (SQL);
//increase deletion method executeUpdate (SQL)
int num=sta. ExecuteUpdate (SQL);
//the fourth step: processing results
if (num> 0) {//determine whether add success
System. The out. Println (" insert success ");
}
/*
Modify the Sql statement
String sql1="Update dog set health=60 love=100 where id=1";
Int num1=sta. ExecuteUpdate (SQL);
If (num1 & gt; 0) {//determine whether add success
System. The out. Println (" insert success ");
}
//the third step: executing SQL statements (placeholder)
String SQL="INSERT INTO the master (` name `, ` money `) VALUES (?,?,?,?,? ,?) ";
Psta=conn. PrepareStatement (SQL);
Psta. SetString (1, "hey hey");
Psta. SetInt (2100);
System. The out. Println (SQL);
Int num=psta. ExecuteUpdate ();
If (num> 0)
System. The out. Println (" insert success ");
*/
//the Sql query
String sql2="select * from the master".
Rs=sta. ExecuteQuery (sql2);
While (rs), next ()) { //go to the next data,
//get data types () method takes the data column names or subscript 1 start no 0
int id=rs. Get int (" id "); //receive access to the value of the
String name=rs. Get String (" name "); //receive access to the value of the
I nt money=rs. Get int (" money ");
System. Out. Println (id + "\ t" + money + name + "\ t"); //
}
} the catch (Exception e) {
System. The out. Println (" error "+ um participant etMessage ());
} the finally {
//step 5: close the database
try {
If (rs!=null) {
Rs. The close ();
}
If (sta!=null) {
Sta. The close ();
}
If (conn!=null) {
Conn. Close ();
}
System. The out. Println (" close the database success ");
} the catch (SQLException e) {
e.printStackTrace();
}
}