Java 执行 SQL 脚本文件
是拷贝的别人的,以备学习
package com.unmi.db;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 读取 SQL 脚本并执行
* @author Unmi
*/
public class SqlFileExecutor {
/**
* 读取 SQL 文件,获取 SQL 语句
* @param sqlFile SQL 脚本文件
* @return List<sql> 返回所有 SQL 语句的 List
* @throws Exception
*/
private List<String> loadSql(String sqlFile) throws Exception {
List<String> sqlList = new ArrayList<String>();
try {
InputStream sqlFileIn = new FileInputStream(sqlFile);
StringBuffer sqlSb = new StringBuffer();
byte[] buff = new byte[1024];
int byteRead = 0;
while ((byteRead = sqlFileIn.read(buff)) != -1) {
sqlSb.append(new String(buff, 0, byteRead));
}
// Windows 下换行是 \r\n, Linux 下是 \n
String[] sqlArr = sqlSb.toString().split("(;\\s*\\r\\n)|(;\\s*\\n)");
for (int i = 0; i < sqlArr.length; i++) {
String sql = sqlArr[i].replaceAll("--.*", "").trim();
if (!sql.equals("")) {
sqlList.add(sql);
}
}
return sqlList;
} catch (Exception ex) {
throw new Exception(ex.getMessage());
}
}
/**
* 传入连接来执行 SQL 脚本文件,这样可与其外的数据库操作同处一个事物中
* @param conn 传入数据库连接
* @param sqlFile SQL 脚本文件
* @throws Exception
*/
public void execute(Connection conn, String sqlFile) throws Exception {
Statement stmt = null;
List<String> sqlList = loadSql(sqlFile);
stmt = conn.createStatement();
for (String sql : sqlList) {
stmt.addBatch(sql);
}
int[] rows = stmt.executeBatch();
System.out.println("Row count:" + Arrays.toString(rows));
}
/**
* 自建连接,独立事物中执行 SQL 文件
* @param sqlFile SQL 脚本文件
* @throws Exception
*/
public void execute(String sqlFile) throws Exception {
Connection conn = DBCente
相关文档:
怎么增加SQL Server连接数
1. 看操作平台的連接數是多少
控制台的授權看看
2. 進GENERAL看看SQL SERVER USERS CONNECTIONS
增大即可
sp_configure 'number of connection'
go ......
如何修改SQL Server的连接数
我把SQL Server 7.0的用户连接数设为1后,数据库就再也连不上了,所以也没办法修改连接数
请问有什么办法能修改连接数?
在server 的属性里面有个connetctions 的
maximun concurr ......
在有关数据库的项目开发中,编码,bug修改等等,都需要查看操作相关的SQL文,如果SQL文比较复杂的话,我们自己排版非常麻烦,同时也很花费时间。可能有的公司自己开发了格式化工具或者购买了格式化工具软件。有了格式工具我们就节省了排版时间。
介绍一下SQLinFormpro_Desktop(htt ......
transaction 1:
------------------------------------------------------
begin transaction
update table1 set cola = 'str1'
Waitfor time '10:12:00'
update table2 set colb = 'str2'
rollback transaction
transaction 2:
---- ......
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE proc pGetInsertSQL (@TableName varchar(256))
as
begin
set nocount on
declare @sqlstr varchar(4000)
declare @sqlstr1 varchar(4000)
declare @sqlstr2 v ......