Java 批量插入数据到数据库(MySQL)中

实现Java批量插入数据库数据:

package Proxy;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class TextReader{
		public static void main(String args[]) {  
	        try
	        {
		        	Class.forName("com.mysql.jdbc.Driver");  
					Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://" +  
					        "localhost:3306/DP", "admin", "123456");  
					String sql = "Insert into ACT_cdKeytempsss (cdKey, ActivityId) VALUES (?,3577)";
					
					String sqlupdate = "update ACT_cdKey set status=1 where CdKey = ?";
					
					PreparedStatement ps = con.prepareStatement(sql);
					
					PreparedStatement ps1 = con.prepareStatement(sqlupdate);
					
					con.setAutoCommit(false);  // 关闭事务自动提交  
	        	
	        	
		        	  String pathname = "C:/Users/Administrator/Desktop/result8.txt";
		        	  File fileName = new File(pathname);
		        	  
		        	  InputStreamReader reader = new InputStreamReader(new FileInputStream(fileName));
		        	  BufferedReader br = new BufferedReader(reader); 
		        	  String line = "";  
		              line = br.readLine();  
		              
		              
		              while (line != null) {  
		                  ps.setString(1, line);
		                  ps1.setString(1, line);
		                  line = br.readLine();
		                  ps.addBatch();
		                  ps1.addBatch();
		              }  
		              
		              ps.executeBatch();
		              ps1.executeBatch();
		              con.commit();
		              ps.close();
		              ps1.close();
		              con.close();
		              br.close();
		        }
		        catch (Exception e) {
					e.printStackTrace();// TODO: handle exception
				}
	            
  }
}	        



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值