Basic steps for JDBC to access a database:
(1) The JDBC driver of the database is loaded into classpath, and in the actual development of the Web application based on Java EE,
Typically, the JDBC driver for the target product is copied to the Web-inf/lib
(2) Load the JDBC driver and register it in DriverManager
Oracle8/8i/9i (thin mode) database
Class.forName ("Oracle.jdbc.driver.OracleDriver"). newinstance ();
SQL server2005 Database
Class.forName ("Com.microsoft.jdbc.sqlserver.SQLServerDriver"). newinstance ();
MySQL Database
Class.forName ("Com.mysql.jdbc.Driver"). newinstance ();
(3) Establish a database connection. Get Connection Object
MySQL Database
String url= "jdbc:mysql://localhost:3306/test?user=root&passwordroot&useuniclode=true& characterencoding=gb2312 ";
Connection conn=drivermanager.getconnection (URL);
(4) Creating statement objects or PreparedStatement objects
Statement stat=conn.createstatement ();
Creating PreparedStatement Objects
String sql= "SELECT * from Test where username=? and password=? ";
PreparedStatement pstmt=conn.preparedstatement (SQL);
Pstmt.setstring (1, "admin");
Pstmt.setstring (2, "Hephec");
(5) Execute SQL statement
Execute a static SQL query
String sql= "SELECT * from users";
ResultSet rs=stmt.executequery (SQL);
Execute a dynamic SQL query
ResultSet Rs=pstmt.executequery ();
Execute statements such as Insert,update,delete, first define SQL
Stmt.executeupdate (SQL);
(6) Security Code result recordset ResultSet object
while (Rs.next ()) {
Out.println ("first field" +rs.getstring ());
Out.println ("second field" +rs.getstring ());
}
(7) Turn off the Resultset,statement,preparedstatement,connection object in turn, freeing the resource occupied
Project Summary-JDBC Basic steps to access a database