首页 > 代码库 > java程序执行sql文件

java程序执行sql文件

List<String> sqlFileList = new ArrayList<String>();
从文件读放内容到按分号放到sqlFileList
public List<String> readSqlFiles(List<String> fileNameList) {
List<String> sqlList = new ArrayList<String>();
for (String fileName : fileNameList) {
File myFile = new File(fileName);
if (!myFile.exists()) {
System.err.println("Can‘t Find " + fileName);
continue;
}
StringBuffer temp = new StringBuffer();
try {
BufferedReader in = new BufferedReader(new FileReader(myFile));
String str;
while ((str = in.readLine()) != null) {
temp.append(str);
}
in.close();
} catch (IOException e) {
e.getStackTrace();
}
String sqls[] = temp.toString().split(";");
for (String sql : sqls) {
sqlList.add(sql);
}
}

return sqlList;
}


然后一句句的执行
for (String sql : sqlList) {
pstmt = con.prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
pstmt.execute();

}