import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class InertDB_batch {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("***************");
String buf = null;
BufferedReader in = null;
Statement stmt =null;
Connection con =null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?characterEncoding=gb2312", "root", "root");
stmt = con.createStatement();
}catch(Exception e){
e.printStackTrace();
}
try {
in = new BufferedReader(new FileReader("insert.sql"));
buf = in.readLine();
System.out.println(buf);
} catch (FileNotFoundException e) {
new Exception("insert.sql File not exists!");
} catch (IOException e) {
e.printStackTrace();
}
while (buf != null) {
System.out.println(buf);
try {
//将给定的 SQL 命令添加到此 Statement 对象的当前命令列表中
stmt.addBatch(buf);
buf = in.readLine();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try{
int []line = stmt.executeBatch();
for(int i=0;i<line.length;i++){
System.out.println("i = "+i);
}
//System.out.println(line);
}catch(Exception ex){
ex.printStackTrace();
}
System.out.println("***************");
System.out.println("完了!!");
}
}
使用JAVA语言中的addBatch和executeBatch()实现数据批处理插入数据库
最新推荐文章于 2022-08-09 15:07:49 发布
