Process of using the database:
1. Registration drive;
2. Use the Drivermanager.getconnection method to obtain the Connection object con;
A method:
3. Use the Createstatement () method of the Connection object to get the object stmt that can execute the SQL statement;
4.stmt Execute Query (query statement);
b Method (recommended):
3. Using the Preparestatement method of the Connection object, the preprocessing object pstmt with parameter (query statement) is obtained.
4.PSTMT executes the query and assigns the result set to the ResultSet object;
1 Public classdbtest{2 Public Static voidMain (string[] args) {3 /**4 * Use of the database:5 * 1. Registration drive;6 * 2. Use the Drivermanager.getconnection method to obtain the Connection object con;7 * A method:8 * 3. Use the Createstatement () method of the Connection object to get the object stmt that can execute the SQL statement;9 * 4.stmt Execution Query (query statement);Ten * B Method (recommended): One * 3. Using the Preparestatement method of the Connection object, the preprocessing object pstmt with parameter (query statement) is obtained. A * 4.PSTMT executes the query and assigns the result set to the ResultSet object; - */ - Try { theClass.forName ("Com.mysql.jdbc.Driver");//Registration Driver -Connection con = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/book", "root", "root123");//Get the Connection object - //using the statement method to handle the execution of SQL statements is slightly different from the PreparedStatement method. -Statement stmt=con.createstatement ();//get an object that can execute SQL statements +ResultSet rs=stmt.executequery ("select * from BookInfo where id=1");//executes the query and assigns the result set to the ResultSet object - /**The following methods are recommended: + preparedstatement pstmt=con.preparestatement ("select * from BookInfo where id=1");//Get Preprocessed object A ResultSet rs=pstmt.executequery (); Executes the query and assigns the result set to the ResultSet object at */ - while(Rs.next ()) -{intId=rs.getint (1); -System.out.println (rs.getstring (2) + "" "+rs.getstring (3) +" "+rs.getstring (4)); - } -}Catch(ClassNotFoundException e) {//The capture driver class could not find the exception in E.printstacktrace (); -}Catch(SQLException e) {//Capturing SQL Exceptions to e.printstacktrace (); + } - } the}
MySQL database usage flow, code interpretation