자바 sql 스 크 립 트 를 실행 하 는 3 가지 방식(ant,ibatis,ScriptRunner)

5528 단어 ibatis

자바 에서 sql 파일 을 실행 하 는 것 이 번 거 로 운 점 은 sql 스 크 립 트 파일 을 분석 하 는 것 입 니 다.다행히 ant 와 ibatis 는 이러한 실현 을 제공 했다.
package com.unmi;    import java.io.*;    import org.apache.tools.ant.*;   
import org.apache.tools.ant.taskdefs.*;   
import org.apache.tools.ant.types.*;  
/**  
*    ant.jar   SQLExec    SQL       
* @author Unmi  
*/  
public class AntExecSql {  
/**  
* @param args  
*/  
public static void main(String[] args) {   
SQLExec sqlExec = new SQLExec();   
//          
sqlExec.setDriver("oracle.jdbc.driver.OracleDriver");   
sqlExec.setUrl("jdbc:oracle:thin:@10.128.x.x:1521:xxsid");  
sqlExec.setUserid("xxuser");   
sqlExec.setPassword("xxpass");   
//         
sqlExec.setSrc(new File("src/data.sql"));  
//              
sqlExec.setOnerror((SQLExec.OnError)(EnumeratedAttribute.getInstance(   
SQLExec.OnError.class, "abort")));  
sqlExec.setPrint(true); //        
//      sql.out  ;      ,           
sqlExec.setOutput(new File("src/sql.out"));   
sqlExec.setProject(new Project()); //        ,        
sqlExec.execute();   
}   
}  
package com.unmi;
import java.io.*;
import org.apache.tools.ant.*;
import org.apache.tools.ant.taskdefs.*;
import org.apache.tools.ant.types.*;
/**
*    ant.jar   SQLExec    SQL     
* @author Unmi
*/
public class AntExecSql {
/**
* @param args
*/
public static void main(String[] args) {
SQLExec sqlExec = new SQLExec();
//       
sqlExec.setDriver("oracle.jdbc.driver.OracleDriver");
sqlExec.setUrl("jdbc:oracle:thin:@10.128.x.x:1521:xxsid");
sqlExec.setUserid("xxuser");
sqlExec.setPassword("xxpass");
//      
sqlExec.setSrc(new File("src/data.sql"));
//           
sqlExec.setOnerror((SQLExec.OnError)(EnumeratedAttribute.getInstance(
SQLExec.OnError.class, "abort")));
sqlExec.setPrint(true); //      
//      sql.out  ;      ,        
sqlExec.setOutput(new File("src/sql.out"));
sqlExec.setProject(new Project()); //        ,     
sqlExec.execute();
}
}
ibatis: package com.ibatis.jpetstore.test;
import java.sql.DriverManager;
import java.util.Properties;
import com.ibatis.common.jdbc.ScriptRunner;
import com.ibatis.common.resources.Resources;
import com.mysql.jdbc.Connection;
public class DBTestSQL {
 
 public static void main(String[] args) {
  
  try {
   Properties props = Resources.getResourceAsProperties("properties/database.properties");
   String url = props.getProperty("url");
   String driver = props.getProperty("driver");
   String username = props.getProperty("username");
   String password = props.getProperty("password");
   System.out.println(url);
   if(url.equals("jdbc:mysql://localhost:3306/jpetstore1")) {
    Class.forName(driver).newInstance();
    Connection conn = (Connection) DriverManager.getConnection(url, username, password);
    ScriptRunner runner = new ScriptRunner(conn, false, false);
    runner.setErrorLogWriter(null);
    runner.setLogWriter(null);
    runner.runScript(Resources.getResourceAsReader("ddl/mysql/jpetstore-mysql-schema.sql"));
    runner.runScript(Resources.getResourceAsReader("ddl/mysql/jpetstore-mysql-dataload.sql"));
    
   }
   
  } catch (Exception e) {
   e.printStackTrace();
  }
  
 }
} 
ScriptRunner(com.ibatis.common.jdbc.*)용법ScriptRunner     SQL  ,       schema,             。                 :
     1:
   Connection conn=getConnection();//some method to get a Connection
  ScriptRunner runner=new ScriptRunner();
  runner.runScript(conn,Resources.getResourceAsReader("com/some/resource/path/initialize.sql"));
   conn.close();
   2:
  ScriptRunner runner=new ScriptRunner("com.some.Driver","jdbc:url://db","login","password");
  runner.runScript(conn,new FileReader("/user/local/db/scripts/initialize-db.sql"));
3:
  Properties props= getProperties();//some properties form somewhere
ScriptRunner runner =new ScriptRunner(props);
runner.runScript(conn,new FileReader("/user/local/db/scripts/initialize-db.sql"));

좋은 웹페이지 즐겨찾기