PreparedStatement 和 Statement 的区别(推荐使用PreparedStatement)
PreparedStatement与Statement在使用时的区别:
1.Statement:
String sql=" ";
executeUpdate(sql)
2.
PreparedStatement:
String sql(可能存在占位符?)
在创建PreparedStatement 对象时,将sql预编译 prepareStatement(sql)
executeUpdate()
setXxx()替换占位符?
推荐使用PreparedStatement:原因如下:
1. 编码更加简便(避免了字符串的拼接)
String name = "zs" ;
int age = 23 ; stmt:
String sql =" insert into student(stuno,stuname) values('"+name+"', "+age+" ) " ;
stmt.executeUpdate(sql); pstmt:
String sql =" insert into student(stuno,stuname) values(?,?) " ;
pstmt = connection.prepareStatement(sql);//预编译SQL
pstmt.setString(1,name);
pstmt.setInt(2,age);
2. 提高性能(因为 有预编译操作,预编译只需要执行一次)
需要重复增加100条数
stmt://编译100次,执行100次
String sql =" insert into student(stuno,stuname) values('"+name+"', "+age+" ) " ;
for(100)
stmt.executeUpdate(sql); pstmt://编译1次,执行100次
String sql =" insert into student(stuno,stuname) values(?,?) " ;
pstmt = connection.prepareStatement(sql);//预编译SQL
pstmt.setString(1,name);
pstmt.setInt(2,age);
for( 100){
pstmt.executeUpdate();
}
3.安全(可以有效防止 sql 注入)
sql 注入: 将客户输入的内容 和 开发人员的SQL语句 混为一体
stmt:存在被sql注入的风险
(例如输入 用户名:任意值 ' or 1=1 --
密码:任意值)
分析:
select count(*) from login where uname='任意值 ' or 1=1 --' and upwd ='任意值' ; select count(*) from login where uname='任意值 ' or 1=1 ; select count(*) from login ; select count(*) from login where uname='"+name+"' and upwd ='"+pwd+"' pstmt:有效防止sql注入
推荐使用PreparedStatement
代码对比
package jdbcproject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; public class JDBCStatementDemo {
private static final String URL = "jdbc:mysql://localhost:3306/mydatabase?serverTimezone=GMT%2B8";
private static final String USERNAME = "root";
private static final String PWD = "password"; public static void update() throws ClassNotFoundException, SQLException {// 增删改
// a. 导入驱动,加载具体的驱动类
Class.forName("com.mysql.cj.jdbc.Driver");
// b.与数据库建立连接
Connection connection = DriverManager.getConnection(URL, USERNAME, PWD);
// c.发送sql,执行增删改查
Statement stmt = connection.createStatement();
//增加 String sql = "insert into student values(2,'李四',21)";
//修改 String sql = "update student set name='张三' where id=1";
//删除
String sql = "delete from student where id=1";
int count = stmt.executeUpdate(sql);
if (count > 0) {
System.out.println("操作成功!");
}
stmt.close();
connection.close(); }
public static void query() throws ClassNotFoundException, SQLException {// 增删改
// a. 导入驱动,加载具体的驱动类
Class.forName("com.mysql.cj.jdbc.Driver");
// b.与数据库建立连接
Connection connection = DriverManager.getConnection(URL, USERNAME, PWD);
// c.发送sql,执行增删改[查]
Statement stmt = connection.createStatement();
char stuname='a';
//模糊查询
String sql = "select id,name,age from student where name like '%"+stuname+"%'";
ResultSet rs=stmt.executeQuery(sql);
//int count = stmt.executeUpdate(sql);
while (rs.next()) {
int id=rs.getInt("id");
String name=rs.getString("name");
int age=rs.getInt("age");
System.out.println(id+"--"+name+"--"+age); }
rs.close();
stmt.close();
connection.close(); }
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// update();
query();
}
}
JDBCStatementDemo
package jdbcproject; import java.lang.invoke.StringConcatFactory;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; public class JDBCPreparedStatementDemo {
private static final String URL = "jdbc:mysql://localhost:3306/mydatabase?serverTimezone=GMT%2B8";
private static final String USERNAME = "root";
private static final String PWD = "password"; public static void update() throws ClassNotFoundException, SQLException {// 增删改
// a. 导入驱动,加载具体的驱动类
Class.forName("com.mysql.cj.jdbc.Driver");
// b.与数据库建立连接
Connection connection = DriverManager.getConnection(URL, USERNAME, PWD);
// c.发送sql,执行增删改查
//sql提前写
String sql="insert into student values(?,?,?)";
PreparedStatement pstmt = connection.prepareStatement(sql);//预编译
pstmt.setInt(1, 5);
pstmt.setString(2, "超凡");
pstmt.setInt(3, 21); //增加 String sql = "insert into student values(2,'李四',21)";
//修改 String sql = "update student set name='张三' where id=1";
//删除 String sql = "delete from student where id=1";
//这里括号不用再写sql
int count = pstmt.executeUpdate();
if (count > 0) {
System.out.println("操作成功!");
}
//后开先关,先开的后关,和栈类似
pstmt.close();
connection.close(); }
public static void query() throws ClassNotFoundException, SQLException {// 增删改
// a. 导入驱动,加载具体的驱动类
Class.forName("com.mysql.cj.jdbc.Driver");
// b.与数据库建立连接
Connection connection = DriverManager.getConnection(URL, USERNAME, PWD);
// c.发送sql,执行增删改[查]
//sql提前写
char stuname='a';
String sql = "select * from student where name like ?";
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setString(1, "%g%");
//模糊查询 ResultSet rs=pstmt.executeQuery();
//int count = stmt.executeUpdate(sql);
while (rs.next()) {
int id=rs.getInt("id");
String name=rs.getString("name");
int age=rs.getInt("age");
System.out.println(id+"--"+name+"--"+age); }
//后开先关,先开的后关,和栈类似
rs.close();
pstmt.close();
connection.close(); }
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// update();
query();
}
}
JDBCPreparedStatementDemo
PreparedStatement 和 Statement 的区别(推荐使用PreparedStatement)的更多相关文章
- PreparedStatement与Statement的区别
PreparedStatement与statement的区别 1.PreparedStatement是预编译的,对于批量处理可以大大提高效率. 也叫JDBC存储过程 2.使用 Statement 对象 ...
- JDBC增删改查,PreparedStatement和Statement的区别
此篇是在上一篇的基础上使用PreparedStatement对象来实现JDBC增删改查的 具体工具类JDBCTools和实现类和配置文件在上一篇Statement对象实现的时候有写. 上一篇地址htt ...
- PreparedStatement和Statement的区别
转自:http://blog.sina.com.cn/s/blog_77eba18f01019csh.html 1. PreparedStatement接口继承Statement, PreparedS ...
- JDBC中PreparedStatement和Statement的区别
共同点: PreparedStatement和Statement都是用来执行SQL查询语句的API之一. 不同点: 在PreparedStatement中,当我们经常需要反复执行一条结构相似的sql语 ...
- PreparedStatement 与 Statement 的区别
1. PreparedStatement 接口继承 Statement, PreparedStatement 实例包含已编译的 SQL 语句,所以其执行速度要快于 Statement 对象. 2.作为 ...
- java PreparedStatement和statement的区别
1. PreparedStatement接口继承Statement, PreparedStatement 实例包含已编译的 SQL 语句,所以其执行速度要快于 Statement 对象.2.作为 St ...
- JDBC 4 PreparedStatement 与Statement 的区别
1 有安全性 PreparedStatement 可以由于不是使用拼接,防止了sql注入,提高了安全性. 2 更方便 PreparedStatement 可以自动对类型进行转换,代码可读性,可维护 ...
- PreparedStatement与Statement区别
就这牛客网的一道题,进行分析PreparedStatement与Statement的区别. 题目: 关于PreparedStatement与Statement描述错误的是() A 一般而言,Prepa ...
- java.sql.preparedstatement和java.sql.statement的区别
本文转自CSDN,然后整理了一遍.原文出处:CSDN JDBC(java database connectivity,java数据库连接)的api中的主要的四个类之一的java.sql.stateme ...
随机推荐
- T-SQL常用的函数
http://blog.sina.com.cn/s/blog_4af01cd50100hsac.html
- HashMap 原理?jdk1.7 与 1.8区别
HashMap 结构 以及 1.7 与 1.8 一.区别 01. jdk 1.7 用的是头插法,而jdk1.8以后使用的是尾插法?为什么这样做呢?因为 JDK 1.7 是用单链表进行纵向延伸,采用头插 ...
- 【原】php中fastcgi和php-fpm是什么东西
fastcgi 是一个与平台无关,与语言无关,任何语言只要按照它的接口来实现,就能实现自己语言的fastcgi能力和web server 通讯. PHP-CGI就是PHP实现的自带的FastCGI管理 ...
- Python数据类型-8 集合set
集合set set集合是一个无序不重复元素的集,基本功能包括关系测试和消除重复元素.集合使用大括号({})框定元素,并以逗号进行分隔.但是注意:如果要创建一个空集合,必须用 set() 而不是 {} ...
- tensorflow文本分类实战——卷积神经网络CNN
首先说明使用的工具和环境:python3.6.8 tensorflow1.14.0 centos7.0(最好用Ubuntu) 关于环境的搭建只做简单说明,我这边是使用pip搭建了python的 ...
- MIT宣布人工智能独立设系!
导读 MIT宣布人工智能独立设系!AI与电子工程.计算机科学系将三分天下? MIT 电子工程和计算机科学系(EECS)拆分啦.拆分后分为 3 个学科群(faculty),或者说 3 个系:电子工程(E ...
- Linux命令:ping命令
ping命令:类似于windows的ping命令,用于测试网络主机ICMP请求回应的 ping选项 ping -c # # 执行次数 -w # #测试 ...
- Spark 写 Hive table 非常慢【解决】
代码如下: dataFrame.createOrReplaceTempView("view_page_utm") val sql = s""" |in ...
- Windows XP 常用DOS命令
winver 检查Windows版本 wmimgmt.msc 打开windows管理体系结构(WMI) wupdmgr windows更新程序 wscript windows脚本宿主设置 writ ...
- 复习jquery菜鸟教程
https://www.runoob.com/jquery/jquery-plugin-treeview.html