1, Statement interface query
This is usually more commonly used. Statement Statement = Connection.createstatement ();
ResultSet rs = Getresultset ("select * from Tb_students WHERE age=20");
...
while (Rs.next ())
... {
...
}
2, PreparedStatement interface query
If you need to execute SQL statements more than once, PreparedStatement is preferred. Because he contains a compiled SQL statement, improve the efficiency and performance of the program.
The following is an example of querying, inserting, modifying, and deleting the tb_customers table respectively.
Inquire:
public void Prequerycustomerstb (int ID)
... {
Try
... {
PreparedStatement pstmt = connection.preparestatement ("select * from tb_customers WHERE ID =?;");
Pstmt.setint (1, ID);
Pstmt.executequery ();
Connection.commit ();
Pstmt.clearparameters ();
catch (SQLException e)
... {
E.printstacktrace ();
}
}
Insert: Public boolean preinsertcustomerstb (String company,string name,string address,string email,string phone,string othe R
... {
Try
... {
PreparedStatement pstmt = connection.preparestatement ("INSERT into Tb_customers (Company,name,address,email,phone, Other) VALUES (?,?,?,?,?,?); ");
Pstmt.setstring (1, company);
Pstmt.setstring (2, name);
Pstmt.setstring (3, address);
Pstmt.setstring (4, email);
Pstmt.setstring (5, phone);
Pstmt.setstring (6, other);
Pstmt.execute ();
Connection.commit ();
catch (SQLException e)
... {
TODO automatically generate catch blocks
E.printstacktrace ();
return false;
}
return true;
}
Modified: public boolean preupdatacustomerstb (int id,string company,string name,string address,string email,string phone,St Ring and other)
... {
Try
... {
PreparedStatement pstmt = connection.preparestatement (
"Update tb_customers SET company =., name =?, address =?, email =?, phone =?, and other =?" WHERE ID =?; ");
Pstmt.setstring (1, company);
Pstmt.setstring (2, name);
Pstmt.setstring (3, address);
Pstmt.setstring (4, email);
Pstmt.setstring (5, phone);
Pstmt.setstring (6, other);
Pstmt.setint (7, id);
Pstmt.execute ();
Connection.commit ();
}