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!