mysql使用

来源:互联网 发布:asp.net 微信支付源码 编辑:程序博客网 时间:2024/05/18 03:53

往mysql中导入一个百兆的sql脚本,都好慢啊,有空得学学用java代码成批导入怎么弄,下去写了一段,有错,然后就没再写了,贴一下先;

package cc;


import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;




public class Insertmysql {
    public static void main(String[] args) throws SQLException
    {
    String driver = "com.mysql.jdbc.Driver";
        String sql;
     
    try{
    Class.forName(driver);
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/envp", "root", "root");
       try {
Statement stmt = conn.createStatement();
conn.setAutoCommit(false);
FileReader fr = new FileReader("D:/environment/data11/traffic.sql");
BufferedReader br = new BufferedReader(fr);
int count = 0;
while ((sql = br.readLine()) != null ) {
if(++count < 200){
stmt.addBatch(sql);
}
else{
stmt.executeBatch();
stmt.clearBatch();
count = 0;
}

}
} catch (Exception e) {
// TODO: handle exception
}finally{
//stmt.close();
}
    }catch(ClassNotFoundException e){
    e.printStackTrace();  
    }catch(SQLException e){
    e.printStackTrace();
    }
    finally{
    //conn.close();
    }
   
   
    }
}

原创粉丝点击