(1) Call the Class.forName () method to load the appropriate database driver
Class.forName ("Com.mysql.jdbc.Driver");
(2) Call the Drivermanager.getconnection () method to obtain a connection object that represents an open connection
Connection conn = drivermanager.getconnection (URL, username, password);
(3) Use the Connection.createstatement () method to create a statement statement object that passes simple SQL statements without arguments to the database management system for execution
Statement stmt = Conn.createstatement ();
Use the Connection.preparestatement () method to create a PreparedStatement statement object that passes an SQL statement with one or more input parameters
Preparestatement PSM = conn.preparestatement ("INSERT into book (BookID, Name) VALUES (?,?)");
Use the Connection.preparecall () method to create a CallableStatement statement object that calls the stored procedure
CallableStatement CSM = Conn.preparecall ("{Call validate (?,?)}"); –validate is the name of the stored procedure
(4) EXECUTE statement
Execute Query statement
ResultSet rs = stmt.executequery (SQL);
Executing DML statements (that is, insert, UPDATE, DELETE)
int n = stmt.executeupdate (sql);
The Execute () method can execute both of these SQL statements
(5) Handling ResultSet objects
while (Rs.next ()) {
Rs.getstring (1);
......
}
(6) Close the connection
Rs.close ();
Stmt.close ();
Conn.close ();
Java programs connect to MySQL database