首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

JDBC施行匿名块

2012-09-28 
JDBC执行匿名块mytables.sqlpackage com.geosun.mainimport java.io.BufferedReaderimport java.io.File

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();}}}

热点排行