• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

java连接mysql数据库实现单条插入和批量插入

java 搞代码 4年前 (2022-01-09) 16次浏览 已收录 0个评论

本文实例为大家分享了java连接mysql数据库实现单条和批量插入的具体代码,供大家参考,具体内容如下

本文插入数据库的数据来源:java + dom4j.jar提取xml文档内容

1、连接数据库

package com.njupt.ymh;
 
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
 
public class Connect_MySQL {
 
 private static final String URL="jdbc:mysql://127.0.0.1:3306/news"; // 一般默认3306,这里设置成6666 (33060) MYSQL8 WMPNetworkSvc
 private static final String USER="root";
 private static final String PASSWORD="12345";
 private static Connection connection=null;
 
 static{
 //1、加载驱动程序(反射的方法)
 try {
  Class.forName("com.mysql.jdbc.Driver");
 } catch (ClassNotFoundException e) {
  e.printStackTrace();
 }
 //2、连接数据库
 try {
 connection=(Connection) DriverManager.
   getConnection(URL, USER,PASSWORD);//地址,用户名,密码
 } catch (SQLException e) {
  e.printStackTrace();
 }
 }
 public static Connection getConnection(){
 return connection;
 }
 
}

 2、单条插入

package com.njupt.ymh;
/**
 * 单条插入数据
 */
 
import java.sql.SQLException;
import java.util.List;
 
import com.mysql.jdbc.Connection;
 
public class OperationPaper {
 
 private static Connection connection=Connect_MySQL.getConnection();
 
 public void addNewsPaper(NewsPaper newsPaper){//增
 // connection = Connect_MySQL.getConnection();
 String sql="insert into papertest (id, date, title, lead_pargraph, full_text) values(?, ?, ?, ?, ?)";
 
  java.sql.PreparedStatement ptmt = null;
  try {
  ptmt = connection.prepareStatement(sql);
  } catch (SQLException e1) {
  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 (SQLException e) {
  e.printStackTrace();
  }
 }
 public static void main(String[] args) {
 OperationPaper operationPaper = new OperationPaper();
 List<String> listFile = SearchFile.getAllFile("E:\\huadai\\1996\\07\\21", false); // 文件列表
 
 for (String string : listFile) {  
  NewsPaper newsPaper = new NewsPaper(string);
  if (newsPaper.isUseful()) 
  operationPaper.addNewsPaper(newsPaper); // 插入数据库
 }
 }
 
}

3、批量插入

package com.njupt.ymh;
 
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
 
import com.mysql.jdbc.Connection;
 
public class OperaOnNewsPaper implements Cloneable{
 
private static Connection connection=Connect_MySQL.getConnection();
 
 
 /**
 * 支持批量插入数据
 * @param newsPaper
 */
 
 public void addNewsPaper(ArrayList<NewsPaper> listNewsPaper){//增
 String sql="insert into papertest (id, date, title, lead_pargraph, full_text) values(?, ?, ?, ?, ?)";
 java.sql.PreparedStatement ptmt = null;
 
 try {
  connection.setAutoCommit(false);// 关闭事务
  ptmt = connection.prepareStatement(sql);
 
 } catch (SQLException e2) {
  e2.printStackTrace();
 } 
 
 for (NewsPaper paperaper : 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 (SQLException e) {
  e.printStackTrace();
  }
<div style="color:transparent">本文来源gaodai.ma#com搞##代!^码@网*</div> }
 try {
  ptmt.executeBatch();//执行给定的SQL语句,该语句可能返回多个结果
  connection.commit();
 } catch (SQLException e) {
  e.printStackTrace();
 }
 }
 
 
 public static void main(String[] args) {
 OperaOnNewsPaper operation = new OperaOnNewsPaper();
 List<String> listFile = SearchFile.getAllFile("E:\\huadai\\2007", false); // 文件列表
 ArrayList<NewsPaper> listPaper = new ArrayList<>();
 int count = 0;
 int sizenum = 1000;
 for (String string : listFile) {
  NewsPaper newsPaper = new NewsPaper(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("zui hou ");
 } 
 }
}

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:java连接mysql数据库实现单条插入和批量插入
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址