0

我想执行多个相互依赖的 sql 脚本文件。所以知道前面的脚本是否成功执行对我来说很重要。我尝试使用以下方法解决它,但我不确定这是否正确。

public static  boolean connect(){
          Connection con=null;
          boolean isScriptExecuted = false;       
          try{

             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
             DriverManager.setLogWriter( new PrintWriter( System.out ) );
             con=DriverManager.getConnection("jdbc:odbc:Test");
             System.out.println("Connected");
             Statement stmnt =con.createStatement();

            String aSQLScriptFilePath = "path1";
            String eSQLScriptFilePath = "path2";
            try {
            //              String sb = readScript(aSQLScriptFilePath);
            int rows=stmnt.executeUpdate(readScript(aSQLScriptFilePath));
            Thread.sleep(500);
            System.out.println(rows);
            if(rows >0)
            {
            System.out.println("2nd");
           //               sb=readScript(aSQLScriptFilePath);
            con=DriverManager.getConnection("jdbc:odbc:Test");
            stmnt =con.createStatement();
            int rows2 =stmnt.executeUpdate(readScript(eSQLScriptFilePath));
            if ( rows2 >0)
            isScriptExecuted = true;
            }


            } catch (Exception e) {
            System.err.println("Failed to Execute" + aSQLScriptFilePath +". The error is"+ e.getMessage());
            }


             }catch(Exception e){
              System.out.println(e.getMessage());
             }
          ResultSet rs = null;
            ResultSetMetaData rsmd = null;

          return isScriptExecuted;
    }

    private static String readScript(String aSQLScriptFilePath)
        throws FileNotFoundException, IOException {
    BufferedReader in = new BufferedReader(new FileReader(aSQLScriptFilePath));
    String str;
    StringBuffer sb = new StringBuffer();
    while ((str = in.readLine()) != null) {
    sb.append(str + "\n ");
    }
    in.close();
    return sb.toString();
}
4

0 回答 0