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 = '王五'";// 获取预处理statementstatement = 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();}}}}

转载于:https://www.cnblogs.com/fanBlog/p/9511058.html

Jdbc -Statement相关推荐

  1. java面试题3 牛客:下面有关jdbc statement的说法错误的是

    下面有关jdbc statement的说法错误的是? A JDBC提供了Statement.PreparedStatement 和 CallableStatement三种方式来执行查询语句, 其中 S ...

  2. 错误信息 Error executing DDL via JDBC Statement 解决办法

    二月 27, 2018 10:34:44 上午 org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl handleExceptio ...

  3. mysql jdbc execute_MySQL JDBC Statement.executeBatch实践问题

    MySQL JDBC Statement.executeBatch实践(执行效率低) 现在很少使用原生jdbc去实现代码, 最近在测试MySQL批处理数据遇到一个问题: 执行Statement.exe ...

  4. java的maxrow_聊聊pg jdbc statement的maxRows参数

    序 本文主要解析一下pg jdbc statement的maxRows参数 Statement.setMaxRows void setMaxRows(int max) throws SQLExcept ...

  5. GenerationTarget encountered exception accepting command :Error executing DDL via JDBC Statement

    GenerationTarget encountered exception accepting command :Error executing DDL "create table ... ...

  6. JDBC Statement PrepareStatement

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

  7. Java JDBC Statement

    使用Connection对象创建Statement对象 Statement createStatement() 创建一条 SQL 语句对象 Statement接口 描述 int executeUpda ...

  8. JDBC Statement RETURN_GENERATED_KEYS返回自动生成的ID

    Java 语句 RETURN_GENERATED_KEYS 指示生成的密钥应可用于检索的常量. 语法 语句中的字段RETURN_GENERATED_KEYS() 声明为: 复制 int RETURN_ ...

  9. 梦回JDBC —— (Statement对象)

    Statement对象 jdbc中的statement 对象用于向数据库发送sql语句,想完成对数据库的增删改查,只需要通过 这个对象发送增删改查的语句就好. statement对象的executeU ...

最新文章

  1. 递归神经网络(Recurrent Neural Networks,RNN)
  2. python使用numpy包编写自定义函数计算MAPE(平均绝对百分比误差)指标mean absolute percentage error (MAPE)、MAPE指标解读、MAPE指标使用的注意事项
  3. echart横轴文字显示省略号_echarts X轴显示不全 有省略
  4. 理解 Linux 的硬链接与软链接
  5. 在C#程序设计中使用Win32 API
  6. 【活动(深圳)】告别2018之12.22 大湾区.NET Meet 大会 ,同时有网络直播
  7. LinkedList专题2
  8. canal mysql从库_canal中间件|数据增量同步解决方案
  9. 全栈深度学习第6期: 模型测试和部署
  10. 时光就是一颗巨大的牛轧糖
  11. 聊聊数据权限哪些事儿
  12. 台式电脑主板插线步骤图_主板电池怎么放电?电脑主板电池放电的方法
  13. 2010年5月18日 小细节大隐患
  14. unlocker解锁虚拟机安装黑苹果出现权限错误问题permission denied
  15. xml测试_规范的XML测试
  16. 虚拟机怎么安装操作系统 虚拟机怎么安装win7系统教程
  17. 第六章第三十题(游戏:双骰子赌博)(Game: craps)
  18. op 圣诞节活动_圣诞节到了–这是我们精选的IT饼干笑话
  19. jzoj1794 保镖排队 (树形dp)
  20. 认识 WebAssembly

热门文章

  1. 自己的父母,能把钱交给他们存吗?
  2. 艺术美的价值是什么?
  3. 企业怎样才能开除一个股东?
  4. 订婚和结婚有什么区别
  5. 张一鸣早期创业的访谈还挺有启发,不知道他现在的观念有没有改变
  6. 别人的趋势不一定是你的未来
  7. 红利,本质上来自于供求关系的不平衡
  8. 个人事业实现突破的关键点
  9. 配电脑时什么配置可以缩水?
  10. 手机老显示存储空间不足该咋办?可删除哪些内容来增加存储空间?