1、创建 Statement 对象 建立了到特定数据库的连接之后,就可用该连接发送 SQL 语句。Statement 对象用 Connection 的方法 createStatement 创建,如下列代码段中所示: Connection con = DriverManager.getConnection(url, "sunny", ""); Statement stmt = con.createStatement(); 为了执行 Statement 对象,被发送到数据库的 SQL 语句将被作为参数提供给 Statement 的方法: ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table2"); 2、使用 Statement 对象执行语句 Statement 接口提供了三种执行 SQL 语句的方法:executeQuery、exe 通过工具来创建SQL statement
cuteUpdate 和 execute。使用哪一个方法由 SQL 语句所产生的内容决定。 方法 executeQuery 用于产生单个结果集的语句,例如 SELECT 语句。 方法 executeUpdate 用于执行 INSERT、UPDATE 或 DELETE 语句以及 SQL DDL(数据定义语言)语句,例如 CREATE TABLE 和 DROP TABLE。INSERT、UPDATE 或 DELETE 语句的效果是修改表中零行或多行中的一列或多列。executeUpdate 的返回值是一个整数,指示受影响的行数(即更新计数)。对于 CREATE TABLE 或 DROP TABLE 等不操作行的语句,executeUpdate 的返回值总为零。 方法 execute 用于执行返回多个结果集、多个更新计数或二者组合的语句。因为多数程序员不会需要该高级功能,所以本概述后面将在单独一节中对其进行介绍。 执行语句的所有方法都将关闭所调用的 Statement 对象的当前打开结果集(如果存在)。这意味着在重新执行 Statement 对象之前,需要完成对当前 ResultSet 对象的处理。 应注意,继承了 Statement 接口中所有方法的 PreparedStatement 接口都有自己的 executeQuery、executeUpdate 和 execute 方法。Statement 对象本身不包含 SQL 语句,因而必须给 Statement.execute 方法提供 SQL 语句作为参数。PreparedStatement 对象并不将 SQL 语句作为参数提供给这些方法,因为它们已经包含预编译 SQL 语句。CallableStatement 对象继承这些方法的 PreparedStatement 形式。对于这些方法的 PreparedStatement 或 CallableStatement 版本,使用查询参数将抛出 SQLException。 3、语句完成 当连接处于自动提交模式时,其中所执行的语句在完成时将自动提交或还原。语句在已执行且所有结果返回时,即认为已完成。对于返回一个结果集的 executeQuery 方法,在检索完 ResultSet 对象的所有行时该语句完成。对于方法 executeUpdate,当它执行时语句即完成。但在少数调用方法 execute 的情况中,在检索所有结果集或它生成的更新计数之后语句才完成。
编辑本段关闭 Statement 对象
Statement 对象将由 Java 垃圾收集程序自动关闭。而作为一种好的编程风格,应在不需要 Statement 对象时显式地关闭它们。这将立即释放 DBMS 资源,有助于避免潜在的内存问题。
分享到:
相关推荐
hsbc statement template
Statement Tracer for Oracle绿色
prepareStatement和Statement的区别
statement-tracer-for-oracle 跟踪器
project scope statement template
解决:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after statement closed.
爱思唯尔旗下的Credit Author Statement模板 论文已正式出版~ 相关博客:https://blog.csdn.net/qq_39763246/article/details/126797385
Statement和PreparedStatement之间的区别
simple templet of income statement in english
项目范围说明书(Project Scope Statement)适用于各个行业的范围说明书,中英文对照标题
Vuejs报错error: Unexpected console statement (no-console) at src\... 解决办法
Statement Tracer for ADO 绿色版,捕捉ADO SQL代码.
jdbc中statement和prepared区别,jdbc中statement和prepared区别
Generate code statement for hardcode
Do you know how to prepare a cashflow statement?
Financial Statement Analysis of SICHUAN CHANGHONG ELECTRIC CO., LTD 【Abstract】 A financial tsunami which had never been so violent over the past 100 years is confusing the whole world in 2008....
java数据库连接PrepareStatement
完整的英文版重力式方块码头施工方案Method Statement for Quay Wall Construction, Construction Preparation→ General Reclamation & Backfill of Platform→ Excavation and Slope Trimming→ Placing of ...
java中PreparedStatement和Statement的区别