当前位置: 代码迷 >> SQL >> java 当地解析sql文件,执行批量更新
  详细解决方案

java 当地解析sql文件,执行批量更新

热度:90   发布时间:2016-05-05 12:49:20.0
java 本地解析sql文件,执行批量更新

???????????? ?java? 解析sql文件,批量更新

?

package cn.com.superv.netmessage.util;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public? class DBTool {

?/**
? * @param args
? * @throws ClassNotFoundException
? * @throws SQLException
? * @throws IOException
? * @throws IllegalAccessException
? * @throws InstantiationException
? */
? //private final static ThreadLocal? local? = new ThreadLocal();
?
?public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException, InstantiationException, IllegalAccessException {
??// TODO Auto-generated method stub
??//要获得的文件名
??String path = "data.sql";
??//连接变量
??String url,uid,pwd;
??Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
??url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
??uid = "netmessage";
??pwd = "netmessage";
??????? execute(url, uid, pwd, path);

?}
?public static Connection? getConnection(String url,String uid,String pwd) throws InstantiationException, IllegalAccessException, ClassNotFoundException{
?? Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
?? Connection cn = null;
??try {
???cn = DriverManager.getConnection(url,uid,pwd);
??} catch (SQLException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??}
?? return cn;
?}
?public static void execute(String url,String uid,String pwd ,String path){
??Connection cn = null;
??try {
???cn = getConnection (url,uid,pwd);
??} catch (Exception e1) {
???// TODO Auto-generated catch block
???e1.printStackTrace();
??}
?? Statement stmt = null;
????? //读取文件
????? try
????? {stmt = cn.createStatement();
????????? InputStream r = new FileInputStream(path);
????????? ByteArrayOutputStream byteout = new ByteArrayOutputStream();
????????? byte tmp[] = new byte[99999];
????????? byte context[];
????????? int i = 0;
????????? while ((i = r.read(tmp)) != -1)
????????? {
????????????? byteout.write(tmp);
????????? }
????????? context = byteout.toByteArray();
????????? String str = new String(context, "UTF-8");
????????? // 分隔行
????????? String stra[] = str.split(";");
????????? for (int n = 0; n < stra.length; n++)
????????? {
????????????? System.out.println(stra[n]);
????????????? stmt.addBatch(stra[n]);
????????? }
????????? stmt.executeBatch();
????? }
????? catch (Exception e)
????? {
????????? e.printStackTrace();
????? } finally {
????? ?? if(cn!=null){
?????? try {
??????if(!cn.isClosed()){
???????? cn.close();
??????? }
?????} catch (SQLException e) {
??????// TODO Auto-generated catch block
??????e.printStackTrace();
?????}
????? }}
?}
}

?

http://www.1diaocha.com/user/Register.aspx?account=soqian
参加调查,轻松赚钱

  相关解决方案