execute、executeUpdate、executeQuery三者的区别及各自返回值

 1. ResultSet executeQuery(String sql); 执行SQL查询,并返回ResultSet 对象。
 2.int executeUpdate(String sql); 可执行增,删,改,返回执行受到影响的行数。
 3. boolean execute(String sql); 可执行任何SQL语句,返回一个布尔值,表示是否返回ResultSet 。

execute是executeQuery和executeUpdate的综合.

-----

executeUpdate() 这是 PreparedStatement 接口中的方法
executeUpdate(String sql) 这是 PreparedStatement 从父接口 Statement 中继承过来的方法

executeUpdate() 中执行 SQL 语句需要在创建 PerparedStatement 时通过 Connection 的 prepareStatement(String sql) 方法中写出,因为 PerparedStatement 中的 SQL 语句数据库需要进行预编译和缓存,因此要在创建 PerparedStatement 对象时给出 SQL 语句。

而 executeUpdate(String sql) 是 Statement 中的方法,参数中的 SQL 语句只是提交给数据库去执行,并不需要预编译。

如果 SQL 语句中有 ? 占位符,那么在设置好占位符中的值后,必须使用 executeUpdate() 执行。而 executeUpdate(String sql) 只是提交一个 SQL 语句,且这个语句中不能带有 ? 占位符。
原文地址:https://www.cnblogs.com/winkey4986/p/2300266.html