execute、executeQuery和executeUpdate之间的区别 转
转:http://blog.csdn.net/colin_fantasy/article/details/3898070
execute、executeQuery和executeUpdate之间的区别
JDBC中Statement 接口提供了三种执行 SQL 语句的方法:executeQuery、executeUpdate 和 execute。使用哪一个方法由 SQL 语句所产生的内容决定。
1>方法executeQuery
用于产生单个结果集(ResultSet)的语句,例如 SELECT 语句。
被使用最多的执行 SQL 语句的方法。这个方法被用来执行 SELECT 语句,它几乎是使用最多的 SQL
语句。但也只能执行查询语句,执行后返回代表查询结果的ResultSet对象。
如:
//加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//使用DriverManager获取数据库连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
"root","1234"); //使用Connection来创建一个Statment对象
Statement stmt = conn.createStatement(); //执行查询语句
ResultSet rs =stmt.executeQuery("select * from teacher"); //把查询结果输出来
while (rs.next()) { System.out.println(rs.getInt(1) + "/t" + rs.getString(2));
}
2>方法executeUpdate
用于执行 INSERT、UPDATE 或 DELETE 语句以及 SQL DDL(数据定义语言)语句,例如 CREATE TABLE 和 DROP TABLE。INSERT、UPDATE 或 DELETE 语句的效果是修改表中零行或多行中的一列或多列。executeUpdate 的返回值是一个整数(int),指示受影响的行数(即更新计数)。对于 CREATE TABLE 或 DROP TABLE 等不操作行的语句,executeUpdate 的返回值总为零。
如:
//加载数据库驱动
Class.forName("com.mysql.jdbc.Driver"); //使用DriverManager获取数据库连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
"root","1234"); //使用Connection来创建一个Statment对象
Statement stmt = conn.createStatement();
//执行DML语句,返回受影响的记录条数
return stmt.executeUpdate(sql);
3>方法execute:
可用于执行任何SQL语句,返回一个boolean值,表明执行该SQL语句是否返回了ResultSet。如果执行后第一个结果是ResultSet,
则返回true,否则返回false。但它执行SQL语句时比较麻烦,通常我们没有必要使用execute方法来执行SQL语句,而是使用
executeQuery或executeUpdate更适合,但如果在不清楚SQL语句的类型时则只能使用execute方法来执行该SQL语句了。
如:
//加载驱动
Class.forName(driver);
//获取数据库连接
conn = DriverManager.getConnection(url , user , pass);
//使用Connection来创建一个Statment对象
stmt = conn.createStatement();
//执行SQL,返回boolean值表示是否包含ResultSet
boolean hasResultSet = stmt.execute(sql);
//如果执行后有ResultSet结果集
if (hasResultSet) { //获取结果集
rs = stmt.getResultSet(); //ResultSetMetaData是用于分析结果集的元数据接口
ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount();
//迭代输出ResultSet对象
while (rs.next()) {//依次输出每列的值
for (int i = 0 ; i < columnCount ; i++ )
{
System.out.print(rs.getString(i + 1) + "/t");
}
System.out.print("/n");
}
} else
{ System.out.println("该SQL语句影响的记录有" + stmt.getUpdateCount() + "条");
}
execute、executeQuery和executeUpdate之间的区别 转的更多相关文章
- mysql 中execute、executeQuery和executeUpdate之间的区别
在用纯JSP做一个页面报警功能的时候习惯性的用executeQuery来执行SQL语句,结果执行update时就遇到问题,语句能执行,但返回结果出现问题,另外还忽略了executeUpdate的返回值 ...
- execute、executeQuery和executeUpdate之间的区别
JDBCTM中Statement接口提供的execute.executeQuery和executeUpdate之间的区别 Statement 接口提供了三种执行 SQL 语句的方法:executeQu ...
- java execute、executeQuery和executeUpdate之间的区别
在用纯JSP做一个页面报警功能的时候习惯性的用executeQuery来执行SQL语句,结果执行update时就遇到问题,语句能执行,但返回结果出现问题,另外还忽略了executeUpdate的返回值 ...
- JDBC中Statement接口提供的execute、executeQuery和executeUpdate之间的区别
Statement 接口提供了三种执行 SQL 语句的方法:executeQuery.executeUpdate 和 execute.使用哪一个方法由 SQL 语句所产生的内容决定. 方法execut ...
- JDBCTM中Statement接口提供的execute、executeQuery和executeUpdate之间的区别
Statement 接口提供了三种执行 SQL 语句的方法:executeQuery.executeUpdate 和 execute.使用哪一个方法由 SQL 语句所产生的内容决定. 方法execut ...
- sql语句executeQuery和executeUpdate之间的区别
方法一.executeQuery 用于产生单个结果集(ResultSet)的语句,例如 SELECT 语句. 被使用最多的执行 SQL 语句的方法.这个方法被用来执行 SELECT 语句,它几乎是使用 ...
- execute,executeQuery和executeUpdate的区别
在jdbc中有3种执行sql的语句分别是execute,executeQuery和executeUpdate execute执行增删改查操作 execute返回的结果是个boolean型,当返回的是t ...
- JDBC中execute、executeQuery和executeUpdate的区别
Statement 接口提供了三种执行 SQL 语句的方法:executeQuery.executeUpdate 和 execute.使用哪一个方法由 SQL 语句所产生的内容决定. 1>方法e ...
- executeQuery、executeUpdate 和 execute
Statement 接口提供了三种执行 SQL 语句的方法:executeQuery.executeUpdate 和 execute.使用哪一个方法由 SQL 语句所产生的内容决定. 1. Resul ...
随机推荐
- Temporal-Difference Learning for Prediction
In Monte Carlo Learning, we've got the estimation of value function: Gt is the episode return from t ...
- [2019杭电多校第二场][hdu6599]I Love Palindrome String(回文自动机&&hash)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6599 题目大意为求字符串S有多少个子串S[l,r]满足回文串的定义,并且S[l,(l+r)/2]也满足 ...
- VS2010中解决Qt“Unable to find a Qt build“
转自:http://blog.sina.com.cn/s/blog_687960370101d0eu.html 三种方法: 1.在QT菜单下单击OPTION,然后单击ADD,选择QT安装路径. 2.运 ...
- Django forms组件的校验
引入: from django import forms 使用方法:定义规则,例: class UserForm(forms.Form): name=forms.CharField(max_lengt ...
- 2019牛客暑期多校训练营(第七场) - C - Governing sand - 平衡树
5 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 感觉该出14才对,取前k小写成了取前k大. 5 1 5 4 2 5 3 3 5 2 4 5 1 6 5 5 suf=55 res=0 a ...
- Cocos Creator-TypeScript与JS快速过渡
目前的H5游戏开发引擎,国内主流的是Cocos Creator.Laya Box.Egret, 这3种引擎又有各自的开发语言,JavaScript.TypeScript.AS3 . 不同的公司采用的引 ...
- neo4j 的cql 语句,增、删、改、查(条件查询)(持续更新)
前言 因为做一个比赛的项目 ,需要用到 neo4j 数据库,所以要学习其语言cql,特来整理一下他的基本语言. 整片的语句是按照 了 Neo4j 数据库自带的示例 Movie Graph 来写的. 直 ...
- 如何在springboot中读取自己创建的.properties配置的值
在实体类里面加上 @PropertySource("classpath:/robot_config.properties") robot_config.properties // ...
- Spring基础学习笔记
1. Spring入门 1. 1 Spring的体系结构 1.2 HelloWorld 1.2.1 Maven的使用 1)maven 的javase工程目录结构: 2)maven的settings.x ...
- css设置不允许复制文本内容
之前做一个网上答题的页面时,考虑到要防止考生利用复制粘贴来提高作弊的可能性,就设计了不允许复制.方法也很简单,通过设置CSS 的 user-select就可以达到目的: -moz-user-selec ...