JDBC执行匿名块
mytables.sql
package com.geosun.main;import java.io.BufferedReader;import java.io.FileReader;import java.sql.Connection;import java.sql.PreparedStatement;import com.geosun.conf.DBUtils;public class ProcessUnNameSQL {public static StringBuffer readFile(String filePath){BufferedReader reader = null;StringBuffer sb = new StringBuffer();try {reader = new BufferedReader(new FileReader(filePath));String line = reader.readLine();while(line!=null){sb.append(line+"\n");line = reader.readLine();}} catch (Exception e) {e.printStackTrace();throw new RuntimeException(e);}System.out.println(sb.toString());return sb;}public static void process(Connection conn, String filePath){StringBuffer sb = readFile(filePath);try {conn.setAutoCommit(false);PreparedStatement st = conn.prepareStatement(sb.toString());System.out.println("Start Process");st.execute();conn.commit();System.out.println("Process success.");} catch (Exception e) {// TODO: handle exceptione.printStackTrace();throw new RuntimeException(e);}}public static void main(String[] args) {// TODO Auto-generated method stubtry {Connection conn = DBUtils.getNewConn("jdbc:oracle:thin:@localhost:1521:orcl", "aaa", "aaa", "oracle.jdbc.driver.OracleDriver");String filePath="D:\mytables.sql";process(conn, filePath);conn.close();} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}}