JDBC 的 PreparedStatement 与 Statement
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date; public class StatmentExample { public static void main(String[] args) throws Exception {
mysqlConnection3();
} // mysql 获取自增id的值的方法
public static void mysqlConnection1() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/test?useUnicode=true&&characterEncoding=UTF-8&autoReconnect=true";
String user = "root";
String password = "123456";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null; try {
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
stmt.executeUpdate("insert into dept (deptname) values ('市场部')", Statement.RETURN_GENERATED_KEYS);
rs = stmt.getGeneratedKeys();// mysql 获取自增id的值的方法
if (rs.next()) {
System.out.println(rs.getInt(1));
}
} catch (Exception e) {
throw e;
} finally {
rs.close();
stmt.close();
conn.close();
}
}
// 建议始终以 PreparedStatement 代替 Statement
// 1.虽然代码多出几行,但可读性和可维护性得到提升
// 2.防止SQL注入提高安全性,占位符中的内容都会被转义,['w' or '1' = '1']会被转义成[\'\\'w\\' or \\'1\\' = \\'1\\'\']
// 3.虽然预编译要耗费时间,但sql编译后的执行代码被缓存下来,下次调用时就不需要编译,从而提升性能
public static void mysqlConnection2() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/test?useUnicode=true&&characterEncoding=UTF-8&autoReconnect=true";
String user = "root";
String password = "123456";
Connection conn = null;
PreparedStatement perstmt2 = null;
ResultSet rs2 = null; try {
conn = DriverManager.getConnection(url, user, password);
String sql2 = "select deptno,deptname from dept where deptno = ? ";// dept这张表有deptno,deptname字段
perstmt2 = conn.prepareStatement(sql2);
perstmt2.setInt(1,11);
rs2 = perstmt2.executeQuery();
while (rs2.next()) {
System.out.print(rs2.getInt("deptno") + " ");
System.out.println(rs2.getString("deptname"));
}
} catch (Exception e) {
throw e;
} finally {
// 不要只关闭conn,因为数据库那边的资源确实释放了,但是java这边的操作系统中的连接资源不会即时释放
rs2.close();
perstmt2.close();
conn.close();
}
}
// 使用PreparedStatement的AddBatch()方法一次性发送多个sql给数据库
public static void mysqlConnection3() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/test?useUnicode=true&&characterEncoding=UTF-8&autoReconnect=true";
String user = "root";
String password = "123456";
Connection conn = null;
PreparedStatement perstmt2 = null; try {
conn = DriverManager.getConnection(url, user, password);
System.out.println((new Date()).getTime());
perstmt2 = conn.prepareStatement("insert into dept (deptname) values (?)");
for (int n = 0; n < 1000; n++) {
perstmt2.setString(1, "信息部" + n);
perstmt2.addBatch();
}
perstmt2.executeBatch();
System.out.println((new Date()).getTime());
} catch (Exception e) {
throw e;
} finally {
perstmt2.close();
conn.close();
}
}
}
JDBC 的 PreparedStatement 与 Statement的更多相关文章
- JDBC 中preparedStatement和Statement区别
一.概念 PreparedStatement是用来执行SQL查询语句的API之一,Java提供了 Statement.PreparedStatement 和 CallableStatement三种方式 ...
- JDBC中PreparedStatement和Statement的区别
共同点: PreparedStatement和Statement都是用来执行SQL查询语句的API之一. 不同点: 在PreparedStatement中,当我们经常需要反复执行一条结构相似的sql语 ...
- JDBC中PreparedStatement相比Statement的好处
Statement对象: 用于执行不带参数的简单SQL语句: 特点: a. 只执行单条的sql语句: b. 只能执行不带参数的sql语句: c.运行原理的角度,数据库接收到sql语句后需要对该条sql ...
- JDBC 4 PreparedStatement 与Statement 的区别
1 有安全性 PreparedStatement 可以由于不是使用拼接,防止了sql注入,提高了安全性. 2 更方便 PreparedStatement 可以自动对类型进行转换,代码可读性,可维护 ...
- JDBC增删改查,PreparedStatement和Statement的区别
此篇是在上一篇的基础上使用PreparedStatement对象来实现JDBC增删改查的 具体工具类JDBCTools和实现类和配置文件在上一篇Statement对象实现的时候有写. 上一篇地址htt ...
- 在JDBC中使用PreparedStatement代替Statement,同时预防SQL注入
本篇讲诉为何在JDBC操作数据库的过程中,要使用PreparedStatement对象来代替Statement对象. 在前面的JDBC学习中,对于Statement对象,我们已经知道是封装SQL语句并 ...
- Java中PreparedStatement与Statement的总结
概要: PreparedStatement 接口继承自 Statement 接口,PreparedStatement 比普通Statement 对象使用起来更加灵活,更有效率. 一.PreparedS ...
- PreparedStatement与Statement的区别
PreparedStatement与statement的区别 1.PreparedStatement是预编译的,对于批量处理可以大大提高效率. 也叫JDBC存储过程 2.使用 Statement 对象 ...
- preparedStatement和Statement 有什么不一样
1. PreparedStatement接口继承Statement, PreparedStatement 实例包含已编译的 SQL 语句,所以其执行速度要快于 Statement 对象. 2.作 ...
随机推荐
- git学习——<四>git版本管理
一.git版本管理的优势 都说git比svn强大,强大在哪呢? 首先,从部署上说:svn.cvs都是集中式的,一台服务器上部署服务,所有客户端编写的代码都要提交到该服务器上.git是分布式的,所有人都 ...
- 【react 分页器】 基于react-virtualized组件的分页器
react-virtualized 组件本身没有提供分页器功能,见这个issue:https://github.com/bvaughn/react-virtualized/issues/24 如果想给 ...
- Linux touch命令
touch命令不常用,一般用于更改文件时间戳,或创建一个空文件 命令选项 -a:只更改访问时间 -c:--no-create 不创建任何文件 -d:--date=字符串 使用指定字符串表示时间而非当前 ...
- django 密文 cookie 加密
默认cookie是明文 # 加密cookie salt 通过这个字符串把cookie内容加密 obj.set_signed_cookie('username111','aaaa',salt=" ...
- sipp模拟电信运营商VoIP终端测试(SIP协议调试)
三大运营商都有SIP服务器,用来支持语音对讲,多媒体调度等功能,他们的平台可能不是标准的SIP协议会话. 为了应对没完没了的对接各个厂商的平台,这里再整理了一套协议脚本,毕竟全都是没有意义的无用功,标 ...
- PHPCMS 修改后台路径简便方法
之前在网上找了很多关于修改phpcms后台路径的修改方法,但是都太繁琐(个人感觉),终于找到了一个相对简单的修改方法,在这里和大家分享一下,希望互相学习. 第一步:在网站根目录创建一个文件夹,以后就要 ...
- jvm学习(重点)
http://blog.csdn.net/yfqnihao/article/details/8271665 http://blog.csdn.net/cutesource/article/detail ...
- lvds split两channel输出到一个屏显示
转:https://blog.csdn.net/changqing1990/article/details/81128552 其实之前写过LCD/LVDS的一些时序的基本概念<与LCD移植相关的 ...
- 安卓Android第三方登录-QQ登录
要实现QQ第三方登录,其实只需要一个封装类:QQLoginManager 几乎 三行代码 就实现QQ登录功能 这里先给出Github开源项目地址,项目下有详细的使用说明 下面就开始详细说一说怎么实 ...
- Spring提前加载与懒加载
首先,Spring默认是提前加载,这意味着当项目启动,spring初始化,spring会把所有的扫描包下的 ,所有带spring 注解(@Component.@Repository.@Service. ...