Java提供了 Statement、PreparedStatement 和 CallableStatement三种方式来执行查询语句;

PreparedStatement是用于执行参数化查询

预编译statement,提高查询速度,防止sql注入。通过调用connection.preparedStatement(sql)方法可以获得PreparedStatment对象。数据库系统会对sql语句进行预编译处理(如果JDBC驱动支持的话),预处理语句将被预先编译好,这条预编译的sql查询语句能在将来的查询中重用,这样一来,它比Statement对象生成的查询速度更快。预编译:比如java会编译为class文件,然后在编译为计算机能识别的语言,sql语句也一样。

使用范围:当执行相似sql语句的次数比较多(例如用户登陆,对表频繁操作..)语句一样,只是具体的值不一样,被称为动态SQL

优点:语句只编译一次,减少编译次数。提高了安全性(阻止了SQL注入)

缺点: 执行非相似SQL语句时,速度较慢。

原理:相似SQL只编译一次,减少编译次数

Statement 用于通用查询。

使用范围:当执行相似SQL(结构相同,具体值不同)语句的次数比较少

优点:语法简单

缺点:采用硬编码效率低,安全性较差。

原理:硬编码,每次执行时相似SQL都会进行编译

CallableStatement则是用于存储过程。

1 statement 代码实例

public static void main(String[] args) {

        // 数据库连接
Connection connection = null;
//Statement,使用预编译的Statement提高数据库性能
Statement statement = null;
// 结果 集
ResultSet resultSet = null;
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 通过驱动管理类获取数据库链接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/day02?characterEncoding=utf-8",
"root", "");
// 定义sql语句 ?表示占位符
String sql = "select * from user where username = '王五'";
// 获取预处理statement
statement = connection.createStatement();
//新增
statement.executeUpdate("INSERT INTO USER (username ,birthday,sex)VALUES('fyp','2014-01-02',1)",
Statement.RETURN_GENERATED_KEYS);
connection.setAutoCommit(false);
connection.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放资源
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} } }

PreparedStatement

    public static void main(String[] args) {

        // 数据库连接
Connection connection = null;
// 预编译的Statement,使用预编译的Statement提高数据库性能
PreparedStatement preparedStatement = null;
// 结果 集
ResultSet resultSet = null;
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 通过驱动管理类获取数据库链接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/day02?characterEncoding=utf-8",
"root", "");
// 定义sql语句 ?表示占位符
String sql = "select * from user where username = ?";
// 获取预处理statement
connection.createStatement();
preparedStatement = connection.prepareStatement(sql);
// 设置参数,第一个参数为sql语句中参数的序号(从1开始),第二个参数为设置的参数值
preparedStatement.setString(1, "王五");
// 向数据库发出sql执行查询,查询出结果集
resultSet = preparedStatement.executeQuery();
// 遍历查询结果集
while (resultSet.next()) {
System.out.println(resultSet.getString("id") + " " + resultSet.getString("username"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放资源
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
}

Jdbc -Statement的更多相关文章

  1. Error executing DDL via JDBC Statement

    © 版权声明:本文为博主原创文章,转载请注明出处 1.问题描述: 启动hibernate测试案例时报错如下: org.hibernate.tool.schema.spi.CommandAcceptan ...

  2. Error executing DDL via JDBC Statement 导致原因之一:列名使用了sql关键字

    WARN: GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statem ...

  3. 聊聊jdbc statement的fetchSize

    在使用MySQL的JDBC时,如果查询结果集过大,使用一次查询,可能会出现Java.lang.OutOfMemoryError: Java heap space问题,因为DB服务器端一次将查询到的结果 ...

  4. JDBC Statement对象执行批量处理实例

    以下是使用Statement对象的批处理的典型步骤序列 - 使用createStatement()方法创建Statement对象. 使用setAutoCommit()将自动提交设置为false. 使用 ...

  5. java基础 JDBC & Statement & PreparedStatement

    参考文章: http://blog.csdn.net/wang379275614/article/details/23393335 概念 JDBC-数据库连接,是由一些类和接口构成的API,是J2SE ...

  6. JDBC——Statement执行SQL语句的对象

    Statement该对象用于执行静态SQL语句并返回它产生的结果.表示所有的参数在生成SQL的时候都是拼接好的,容易产生SQL注入的问题 PreparedStatement对象是一个预编译的SQL语句 ...

  7. JDBC Statement PrepareStatement

    1.JDBC中Statement接口和PrepareStatement接口关系与区别 Statement接口不能使用占位符?,需要拼sql,所以没有setInt,setString等方法:Prepar ...

  8. JDBC statement的常用方法

    Statement接口: 用于执行静态SQL语句并返回它所生成结果的对象. 三种Statement类: Statement: 由createStatement创建,用于发送简单的SQL语句(最好是不带 ...

  9. Java:Hibernate报错记录:Error executing DDL via JDBC Statement

    想着写一篇hibernate的博文,于是准备从头开始,从官网下了最新的稳定版本来做讲述. 结果利用hibernate自动建表的时候发生下面这个问题. 我很纳闷,之前用低版本一点的没有发生这个问题啊. ...

随机推荐

  1. 数据库 SQL 优化大总结之:百万级数据库优化方案

    网上关于SQL优化的教程很多,但是比较杂乱.近日有空整理了一下,写出来跟大家分享一下,其中有错误和不足的地方,还请大家纠正补充. 这篇文章我花费了大量的时间查找资料.修改.排版,希望大家阅读之后,感觉 ...

  2. Java用Gson遍历json所有节点

    <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</ar ...

  3. 新建web项目myeclipse基本设置

    1. General --> Workspace --> UTF-82. General --> Editors --> Associations --> JSP --& ...

  4. Kattis之旅——Number Sets

    You start with a sequence of consecutive integers. You want to group them into sets. You are given t ...

  5. nGrinder Maven工程使用

    1:新建Maven groovy工程 2:SVN Checkout Maven工程 3:新建JUnit脚本,并运行,提示报错 java.lang.RuntimeException: Please ad ...

  6. oracle闪回、闪回数据归档Flashback Data Archive (Oracle Total Recall)的真正强大之处、11gR2增强以及合理使用

    oracle的闪回很早就出来了,准确的说一直以来应该都较少被真正用户广为使用,除了dba和极少部分开发人员偶尔用于逻辑出错.误删恢复之外,较少被用于产生更有价值的用途. 各种闪回表flashback ...

  7. Git本地分支和远程分支关联

    转载:https://blog.csdn.net/cherishhere/article/details/52606884 转载:https://blog.zengrong.net/post/1746 ...

  8. ODAC(V9.5.15) 学习笔记(四)TMemDataSet (3)

    3.其他 名称 类型 说明 GetBlob TBlob 按照字段名获取当前数据集中某个Blob类型的字段值,并以TBlob对象形式返回 Prepared Boolean 检查Query的SQL是否已准 ...

  9. Restful framework【第十二篇】版本控制

    简单使用 -drf版本控制 -在setting中配置 'DEFAULT_VERSION': 'v1', # 默认版本(从request对象里取不到,显示的默认值) 'ALLOWED_VERSIONS' ...

  10. fedora23安装搜狗輸入法?

    1, 安裝方法, 是通過下載 repo文件, 添加repo文件, 然後通過dnf啦安裝的. repo文件地址是: fedora 的中文社區: https://www.fdzh.org/ fdzh: 就 ...