我有一堆sql脚本,应该在java web应用程序启动时升级数据库。从java运行oracle sql脚本
我试过使用ibatis scriptrunner,但它在定义触发器时光荣地失败,其中“;”字符不标记语句的结尾。
现在我已经编写了我自己的脚本运行器版本,它基本上完成了这项工作,但是破坏了可能的格式和注释,特别是在“创建或替换视图”中。
public class ScriptRunner {
private final DataSource ds;
public ScriptRunner(DataSource ds) {
this.ds = ds;
}
public void run(InputStream sqlStream) throws SQLException, IOException {
sqlStream.reset();
final Statement statement = ds.getConnection().createStatement();
List<String> sqlFragments = createSqlfragments(sqlStream);
for (String toRun : sqlFragments) {
if (toRun.length() > 0) {
statement.execute(toRun);
}
}
}
private static List<String> createSqlfragments(InputStream sqlStream) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(sqlStream));
List<String> ret = new ArrayList<String>();
String line;
StringBuilder script = new StringBuilder();
while ((line = br.readLine()) != null) {
if (line.equals("/")) {
ret.add(removeMultilineComments(script));
script = new StringBuilder();
} else {
//strip comments
final int indexComment = line.indexOf("--");
String lineWithoutComments = (indexComment != -1) ? line.substring(0, indexComment) : line;
script.append(lineWithoutComments).append(" ");
}
}
if (script.length() > 0) {
ret.add(removeMultilineComments(script));
}
return ret;
}
private static String removeMultilineComments(StringBuilder script) {
return script.toString().replaceAll("/\\*(.*?)\\*/", "").trim();
}
是有一个干净的方式来acieve呢?有冬眠的东西,我没有看到? 或者我可以以某种方式将输入流传递给sqlplus吗? 除了我对格式化的担心之外,我怀疑这段代码没有错误,因为我对pl/sql语法的知识有限。
我正在使用ibatis-common-2.jar,我还没有找到setDelimiter(String ,布尔)方法,你正在使用哪个版本的iBatis? – 2014-11-03 07:29:28