需要引入ant的jar包
public static int getConnection(String url,String user,String password) {
SQLExec sqlExec;
try {
String[] sql = {"/table.sql","/tables_in.sql","/tables_up.sql","/cashbilllist_in.sql","/cashbilllist_del.sql","/cashbill_in.sql"
,"/custment_in.sql","/custment_up.sql","/vip_in.sql","/vip_up.sql","/cashbilllist_up.sql"};
System.out.println("size: " + sql.length);
for(int i=0;i<sql.length;i++){
String a = sql[i];
System.out.println(a);
sqlExec = new SQLExec();
//设置数据库参数
sqlExec.setDriver("com.microsoft.sqlserver.jdbc.SQLServerDriver");
sqlExec.setUrl(url);
sqlExec.setUserid(user);
sqlExec.setPassword(password);
sqlExec.setEncoding("UTF8");
//要执行的脚本
sqlExec.setSrc(new File(System.getProperty("user.dir") + sql[i]));
//有出错的语句该如何处理
sqlExec.setOnerror((SQLExec.OnError)(EnumeratedAttribute.getInstance(
SQLExec.OnError.class, "abort")));
sqlExec.setPrint(true); //设置是否输出
//输出到文件 sql.out 中;不设置该属性,默认输出到控制台
sqlExec.setOutput(new File(System.getProperty("user.dir")+"/sql.out"));
sqlExec.setProject(new Project()); // 要指定这个属性,不然会出错
sqlExec.execute();
}
return 1;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}