Java learning - a little sharing about JDBC

1. Download the driver and deploy
       We need to connect to the database before operating the database (here, Intellij Idea is used to connect MySQL as an example).

  (1) Create a new Idea project, download the database connection driver, and put it in the Idea project folder.

 

  (2), load the JDBC driver

    Before connecting to the database, first load the driver of the database you want to connect to the JVM (Java Virtual Machine), which can be implemented with the forName(String className) method.

 

2. Connect to the database and get the database connection object

  (1) Parameters required for JDBC connection (user, password, url)

    a. user username

    b. password password

    c. url defines the protocol, sub-protocol, and data source identifier when connecting to the database.

  (2) Create a database connection    
      a. To connect to the database, you need to request and obtain the Connection object from java.sql.DriverManager, which represents a database connection.    
      b. Use the getConnectin(String url , String username , String password ) method of DriverManager to pass in the specified path of the database to be connected, the username and    
       password of the database to obtain.

  (3) Create a preparedStatement

      To execute a SQL statement, you must obtain an instance of java.sql.Statement. The Statement instance is divided into the following three types:    
        a. Execute static SQL statements. Usually implemented through a Statement instance.    
        b. Execute dynamic SQL statements. Usually implemented through a PreparedStatement instance.    
        c. Execute database stored procedures. Usually implemented through a CallableStatement instance

3. Execute SQL statements    
     The Statement interface provides three methods for executing SQL statements: executeQuery, executeUpdate and execute  

  The database connection object conn calls the above statement method (static sql statement), and assigns the SQL statement to stu_Mgr_select_select_all in the form of a string

  There are two cases of operating the result set:    
       1. The records that are affected by this operation are returned when the update is performed.    
       2. The result returned by executing the query is a ResultSet object (the following is the query)

 

4. Close the JDBC object resource    

  After the operation is completed, all used JDBC objects should be closed to release JDBC resources.

===================================================== =======================================Dividing line

This is my first blog post. If there is anything wrong, please correct me.

The source code is placed below, the driver can go to Sun to download the latest version

package studentScoreMgrSystem; 
//Introduce interface, connect to database, perform database operation, use jdbc
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql. Statement;
class selectStudentMssg_handler implements studentMssg_interface {
public void selectStu() {
System.out.println("Find student information");
//Load jdbc driver package
String driver = "com.mysql.jdbc.Driver";
//Database connection String
String url = "jdbc:mysql://127.0.0.1:3306/curd";
// Username
String username = "root";
// Password
String password = "4950";
Connection conn = null;
Statement stu_Mgr_select_stmt = null;
ResultSet stu_Mgr_select_rs = null;
try {
// 1. Load the database driver (after successful loading, the instance of the Driver class will be registered in the DriverManager class)
Class.forName(driver);
// 2. Get the database connection
conn = DriverManager.getConnection(url, username, password);

if (!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");
// 3. Get the database operation object
stu_Mgr_select_stmt = conn. createStatement();
// 4. The SQL statement that defines the operation
String stu_Mgr_select_select_all = "select * from stu_score";
// 5. Execute the database operation and store the result in stu_Mgr_select_
stu_Mgr_select_rs = stu_Mgr_select_stmt.executeQuery(stu_Mgr_select_select_all);
// 6. Get and operate the result set

} catch (Exception e) { e.
printStackTrace();
}finally {
// 7. Close the object and recycle database resources
if (stu_Mgr_select_rs != null ) { //Close the result set object
try {
stu_Mgr_select_rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stu_Mgr_select_stmt != null) { //Close the database operation object
try {
stu_Mgr_select_stmt.close ();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) { // close the database connection object
try {
if (!conn.isClosed()) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326497499&siteId=291194637