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. 网络编程 - UDP协议

    UDP协议 服务端 ''' UDP 协议 又称 数据报协议 SOCK_DGRAM ''' from socket import * # 一般不这样做 会重名 但写socket可以这样写 因为要用到太多 ...

  2. Django 框架之视图函数(Views)

    http请求中产生的两个核心对象: http请求: HttpRequest对象 http响应: HttpResponse对象 所在位置: django.http HttpRequest 对象的属性和方 ...

  3. 使用Custom Draw优雅的实现ListCtrl的重绘

    common control 4.7版本介绍了一个新的特性叫做Custom Draw,这个名字显得模糊不清,让人有点摸不着头脑,而且MSDN里也只给出了一些如风的解释和例子,没有谁告诉你你想知道的,和 ...

  4. Python面试应急5分钟!

    ​ 不论你是初入江湖,还是江湖老手,只要你想给自己一个定位那就少不了面试!面试的重要性相信大家都知道把,这就是我们常说的“第一印象”,给大家说一下我的面试心得把,面试前的紧张是要的,因为这能让你充分准 ...

  5. iOS开发- 速学Swift-中文概述

    Swift是什么? Swift是苹果于WWDC 2014公布的编程语言,这里引用The Swift Programming Language的原话: Swift is a new programmin ...

  6. Powerdesigner显示列名

    设置要修改的列 点击ok即可.

  7. 001-shell基础,创建,运行

    一.概述 Shell 是一个用 C 语言编写的程序.Shell 既是一种命令语言,又是一种程序设计语言. Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服 ...

  8. centos运行asp.netcore的正确姿势

    centos安装.netcore2.0 一切都很顺利,console app运行 dotnet run也ok 但是asp.net core app运行 dotnet run 会抛出Warn:“Unab ...

  9. APP移动端开发html模板

    移动端开发模板: 750的稿子除以75: <!DOCTYPE html> <html> <head> <meta charset="utf-8&qu ...

  10. 今天刚接触lua 写写环境配置和基本设置

    首先是安装开发工具: Quick: http://www.tairan.com/engines-download 引擎 Sublime Text的下载地址为:http://www.sublimetex ...