博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 执行 SQL 脚本文件
阅读量:6911 次
发布时间:2019-06-27

本文共 3199 字,大约阅读时间需要 10 分钟。

hot3.png

 

转自:

1 package com.unmi.db;  2   3 import java.io.FileInputStream;  4 import java.io.InputStream;  5 import java.sql.Connection;  6 import java.sql.Statement;  7 import java.util.ArrayList;  8 import java.util.Arrays;  9 import java.util.List; 10  11 /** 12  * 读取 SQL 脚本并执行 13  * @author Unmi 14  */ 15 public class SqlFileExecutor { 16  17     /** 18      * 读取 SQL 文件,获取 SQL 语句 19      * @param sqlFile SQL 脚本文件 20      * @return List
返回所有 SQL 语句的 List 21 * @throws Exception 22 */ 23 private List
loadSql(String sqlFile) throws Exception { 24 List
sqlList = new ArrayList
(); 25 26 try { 27 InputStream sqlFileIn = new FileInputStream(sqlFile); 28 29 StringBuffer sqlSb = new StringBuffer(); 30 byte[] buff = new byte[1024]; 31 int byteRead = 0; 32 while ((byteRead = sqlFileIn.read(buff)) != -1) { 33 sqlSb.append(new String(buff, 0, byteRead)); 34 } 35 36 // Windows 下换行是 /r/n, Linux 下是 /n 37 String[] sqlArr = sqlSb.toString().split("(;//s*//r//n)|(;//s*//n)"); 38 for (int i = 0; i < sqlArr.length; i++) { 39 String sql = sqlArr[i].replaceAll("--.*", "").trim(); 40 if (!sql.equals("")) { 41 sqlList.add(sql); 42 } 43 } 44 return sqlList; 45 } catch (Exception ex) { 46 throw new Exception(ex.getMessage()); 47 } 48 } 49 50 /** 51 * 传入连接来执行 SQL 脚本文件,这样可与其外的数据库操作同处一个事物中 52 * @param conn 传入数据库连接 53 * @param sqlFile SQL 脚本文件 54 * @throws Exception 55 */ 56 public void execute(Connection conn, String sqlFile) throws Exception { 57 Statement stmt = null; 58 List
sqlList = loadSql(sqlFile); 59 stmt = conn.createStatement(); 60 for (String sql : sqlList) { 61 stmt.addBatch(sql); 62 } 63 int[] rows = stmt.executeBatch(); 64 System.out.println("Row count:" + Arrays.toString(rows)); 65 } 66 67 /** 68 * 自建连接,独立事物中执行 SQL 文件 69 * @param sqlFile SQL 脚本文件 70 * @throws Exception 71 */ 72 public void execute(String sqlFile) throws Exception { 73 Connection conn = DBCenter.getConnection(); 74 Statement stmt = null; 75 List
sqlList = loadSql(sqlFile); 76 try { 77 conn.setAutoCommit(false); 78 stmt = conn.createStatement(); 79 for (String sql : sqlList) { 80 stmt.addBatch(sql); 81 } 82 int[] rows = stmt.executeBatch(); 83 System.out.println("Row count:" + Arrays.toString(rows)); 84 DBCenter.commit(conn); 85 } catch (Exception ex) { 86 DBCenter.rollback(conn); 87 throw ex; 88 } finally { 89 DBCenter.close(null, stmt, conn); 90 } 91 } 92 93 public static void main(String[] args) throws Exception { 94 List
sqlList = new SqlFileExecutor().loadSql(args[0]); 95 System.out.println("size:" + sqlList.size()); 96 for (String sql : sqlList) { 97 System.out.println(sql); 98 } 99 }100 }

 

转载于:https://my.oschina.net/ZaneYoung/blog/330759

你可能感兴趣的文章
自定义Windows 7右键发送到选项
查看>>
我的友情链接
查看>>
虚拟化二、Xen虚拟化技术
查看>>
Spring Boot学习--属性配置和使用
查看>>
CentOS 安装 apache
查看>>
Oracle 11g数据库随系统自动启动与关闭的设置方法
查看>>
redhat 桥接配置
查看>>
比肩微信小程序的快应用联盟
查看>>
天猫与九大快递合作 价格热战之后的冷静竞争
查看>>
git pull force
查看>>
scons用户手册
查看>>
使用new操作符来调用一个构造函数的时候发生了什么
查看>>
element-ui之el-scrollbar源码解析学习
查看>>
ceph 的pg诊断
查看>>
交换机配置vlan 访问控制列表
查看>>
我的友情链接
查看>>
12个时间管理妙招
查看>>
2014阿里巴巴校园招聘研发工程师笔试题(北邮站)
查看>>
ViewPager的onPageChangeListener里面的一些方法参数:
查看>>
solr搜索引擎使用
查看>>