Java link database easy to mix when building SQL statements

Source: Internet
Author: User

Connection conn = Dbhelper.getconnection ();    // encapsulates a tool class that connects to a database    String sql = "SELECT * from T_test";     = conn.createstatement ();     == dbhelper.getconnection ();    // encapsulates a tool class that connects to a database    String sql = "Delete from t_test where user_id =?" ;     = conn.preparestatement (sql);    Pst.setstring (1, Userid.trim ());     ps.executeupdate ();

The difference between this code is that one is a query statement, one is a conditional query statement, when to use what statement commands are executed by the return value of the SQL statement to determine

1. ResultSet executeQuery (String sql); Executes the SQL query and returns the ResultSet object.

2. int executeupdate (String sql); Executable increment, delete, change, returns the number of rows affected by execution.

3. Boolean execute (String sql); Executes any SQL statement that returns a Boolean value that indicates whether to return resultset.

Method ExecuteQuery
The statement used to produce a single result set, such as a SELECT statement. The most used method of executing SQL statements is executeQuery. This method is used to execute the SELECT statement, which is almost the most frequently used SQL statement.

Method executeupdate
Used to execute INSERT, UPDATE, or DELETE statements, as well as SQL DDL (data definition language) statements, such as CREATE table and DROP table. The effect of an INSERT, UPDATE, or DELETE statement is to modify one or more columns in 0 or more rows in a table. The return value of Executeupdate is an integer that indicates the number of rows affected (that is, the update count). For statements that do not manipulate rows such as CREATE table or DROP table, the return value of executeupdate is always zero. The Executeupdate method is used because the SQL statement in Createtablecoffees is a DDL (data definition Language) statement. Creating tables, altering tables, and deleting tables are examples of DDL statements that are executed using the Executeupdate method. You can also see from its name that method Executeupdate is also used to execute an UPDATE table SQL statement. In fact, Executeupdate is more time-dependent for updating tables than creating tables, because tables need to be created only once, but are often updated.

Method Execute:
Used to perform statements that return multiple result sets, multiple update counts, or a combination of both.

Java link database easy to mix when building SQL statements

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.