JDBC basic steps of:
1. registration drive
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
2. Connect
//DriverManager.getConnection("jdbc:mysql://localhost/test?user=monty&password=greatsqldb");
// 2 to establish a connection parameters: + protocol to access the database, two parameters: user name, parameters III: password.
conn = DriverManager.getConnection("jdbc:mysql://localhost/student", "root", "root");
3. Create statement
// 3. Create statement, dealing with the database, then you need this object
st = conn.createStatement();
4. Perform sql, to give ResultSet
4 // Execute the query result set
String sql = "select * from t_stu";
rs = st.executeQuery(sql);
The traverse the result set
// 5. traversal query each record
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println("id="+id + "===name="+name+"==age="+age);
}
6. Release Resource
if (rs != null) {
try {
rs.close();
} catch (SQLException sqlEx) { } // ignore
rs = null;
}