java_JDBC(3)
2015-06-30 19:30
381 查看
Batch和Fetch两个特性非常重要。
Batch相当于JDBC的写缓冲,Fetch相当于读缓冲
如果把JDBC类比为JAVA IO的话,
不使用Fetch和Batch相当于直接使用FileInputStream和FileOutputStream
而设置了Fetch和Batch相当于使用BufferedInputStream和BufferedOutputStream
Batch相当于JDBC的写缓冲,Fetch相当于读缓冲
如果把JDBC类比为JAVA IO的话,
不使用Fetch和Batch相当于直接使用FileInputStream和FileOutputStream
而设置了Fetch和Batch相当于使用BufferedInputStream和BufferedOutputStream
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class Test { private static int _1W = 10000; private static List<String> list = new ArrayList<String>(100 * _1W); static { for (int i = 0; i < 10 * _1W; i++) { list.add(String.valueOf(i)); } } public static void main(String[] args) throws ClassNotFoundException, SQLException { long start = System.currentTimeMillis(); fetchRead(); long end = System.currentTimeMillis(); System.out.println((end - start) + "ms"); } public static void batchWrite() throws SQLException, ClassNotFoundException { // 1108ms Class.forName("oracle.jdbc.OracleDriver"); Connection connection = DriverManager.getConnection("jdbc:oracle:thin:127.0.0.1:1521:orcl", "xxx", "xxx"); connection.setAutoCommit(false); PreparedStatement cmd = connection.prepareStatement("insert into t values(?)"); for (int i = 0; i < list.size(); i++) { cmd.setString(1, list.get(i)); cmd.addBatch(); if (i % _1W == 0) { cmd.executeBatch(); } } cmd.executeBatch(); connection.commit(); } public static void jdbcWrite() throws ClassNotFoundException, SQLException { // 28189ms Class.forName("oracle.jdbc.OracleDriver"); Connection connection = DriverManager.getConnection("jdbc:oracle:thin:127.0.0.1:1521:orcl", "xxx", "xxx"); connection.setAutoCommit(false); PreparedStatement cmd = connection.prepareStatement("insert into t values(?)"); for (String s : list) { cmd.setString(1, s); cmd.execute(); } connection.commit(); } public static void jdbcRead() throws ClassNotFoundException, SQLException { // 3120ms Class.forName("oracle.jdbc.OracleDriver"); Connection connection = DriverManager.getConnection("jdbc:oracle:thin:127.0.0.1:1521:orcl", "xxx", "xxx"); connection.setAutoCommit(false); PreparedStatement cmd = connection.prepareStatement("select * from t"); ResultSet rs = cmd.executeQuery(); int i = 0; while (rs.next()) { rs.getString(1); i = i + 1; } System.out.println("count:" + i); } public static void fetchRead() throws ClassNotFoundException, SQLException { //764ms Class.forName("oracle.jdbc.OracleDriver"); Connection connection = DriverManager.getConnection("jdbc:oracle:thin:127.0.0.1:1521:orcl", "xxx", "xxx"); connection.setAutoCommit(false); PreparedStatement cmd = connection.prepareStatement("select * from t"); cmd.setFetchSize(_1W); ResultSet rs = cmd.executeQuery(); int i = 0; while (rs.next()) { rs.getString(1); i = i + 1; } System.out.println("count:" + i); } }
相关文章推荐
- Java反射应用案例
- Java文件选择对话框(文件选择器JFileChooser)的使用:以一个文件加密器为例
- Spring MVC-RequestToViewNameTranslator
- springmvc学习笔记---idea创建springmvc项目
- java_JDBC(1)
- Spring整合Junit测试,并且配置事务
- java设计模式之单例模式
- Java新技术---线程学习之常用同步工具类
- 【java0010】一张图看懂java集合
- 4、javaweb listener 应用场景
- 利用Java编写简单的WebService实例
- Java操作PDF之iText超入门
- 3、javaweb listener session绑定&钝化--监听
- java 并行框架 并行编程
- log4j的使用--java如何使用日志
- mybatis与spring整合
- Eclipse 设置代码换行
- 用eclipse还是myeclipse好
- The Skyline Problem Leetcode Java
- java Web应用配置log4j日志记录