描述

当我们需要在Java程序中与数据库进行交互,可能首先想到的是使用某个ORM框架,因为ORM框架封装了一些实现细节,在使用上非常方便,并且一定程度上可以提升代码稳定性。

在ORM框架中,都会依赖MySQL Connector包,因为真正与数据库进行交互的是在MySQL Connector包里面实现。

5.x版本maven依赖:

mysql

mysql-connector-java

5.1.47

三种方式

MySQL Connector执行SQL语句主要有executeQuery,executeUpdate,execute等三种方式。

executeQuery

此方法执行Selec查询语句,通过ResultSet返回结果集。

private static void executeQuery() throws Exception{

Connection connection = null;

Statement statement = null;

ResultSet rs = null;

try {

connection = DriverManager.getConnection(url);

statement = connection.createStatement();

statement.setFetchSize(Integer.MIN_VALUE);

String sql = "select * from user";

rs = statement.executeQuery(sql);

int count=0;

while (rs.next()){

count++;

}

System.out.println("executeQuery count: " + count);

} catch (Exception e){

e.printStackTrace();

} finally {

close(connection,statement,rs);

}

}

executeUpdate

此方法执行Insert,Update,Delete语句,返回变更影响的行数。

private static void executeUpdate() throws Exception{

Connection connection = null;

Statement statement = null;

int updateCount = 0;

try {

connection = DriverManager.getConnection(url);

statement = connection.createStatement();

String sql = "update user set name='啊啊啊' where id = " + new Random().nextInt(999999);

updateCount = statement.executeUpdate(sql);

System.out.println("executeUpdate count: " + updateCount);

} catch (Exception e){

e.printStackTrace();

} finally {

close(connection,statement,null);

}

}

execute

当我们不知道来源SQL是Select查询还是Insert/Update/Delete更新时,可以统一使用excute()方法来执行SQL语句,此方法返回一个boolean值,如果返回true,表示执行的SQL语句为Select查询语句,此时可以通过Statement#getResultSet()方法来获取结果集;如果返回false,表示执行的时Insert/Update/Delete语句,此时可以通过Statement#getUpdateCount()来返回此次SQL执行对数据库影响的行数。

private static void executeForSelect() throws Exception{

Connection connection = null;

Statement statement = null;

ResultSet rs = null;

try {

connection = DriverManager.getConnection(url);

statement = connection.createStatement();

statement.setFetchSize(Integer.MIN_VALUE);

String sql = "select * from user";

if (statement.execute(sql)){

rs = statement.getResultSet();

}

int count=0;

while (rs.next()){

count++;

}

System.out.println("executeForSelect count: " + count);

} catch (Exception e){

e.printStackTrace();

} finally {

close(connection,statement,rs);

}

}

private static void executeForUpdate() throws Exception{

Connection connection = null;

Statement statement = null;

int updateCount = 0;

try {

connection = DriverManager.getConnection(url);

statement = connection.createStatement();

String sql = "update user set name='啊啊啊' where id = " + new Random().nextInt(999999);

if (!statement.execute(sql)){

updateCount = statement.getUpdateCount();

}

System.out.println("executeForUpdate updateCount: " + updateCount);

} catch (Exception e){

e.printStackTrace();

} finally {

close(connection,statement,null);

}

}

验证

执行测试:

public static void main(String[] args) throws Exception {

Long start = System.currentTimeMillis();

executeForSelect();

System.out.println("executeForSelect 耗时: " + (System.currentTimeMillis() - start) + " ms \n");

start = System.currentTimeMillis();

executeForUpdate();

System.out.println("executeForUpdate 耗时: " + (System.currentTimeMillis() - start) + " ms \n");

start = System.currentTimeMillis();

executeQuery();

System.out.println("executeQuery 耗时: " + (System.currentTimeMillis() - start) + " ms \n");

start = System.currentTimeMillis();

executeUpdate();

System.out.println("executeUpdate 耗时: " + (System.currentTimeMillis() - start) + " ms \n");

}

private static void close(Connection connection, Statement statement, ResultSet rs) throws Exception{

if (rs != null){

rs.close();

}

if (statement != null){

statement.close();

}

if (connection != null){

connection.close();

}

}

返回结果:

executeForSelect count: 4717924

executeForSelect 耗时: 4791 ms

executeForUpdate updateCount: 1

executeForUpdate 耗时: 6 ms

executeQuery count: 4717924

executeQuery 耗时: 4340 ms

executeUpdate count: 1

executeUpdate 耗时: 8 ms

perl mysql dml_MySQL Connector执行SQL语句的三种方式相关推荐

  1. pdo_fetch执行mysql_PDO中执行SQL语句的三种方法

    在PDO中,我们可以使用三种方式来执行SQL语句,分别是 exec()方法,query方法,以及预处理语句prepare()和execute()方法~大理石构件来图加工 在上一篇文章<使用PDO ...

  2. SAP HANA里执行SQL语句的两种方式

    要获取更多Jerry的原创文章,请关注公众号"汪子熙":

  3. mysql创建函数执行sql语句_MySQL mysql_query 函数执行SQL语句

    mysql_query()函数 PHP MySQL 函数库中,mysql_query() 函数用于向 MySQL 发送并执行 SQL 语句. 对于没有数据返回结果集的 SQL ,如 UPDATE.DE ...

  4. 执行 Python 程序的三种方式及Python 的 IDE —— `PyCharm`

    执行 Python 程序的三种方式 3.1. 解释器 python / python3 Python 的解释器 # 使用 python 2.x 解释器 $ python xxx.py# 使用 pyth ...

  5. Spring容器初始化完成后执行业务逻辑的三种方式

    一  业务背景 监听应用容器启动完毕并扫描容器类特定的Dubbo服务,并把相关元数据注册到网关. 二 思路 1  在容器启动构造元数据上报到网关,影响应用启动性能: 2  监听容器启动完毕后构造元数据 ...

  6. Winform中使用Mysql.Data.dll实现连接Mysql数据库并执行sql语句(排除ddl等非法语句的执行)

    场景 Winform中连接Mysql8并查询表中数据进行显示: Winform中连接Mysql8并查询表中数据进行显示_BADAO_LIUMANG_QIZHI的博客-CSDN博客 与上面实现的流程类似 ...

  7. IDEA连接MySQL数据库并执行SQL语句使用数据

    文章目录 一.IDEA连接MySQL数据库 (一)首先新建普通Java项目 (二)连接数据库 1.点击右侧DataBase 2.点击加号,找到MySQL,添加数据库 3.输入用户名和密码,点击**Te ...

  8. Python 执行Python程序的三种方式

    解释器 python / python3 python 的解释器 # 使用python 2.x 解释器 python xxx.py# 使用python 3.x 解释器 python3 xxx.py P ...

  9. mysql在哪执行sql语句_mysql从命令行执行sql语句

    Codeforce Round #218 Div2 A:没个元素的个数少的变成多的和就是了 B:居然被systemtest搓掉了- -分东西,我改的代码,还是shit一样的过的...别人的直接两个操作 ...

最新文章

  1. 大数据挖掘会让我们避免下一场瘟疫么?
  2. [react] 说说你对reader的context的理解
  3. 分布式ELK日志采集系统
  4. Microduino中LM75温度传感器的使用
  5. 中文情感分析——snownlp类库 源码注释及使用
  6. openstack初探
  7. 将运行时地理数据库(*.geodatabase)复制到文件地理数据库
  8. koa配合axios做接口
  9. 高负载高并发网站架构分析
  10. 【笔记】国际货运代理实务-李娟-河南经贸
  11. 家用计算机常见故障及解决方式,电脑常见故障及处理方法汇总
  12. 在ext4文件系统上恢复被误删除的文件
  13. 松下服务器维修论坛,[分享] 松下空调电脑板维修手记
  14. 图像各向异性平滑滤波
  15. 正则表达式-re.error: unbalanced parenthesis at position 7
  16. dhcp failover linux,Centos7 安装 DHCP 4.1 服务器配置及热备
  17. 第十八届深圳文博会今日开幕,江苏馆携手卓易紫砂街数字文化惊艳亮相!
  18. vue封装自定义数字键盘组件
  19. php发布编辑删除功能,php实现添加修改删除
  20. 技术团队常见的管理困惑与误区

热门文章

  1. LeetCode 654. 最大二叉树(递归)
  2. 法斗几个月长鼻筋_路医生说丨脚底板早起一下地特别疼?得了足底筋膜炎,该怎么办?...
  3. 为了养成NLP卷王,我画了一张路线图
  4. 掌握神经网络,我应该学习哪些至关重要的知识点?
  5. 开源:Swagger Butler 1.1.0发布,利用ZuulRoute信息简化配置内容
  6. 论文浅尝 - CIKM2020 | 用于推荐系统的多模态知识图谱
  7. 论文浅尝 | 虚拟知识图谱:软件系统和应用案例综述
  8. 技术动态 | 藏经阁计划发布一年,阿里知识引擎有哪些技术突破?
  9. 超详细中文注释的GPT2新闻标题生成项目
  10. 商汤科技-数据运维工程师-提前批笔试题目汇总