java连接mysql数据库实现单条插入和批量插入
本文实例为大家分享了java连接mysql数据库实现单条和批量插入的具体代码,供大家参考,具体内容如下
本文插入数据库的数据来源:java+dom4j.jar提取xml文档内容
1、连接数据库
packagecom.njupt.ymh; importjava.sql.DriverManager; importjava.sql.SQLException; importcom.mysql.jdbc.Connection; publicclassConnect_MySQL{ privatestaticfinalStringURL="jdbc:mysql://127.0.0.1:3306/news";//一般默认3306,这里设置成6666(33060)MYSQL8WMPNetworkSvc privatestaticfinalStringUSER="root"; privatestaticfinalStringPASSWORD="12345"; privatestaticConnectionconnection=null; static{ //1、加载驱动程序(反射的方法) try{ Class.forName("com.mysql.jdbc.Driver"); }catch(ClassNotFoundExceptione){ e.printStackTrace(); } //2、连接数据库 try{ connection=(Connection)DriverManager. getConnection(URL,USER,PASSWORD);//地址,用户名,密码 }catch(SQLExceptione){ e.printStackTrace(); } } publicstaticConnectiongetConnection(){ returnconnection; } }
2、单条插入
packagecom.njupt.ymh; /** *单条插入数据 */ importjava.sql.SQLException; importjava.util.List; importcom.mysql.jdbc.Connection; publicclassOperationPaper{ privatestaticConnectionconnection=Connect_MySQL.getConnection(); publicvoidaddNewsPaper(NewsPapernewsPaper){//增 //connection=Connect_MySQL.getConnection(); Stringsql="insertintopapertest(id,date,title,lead_pargraph,full_text)values(?,?,?,?,?)"; java.sql.PreparedStatementptmt=null; try{ ptmt=connection.prepareStatement(sql); }catch(SQLExceptione1){ e1.printStackTrace(); } try{ ptmt.setLong(1,newsPaper.getID()); ptmt.setString(2,newsPaper.getDate()); ptmt.setString(3,newsPaper.getTitle()); ptmt.setString(4,newsPaper.getLead()); ptmt.setString(5,newsPaper.getfull()); ptmt.execute();//执行给定的SQL语句,该语句可能返回多个结果 }catch(SQLExceptione){ e.printStackTrace(); } } publicstaticvoidmain(String[]args){ OperationPaperoperationPaper=newOperationPaper(); ListlistFile=SearchFile.getAllFile("E:\\huadai\\1996\\07\\21",false);//文件列表 for(Stringstring:listFile){ NewsPapernewsPaper=newNewsPaper(string); if(newsPaper.isUseful()) operationPaper.addNewsPaper(newsPaper);//插入数据库 } } }
3、批量插入
packagecom.njupt.ymh; importjava.sql.SQLException; importjava.util.ArrayList; importjava.util.List; importcom.mysql.jdbc.Connection; publicclassOperaOnNewsPaperimplementsCloneable{ privatestaticConnectionconnection=Connect_MySQL.getConnection(); /** *支持批量插入数据 *@paramnewsPaper */ publicvoidaddNewsPaper(ArrayListlistNewsPaper){//增 Stringsql="insertintopapertest(id,date,title,lead_pargraph,full_text)values(?,?,?,?,?)"; java.sql.PreparedStatementptmt=null; try{ connection.setAutoCommit(false);//关闭事务 ptmt=connection.prepareStatement(sql); }catch(SQLExceptione2){ e2.printStackTrace(); } for(NewsPaperpaperaper:listNewsPaper){ try{ ptmt.setLong(1,paperaper.getID()); ptmt.setString(2,paperaper.getDate()); ptmt.setString(3,paperaper.getTitle()); ptmt.setString(4,paperaper.getLead()); ptmt.setString(5,paperaper.getfull()); ptmt.addBatch(); } catch(SQLExceptione){ e.printStackTrace(); } } try{ ptmt.executeBatch();//执行给定的SQL语句,该语句可能返回多个结果 connection.commit(); }catch(SQLExceptione){ e.printStackTrace(); } } publicstaticvoidmain(String[]args){ OperaOnNewsPaperoperation=newOperaOnNewsPaper(); List listFile=SearchFile.getAllFile("E:\\huadai\\2007",false);//文件列表 ArrayList listPaper=newArrayList<>(); intcount=0; intsizenum=1000; for(Stringstring:listFile){ NewsPapernewsPaper=newNewsPaper(string); if(newsPaper.isUseful()){ count++; listPaper.add(newsPaper);//新闻列表 if(count%sizenum==0){ //System.out.println("ok"); System.out.println(""+count); operation.addNewsPaper(listPaper);//插入数据库 System.out.println(count); listPaper.clear(); } } } if(count%sizenum!=0){ operation.addNewsPaper(listPaper); System.out.println("zuihou"); } } }
通过实际测试,大概十万级数据批量插入要不单条插入节省10分钟左右时间。因为每次单条插入就要和数据库建立一次连接,进行一次日志更新。但是,如果批量插入过程中,批量的数据值有一条不符合格式就将导致本次批量插入整体失败,因此需要对失败情况进行处理,或者对批量插入的数据进行预处理,保证批量插入能够成功。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。