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
参加调查,轻松赚钱

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值