jdbc:executeUpdate()与executeUpdate(String sql)方法有什么区别

zhushoujun 2010-05-28 07:26:29
问题1.是不是dml(增加,删除,修改)语句都用executeUpdate方法?
问题2.executeUpdate()与executeUpdate(String sql)方法有什么区别?应该没有吧
...全文
2052 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
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 语句,且这个语句中不能带有 ? 占位符。
zhushoujun 2010-05-29
  • 打赏
  • 举报
回复
我还没懂无参的executeUpdate呢
dddddz 2010-05-29
  • 打赏
  • 举报
回复
executeUpdate() 这是 PreparedStatement 接口中的方法
executeUpdate(String sql) 这是 PreparedStatement 从父接口 Statement 中继承过来的方法
危险的大猫 2010-05-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 bao110908 的回复:]
executeUpdate() 这是 PreparedStatement 接口中的方法
executeUpdate(String sql) 这是 PreparedStatement 从父接口 Statement 中继承过来的方法

executeUpdate() 中执行 SQL 语句需要在创建 PerparedStatement 时通过 Connection 的 prepareStateme……
[/Quote]
嘿嘿..又见火龙果. 还记得我不
SIOSXIAOQIANG 2010-05-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 bao110908 的回复:]
executeUpdate() 这是 PreparedStatement 接口中的方法
executeUpdate(String sql) 这是 PreparedStatement 从父接口 Statement 中继承过来的方法

executeUpdate() 中执行 SQL 语句需要在创建 PerparedStatement 时通过 Connection 的 prepareStateme……
[/Quote]
又学会一些基础知识啊。
  • 打赏
  • 举报
回复
可以,由于存在 PreparedStatement 了,因此就没有什么理由再去使用 Statement 了。

至少有三个优点可以选择 PreparedStatement,让我们抛弃 Statement:

1:PreparedStatement 中的 SQL 语句采用参数占位符,对于数据库来说是预编译再填充参数,因此写的 SQL 中没有字符串与变量之间拼接,那么不会产生 SQL 注入漏洞;

2:PreparedStatement 的 SQL 对于数据库来说采用预编译,数据库会将其进行缓存,下一次可以直接拿来使用,省掉了解析 SQL 语句、编译 SQL 的时间;

3:Statement 的 SQL 需要语句与参数变量进行拼接,碰到 VARCHAR 类型的还得加单引号('),如果这些参数有很多,我敢保证用不了几分钟就会整晕掉、拼错,而且这种拼错了都花很长的时间才能找出来。

相对于 PreparedStatement 而言 Statement 没有丝毫优势。由于 PreparedStatement 是 Statement 子接口,因此 Statement 有的功能 PreparedStatement 都有,而 Statement 没有的功能 PreparedStatement 也有。

如上所述,我们还有理由再去使用 Statement 么?

Statement 接口目前最广的应用是用于类型定义,因为它是 PreparedStatement, CallableStatement 的父接口,比如像一些关闭的方法就可以使用 Statement 作为参数,然后给 PreparedStatement, CallableStatement 使用。
zhushoujun 2010-05-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 bao110908 的回复:]
executeUpdate() 这是 PreparedStatement 接口中的方法
executeUpdate(String sql) 这是 PreparedStatement 从父接口 Statement 中继承过来的方法

executeUpdate() 中执行 SQL 语句需要在创建 PerparedStatement 时通过 Connection 的 prepareStateme……
[/Quote]
刚才sorry,说错了,
PreparedStatement既然是从Statement继承过来的,可不可以完全把PreparedStatement他当成Statement来用呢?

zhushoujun 2010-05-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 bao110908 的回复:]
executeUpdate() 这是 PreparedStatement 接口中的方法
executeUpdate(String sql) 这是 PreparedStatement 从父接口 Statement 中继承过来的方法

executeUpdate() 中执行 SQL 语句需要在创建 PerparedStatement 时通过 Connection 的 prepareStateme……
[/Quote]

PreparedStatement既然是从Statement继承过来的,可不可以完全把Statement他当成PreparedStatement来用呢?
deva007 2010-05-29
  • 打赏
  • 举报
回复
3L 很强大
再帮LZ 下个例子便于理解..
Connection conn = null;
PreparedStatement s = null;--(Statement换成了PreparedStatement )
String sql="insert into Users values(?,?,?) "; --(?这个是占位符)
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(Exception e){
e.printStackTrace();
}
conn = DriverManager.getConnection("jdbc:odbc:news","sa","sa");
s = conn.prepareStatement(sql);--(通过conn对象创建prepareStatement对象s)
s.setInt(1,1);--(设置values中的值,1,2,3代表顺序,后面的表示要插入的值)
s.setString(2,"admin");
s.setString(3,"admin");
s.executeUpdate();--(执行executeUpdate())
executeUpdate() 用来执行增,删,改。
如果是查询,则要用executeQuery()
...LZ对比第一个例子的看下
deva007 2010-05-28
  • 打赏
  • 举报
回复
问题一 : 增加,删除,修改 都可以用 executeUpdate()
例,Connection conn = null;
Statement s = null;
String sql="insert into Users values(1,'admin','admin') "; --(根据sql语句的不同,执行不同操作)
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(Exception e){
e.printStackTrace();
}
conn = DriverManager.getConnection("jdbc:odbc:news","sa","sa");
s = conn.createStatement();
s.executeUpdate(sql);


问题二:
1. ResultSet executeQuery(String sql); 执行SQL查询,并返回ResultSet 对象。
2.int executeUpdate(String sql); 可执行增,删,改,返回执行受到影响的行数。
3. boolean execute(String sql); 可执行任何SQL语句,返回一个布尔值,表示是否返回ResultSet 。
在Java下连接SQLite数据库 一、下载SQLite数据库的JDBC:http://www.zentus.com/sqlitejdbc/ 二、将下载到的包解压后得到jar包放到%JAVA_HOME%\lib下,并且将其添加到ClassPath系统环境变量中。一定要保证在类路径ClassPath中有该jar包,并且保证在JAVA库路径JAVA Library Path中有本地库Native Library(\workspace\"Web应用"\WebRoot\WEB-INF\lib\下最好也要加入该jar包)。"SQLite.JDBCDriver"作为JDBC的驱动程序类名。连接JDBC的URL格式为jdbc:sqlite:/path。这里的path为指定到SQLite数据库文件的路径,例如: jdbc:sqlite://dirA/dirB/dbfile jdbc:sqlite://DRIVE:/dirA/dirB/dbfile jdbc:sqlite://COMPUTERNAME/shareA/dirB/dbfile 三、下面是使用SQLite的两段代码以供参考: 代码段1: 1 import java.sql.*; 2 import org.sqlite.JDBC; 3 4 public class SQLiteTest { 5 public static void main(String[] args) { 6 try { 7 // The SQLite (3.3.8) Database File 8 // This database has one table (pmp_countries) with 3 columns (country_id, country_code, country_name) 9 // It has like 237 records of all the countries I could think of. 10 String fileName = "c:/pmp.db"; 11 // Driver to Use 12 // http://www.zentus.com/sqlitejdbc/index.html 13 Class.forName("org.sqlite.JDBC"); 14 // Create Connection Object to SQLite Database 15 // If you want to only create a database in memory, exclude the +fileName 16 Connection conn = DriverManager.getConnection("jdbc:sqlite:"+fileName); 17 // Create a Statement object for the database connection, dunno what this stuff does though. 18 Statement stmt = conn.createStatement(); 19 // Create a result set object for the statement 20 ResultSet rs = stmt.executeQuery("SELECT * FROM pmp_countries ORDER BY country_name ASC"); 21 // Iterate the result set, printing each column 22 // if the column was an int, we could do rs.getInt(column name here) as well, etc. 23 while (rs.next()) { 24 String id = rs.getString("country_id"); // Column 1 25 String code = rs.getString("country_code"); //

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧