For jdbc connection to the database, mysql is the database here. I divide it into four steps. Each person has his own division method, and the general basic process remains the same.
Step 1: load the database driver. Here is the mysql driver. Note that you can download a mysql driver by yourself. The version is not limited.
[Java]
Try {
Class. forName ("com. mysql. jdbc. Driver"); // load the MySql Driver Class
} Catch (ClassNotFoundException e ){
// TODO Auto-generated catch block
System. out. println ("MYSQL driver class not found ");
E. printStackTrace ();
}
Step 2: create a Connection, declare a Connection variable, and prepare the database url. My URL is:
[Java]
Private String url = "jdbc: mysql: // localhost: 3306/stu? User = root & password = 123456 "; // set your own Database password
Then execute the following:
[Java]
Try {
Con = DriverManager. getConnection (url );
} Catch (SQLException e ){
// TODO Auto-generated catch block
System. out. println ("connection failed! ");
E. printStackTrace ();
}
Step 3: You need to query, modify, delete, and so on. Here I only make a small query example:
[Java]
Public void chaxun ()
{
String query = "select * from 09 xinji where id = 2 ";
String bu;
Statement stmt = null;
ResultSet rs = null;
Try {
Stmt = con. createStatement ();
Rs1_stmt.exe cuteQuery (query );
While (rs. next ())
{
Bu = "student ID:" + rs. getObject (1) + ''+" name: "+ rs. getObject (2) + ''+" Gender: "+ rs. getObject (3) + "Age:" + rs. getObject (4 );
System. out. println (bu );
}
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Here we only query a record. I have not studied how to store the query results for multiple records. I have time to make up for it.
Part 4: Since we have enabled the connection, we need to close it when we don't need it. Otherwise, it will occupy system resources, especially the server-side programs. A large number of connections will cause a crash, so when it is used up, it will be shut down!
[Html] view plaincopyprint?
Try {
Rs. close ();
Stmt. close ();
Con. close ();
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
After completing the four steps, you can do some simple data processing.
I will complete it and paste the display as well:
[Html]
Import java. SQL .*;
Public class Shujukucon {
Private Connection con = null;
Private String url = "jdbc: mysql: // localhost: 3306/stu? User = root & password = 123456 "; // set your own Database password
Public Connection getcon ()
{
Try {
Class. forName ("com. mysql. jdbc. Driver"); // load the MySql Driver Class
} Catch (ClassNotFoundException e ){
// TODO Auto-generated catch block
System. out. println ("MYSQL driver class not found ");
E. printStackTrace ();
}
Try {
Con = DriverManager. getConnection (url );
} Catch (SQLException e ){
// TODO Auto-generated catch block
System. out. println ("connection failed! ");
E. printStackTrace ();
}
System. out. println ("connection successful! ");
Return con;
}
// If the connection is successful, try to query the data.
Public void chaxun ()
{
String query = "select * from 09 xinji where id = 2 ";
String bu;
Statement stmt = null;
ResultSet rs = null;
Try {
Stmt = con. createStatement ();
Rs1_stmt.exe cuteQuery (query );
While (rs. next ())
{
Bu = "student ID:" + rs. getObject (1) + ''+" name: "+ rs. getObject (2) + ''+" Gender: "+ rs. getObject (3) + "Age:" + rs. getObject (4 );
System. out. println (bu );
}
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Try {
Rs. close ();
Stmt. close ();
Con. close ();
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
Public static void main (String [] args ){
// TODO Auto-generated method stub
Shujukucon sjk = new Shujukucon ();
Sjk. getcon ();
Sjk. chaxun ();
}
}
Author: yinqianhui1990