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.作 ...
随机推荐
- Flume 1.7 源代码分析(四)从Source写数据到Channel
Flume 1.7 源代码分析(一)源代码编译 Flume 1.7 源代码分析(二)总体架构 Flume 1.7 源代码分析(三)程序入口 Flume 1.7 源代码分析(四)从Source写数据到C ...
- 技术架构标杆(Certicom Security Architecture)对比思考——By Me at 20140408
看到一家国外网络安全企业Certicom,官网链接:http://www.certicom.com/,可以作为很好的企业安全技术建构以及产品规划的标杆,下面我绘制了该公司的产品组合以及技术架构框图:
- C++入门(1)
#include<>直接从编译器自带的函数库中寻找文件 #include""是先从自定义的文件中找 ,如果找不到在从函数库中寻找文件 采用"< > ...
- jQuery中on()方法用法实例
这篇文章主要介绍了jQuery中on()方法用法,实例分析了on()方法的功能.定义及在匹配元素上绑定一个或者多个事件处理函数的使用技巧,需要的朋友可以参考下 本文实例讲述了jQuery中on()方法 ...
- Spring的AOP实现
内容详见切面编程系列 https://www.cnblogs.com/jiyukai/category/1265045.html.
- matplotlib中文乱码解决方法
每次编写代码时进行参数设置 import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签 ...
- k8s-离线安装coreos
1.安装准备 下载iso 前往页面https://coreos.com/os/docs/latest/booting-with-iso.html 版本:stable 1465.7.0 日期:2017. ...
- Bitmap: a C++ class
Bitmap: a C++ class The five steps involved to draw a bitmap: Load bitmap using LoadBitmap or ...
- Delphi APP 開發入門(二)Android/iOS設定,Hello World
Delphi APP 開發入門(二)Android/iOS設定,Hello World 分享: Share on facebookShare on twitterShare on google_plu ...
- window7主题破解与恢复(复制)
window7主题破解与恢复 1 2 3 分步阅读 windows7主题破解后可以换自己喜欢的主题,但也有一些弊端.这里帮助打家破解与恢复. 工具/原料 UniversalThemePatcher.e ...