The difference between execute, executeupdate, and ExecuteQuery (and the return value)

Source: Internet
Author: User
the difference between execute, executeupdate, and ExecuteQuery (and the return value)

One, Boolean execute (String sql)

allows the execution of query statements, UPDATE statements, and DDL statements.

when the return value is true, a query statement is executed, the result can be obtained by the Getresultset method, and when the return value is false, an UPDATE statement or a DDL statement is executed, and the Getupdatecount method gets the number of updated records.

Example:

<span style= "FONT-SIZE:16PX;" ><strong>public static void Main (string[] args) {  connection conn = null;   statement stm
= NULL;
  resultset rs = null;   try {   class.forname ("Com.microsoft.sqlserver.jdbc.SQLServerDriver");     conn = Drivermanager.getconnection ("jdbc:sqlserver://localhost:1433;databasename=test;user=sa;password=
Sasa ");
   stm = Conn.createstatement ();
   boolean ret = Stm.execute ("SELECT * from Stuinfo");    if (ret) {    rs = Stm.getresultset ();     while (Rs.next ( ) {     system.out.println ("Name:" +rs.getstring ("stuname") + "\ T Age:" +rs.getstring ("
Stuscore ")); &NBSP;&NBSP;&NBSP;&NBSP}   &nbsp}    ret = Stm.execute ("Update stuinfo set stuScore=62
Where Stuname= ' John ');
   int count = Stm.getupdatecount ();    if (!ret) { &nbsP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN (count+) data modification succeeded.
"); &NBSP;&NBSP;&NBSP}  &nbsp} catch (ClassNotFoundException e) {   e.printstacktrace ();   &NBSP} catch (SQLException e) {   e.printstacktrace ();   }    }</strong ></span>

two, int executeupdate (String sql)

executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement, or an SQL statement that does not return any content, such as a SQL DDL statement.

The return value is the number of records updated

III. ResultSet executequery (String sql)

executes the given SQL statement, which returns a single ResultSet object.


Execute is the synthesis of executeupdate and ExecuteQuery


Reference: executeupdate return value Baidu

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.