📄 dbimpl.java
字号:
package org.com.gather;
import java.util.*;
import java.sql.*;
public class DBImpl{
private Properties pro = null;
private Log log = null;
public DBImpl(Properties pro){
this.pro = pro;
}
public void storeToDB(Collection col)throws Exception{
if(col != null){
long start = System.currentTimeMillis();
Connection con = this.getConnection();
long end = System.currentTimeMillis();
long time = (end - start)/1000;
System.out.println("获取连接时长为 --->"+time);
Collection coll = new Vector();
String s_size = pro.getProperty("batchSize");
int size = new Integer(s_size).intValue();
int c_size = col.size();
Iterator iter = col.iterator();
String sql = "insert into dt values(?)";
PreparedStatement pstmt = con.prepareStatement(sql);
while(iter.hasNext()){
coll.add(iter.next());
if(coll.size() == size){
//System.out.println("每次写入数据库的大小 ---"+size);
con.setAutoCommit(false);
Iterator it = coll.iterator();
while(it.hasNext()){
BIDR bidr = (BIDR)it.next();
pstmt.setString(1,bidr.getLoginName());
pstmt.addBatch();
}
pstmt.executeBatch();
con.commit();
coll.clear();
}
}
//System.out.println("剩下的数据大小 ---"+coll.size());
Iterator itr = coll.iterator();
con.setAutoCommit(false);
while(itr.hasNext()){
BIDR br = (BIDR)itr.next();
pstmt.setString(1,br.getLoginName());
pstmt.addBatch();
}
pstmt.executeBatch();
con.commit();
coll.clear();
System.out.println("成功插入数据库,总数量---"+col.size());
col.clear();
log.writeDebug("成功将数据插入数据库!");
}else{
System.out.println("入库数据量为零!");
}
}
public Connection getConnection()throws Exception{
Class.forName(pro.getProperty("driver"));
Connection con = DriverManager.getConnection(pro.getProperty("url"),pro.getProperty("username"),pro.getProperty("password"));
return con;
}
public void setLog(Log log){
this.log = log;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -