Jdbc -Statement
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();}}}}
2 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相关推荐
- java面试题3 牛客:下面有关jdbc statement的说法错误的是
下面有关jdbc statement的说法错误的是? A JDBC提供了Statement.PreparedStatement 和 CallableStatement三种方式来执行查询语句, 其中 S ...
- 错误信息 Error executing DDL via JDBC Statement 解决办法
二月 27, 2018 10:34:44 上午 org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl handleExceptio ...
- mysql jdbc execute_MySQL JDBC Statement.executeBatch实践问题
MySQL JDBC Statement.executeBatch实践(执行效率低) 现在很少使用原生jdbc去实现代码, 最近在测试MySQL批处理数据遇到一个问题: 执行Statement.exe ...
- java的maxrow_聊聊pg jdbc statement的maxRows参数
序 本文主要解析一下pg jdbc statement的maxRows参数 Statement.setMaxRows void setMaxRows(int max) throws SQLExcept ...
- GenerationTarget encountered exception accepting command :Error executing DDL via JDBC Statement
GenerationTarget encountered exception accepting command :Error executing DDL "create table ... ...
- JDBC Statement PrepareStatement
1.JDBC中Statement接口和PrepareStatement接口关系与区别 Statement接口不能使用占位符?,需要拼sql,所以没有setInt,setString等方法: Prepa ...
- Java JDBC Statement
使用Connection对象创建Statement对象 Statement createStatement() 创建一条 SQL 语句对象 Statement接口 描述 int executeUpda ...
- JDBC Statement RETURN_GENERATED_KEYS返回自动生成的ID
Java 语句 RETURN_GENERATED_KEYS 指示生成的密钥应可用于检索的常量. 语法 语句中的字段RETURN_GENERATED_KEYS() 声明为: 复制 int RETURN_ ...
- 梦回JDBC —— (Statement对象)
Statement对象 jdbc中的statement 对象用于向数据库发送sql语句,想完成对数据库的增删改查,只需要通过 这个对象发送增删改查的语句就好. statement对象的executeU ...
最新文章
- 递归神经网络(Recurrent Neural Networks,RNN)
- python使用numpy包编写自定义函数计算MAPE(平均绝对百分比误差)指标mean absolute percentage error (MAPE)、MAPE指标解读、MAPE指标使用的注意事项
- echart横轴文字显示省略号_echarts X轴显示不全 有省略
- 理解 Linux 的硬链接与软链接
- 在C#程序设计中使用Win32 API
- 【活动(深圳)】告别2018之12.22 大湾区.NET Meet 大会 ,同时有网络直播
- LinkedList专题2
- canal mysql从库_canal中间件|数据增量同步解决方案
- 全栈深度学习第6期: 模型测试和部署
- 时光就是一颗巨大的牛轧糖
- 聊聊数据权限哪些事儿
- 台式电脑主板插线步骤图_主板电池怎么放电?电脑主板电池放电的方法
- 2010年5月18日 小细节大隐患
- unlocker解锁虚拟机安装黑苹果出现权限错误问题permission denied
- xml测试_规范的XML测试
- 虚拟机怎么安装操作系统 虚拟机怎么安装win7系统教程
- 第六章第三十题(游戏:双骰子赌博)(Game: craps)
- op 圣诞节活动_圣诞节到了–这是我们精选的IT饼干笑话
- jzoj1794 保镖排队 (树形dp)
- 认识 WebAssembly