Differences between execute, executeupdate, and executequery (and return values)

Source: Internet
Author: User

1. boolean execute (string SQL)

Query statements, update statements, and DDL statements can be executed.

If the return value is true, the query statement is executed and the result can be obtained using the getresultset method. If the return value is false, the update statement or DDL statement is executed, the getupdatecount method obtains the number of updated records.

Example:

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 = cmd.exe cute ("select * From stuinfo ");
If (RET ){
Rs = STM. getresultset ();
While (Rs. Next ()){
System. Out. println ("name:" + Rs. getstring ("stuname") + "\ t age:" + Rs. getstring ("stuscore "));
}
}
Ret = cmd.exe cute ("Update stuinfo set stuscore = 62 Where stuname = 'zhangsan '");
Int COUNT = STM. getupdatecount ();
If (! RET ){
System. Out. println (count + "data records modified successfully! ");
}
} Catch (classnotfoundexception e ){
E. printstacktrace ();
} Catch (sqlexception e ){
E. printstacktrace ();
}
}

Ii. Int executeupdate (string SQL)

Execute a given SQL statement, which may be an insert, update, or delete statement, or an SQL statement (such as an SQL DDL statement) that does not return any content ).

The returned value is the number of updated records.

Iii. resultset executequery (string SQL)

Execute the given SQL statement, which returns a single resultset object.

Execute is a combination of executeupdate and executequery.

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.