Several methods of database query __ Database

Source: Internet
Author: User

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 ();

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.