For MySQL execute、executeupdate‑executequery

Source: Internet
Author: User

For MySQL execute、executeupdate‑executequery

Differences between execute, executeUpdate, and executeQuery (and return values)

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.

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.