The differences among execute, executeUpdate, and executeQuery (and their return values) 1. boolean execute(String sql) Allows execution of query statements, update statements, and DDL statements. When the return value is true, it means that a query statement is executed, and the result can be obtained through the getResultSet method; when the return value is false, an update statement or DDL statement is executed, and 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 = stm.execute("select * from stuinfo"); if(ret){ rs = stm.getResultSet(); while(rs.next()){ System.out.println("Name: "+rs.getString("stuName")+"\tAge: "+rs.getString("stuScore")); } } ret = stm.execute("update stuinfo set stuScore=62 where stuname='张三'"); int count = stm.getUpdateCount(); if(!ret){ System.out.println(count+"The data was modified successfully!"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } 2. int executeUpdate(String sql) Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement, or an SQL statement that returns nothing (such as an SQL DDL statement). The return value is the number of records updated. 3. ResultSet executeQuery(String sql) Executes the given SQL statement, which returns a single ResultSet object. execute is a combination of executeUpdate and executeQuery Thank you for reading, I hope it can help you, thank you for your support of this site! You may also be interested in:
|
<<: How to use Dockerfile to create a mirror of the Java runtime environment
>>: How to reference external CSS files and iconfont in WeChat applet wxss
MySQL filtering timing of where conditions and ha...
When making a web page, you sometimes use a dividi...
Anyone who has read my articles recently knows th...
Table of contents 1. Installation 2. Import 3. De...
In my recent studies, I found some layout exercis...
1. Prepare the environment (download nodejs and s...
Slow log query function The main function of slow...
Basic environment configuration Please purchase t...
MySQL handles duplicate data Some MySQL tables ma...
1. Download MySQL Community Server 5.7.16 and ins...
<br />Original: http://uicom.net/blog/?p=762...
In actual web page development, accordions also a...
Sometimes in a project, due to some irreversible ...
Docker Overview Docker is an open source software...
In the MySQL database, after tables are associate...