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的更多相关文章

  1. JDBC 中preparedStatement和Statement区别

    一.概念 PreparedStatement是用来执行SQL查询语句的API之一,Java提供了 Statement.PreparedStatement 和 CallableStatement三种方式 ...

  2. JDBC中PreparedStatement和Statement的区别

    共同点: PreparedStatement和Statement都是用来执行SQL查询语句的API之一. 不同点: 在PreparedStatement中,当我们经常需要反复执行一条结构相似的sql语 ...

  3. JDBC中PreparedStatement相比Statement的好处

    Statement对象: 用于执行不带参数的简单SQL语句: 特点: a. 只执行单条的sql语句: b. 只能执行不带参数的sql语句: c.运行原理的角度,数据库接收到sql语句后需要对该条sql ...

  4. JDBC 4 PreparedStatement 与Statement 的区别

    1  有安全性 PreparedStatement 可以由于不是使用拼接,防止了sql注入,提高了安全性. 2  更方便 PreparedStatement 可以自动对类型进行转换,代码可读性,可维护 ...

  5. JDBC增删改查,PreparedStatement和Statement的区别

    此篇是在上一篇的基础上使用PreparedStatement对象来实现JDBC增删改查的 具体工具类JDBCTools和实现类和配置文件在上一篇Statement对象实现的时候有写. 上一篇地址htt ...

  6. 在JDBC中使用PreparedStatement代替Statement,同时预防SQL注入

    本篇讲诉为何在JDBC操作数据库的过程中,要使用PreparedStatement对象来代替Statement对象. 在前面的JDBC学习中,对于Statement对象,我们已经知道是封装SQL语句并 ...

  7. Java中PreparedStatement与Statement的总结

    概要: PreparedStatement 接口继承自 Statement 接口,PreparedStatement 比普通Statement 对象使用起来更加灵活,更有效率. 一.PreparedS ...

  8. PreparedStatement与Statement的区别

    PreparedStatement与statement的区别 1.PreparedStatement是预编译的,对于批量处理可以大大提高效率. 也叫JDBC存储过程 2.使用 Statement 对象 ...

  9. preparedStatement和Statement 有什么不一样

    1. PreparedStatement接口继承Statement, PreparedStatement 实例包含已编译的 SQL 语句,所以其执行速度要快于 Statement 对象.    2.作 ...

随机推荐

  1. VMwareWorkstations中安装ubuntu,apt install报E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)

    ubuntu中apt安装软件python时报: E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily un ...

  2. robotium原理之获取WebElement元素

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/hunterno4/article/details/35569665         robotium ...

  3. SSH secure shell 原理与运用

    转: http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html 作者: 阮一峰 日期: 2011年12月21日 SSH是每一台Linux ...

  4. 学习Zookeeper需要了解的专业名词

    一.Zookeeper的集群角色 Leader:该角色是整个zookeeper集群工作机制中的核心 Follower:该角色是zookeeper集群状态的跟随者 Observer:在集群中充当观察者的 ...

  5. k8s-离线安装coreos

    1.安装准备 下载iso 前往页面https://coreos.com/os/docs/latest/booting-with-iso.html 版本:stable 1465.7.0 日期:2017. ...

  6. 设计模式学习(二):面向对象设计原则与UML类图

    一.UML类图和面向对象设计原则简介 在学习设计模式之前,需要找我一些预备知识,主要包括UML类图和面向对象设计原则. UML类图可用于描述每一个设计模式的结构以及对模式实例进行说明,而模式结构又是设 ...

  7. PHP下使用Redis消息队列发布微博(复制)

    phpRedisAdmin :github地址  图形化管理界面 git clone https://github.com/ErikDubbelboer/phpRedisAdmin.git cd ph ...

  8. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) A. Andryusha and Socks

    地址:http://codeforces.com/contest/782/problem/A 题目: A. Andryusha and Socks time limit per test 2 seco ...

  9. SQL Server怎么备份数据库

    1.打开 2.选择需要备份的数据库,右键 Tasks 3.Tasks的下垃菜单 4.add选备份路径,添加名字 5.OK

  10. Spring MVC 流程

    1. 检查是否为上传文件. 2. 通过HandlerMapping获取HandlerExecutionChain: DispatcherServlet 中包含:handlerMappings , 遍历 ...