packagecom.briup.woss.Server;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.Timestamp;importjava.util.ArrayList;importjava.util.Collection;importjava.util.List;importjava.util.Properties;importcom.briup.util.BIDR;importcom.briup.util.BackUP;importcom.briup.util.Configuration;importcom.briup.util.Logger;importcom.briup.woss.ConfigurationAWare;importcom.briup.woss.server.DBStore;public class DBStoreImpl implementsDBStore,ConfigurationAWare{/** driver=oracle.jdbc.driver.OracleDriver
* url=jdbc:oracle:thin:@localhost:1521:XE username=oracle password=orcle*/
private staticString driver;private staticString url;private staticString username;private staticString password;private staticConnection connection;privateString aaa_login_name;privateString login_ip;privatejava.sql.Date login_date;privatejava.sql.Date logout_date;privateString nas_ip;privateString time_duration;private inti_date;private static PreparedStatement[] ps = new PreparedStatement[31];;
String sql;static int i = 0;private static String pathName = "src/com/briup/woss/File/list.txt";
Configuration conf=null;//存放完整数据
static List list = new ArrayList<>();
@Overridepublic voidinit(Properties p) {
driver=p.getProperty("driver");
url=p.getProperty("url");
username=p.getProperty("username");
password=p.getProperty("password");
}
@Overridepublic void saveToDB(Collection collction) throwsException {
BackUP bi=conf.getBackup();
Logger log=conf.getLogger();try{//注册驱动
Class.forName(driver);//创建连接
connection =DriverManager.getConnection(url,username,password);
System.out.println(connection);for (int i = 0; i < 31; i++) {
sql= "insert into t_detail_" +i+ "(aaa_login_name,login_ip,login_date,logout_date,nas_ip,time_duration) "
+ "values(?,?,?,?,?,?)";
ps[i]=connection.prepareStatement(sql);
}//connection最多创建300个prepareStatement//将ps = connection.prepareStatement(sql);放在下面的for (BIDR bidr : collction)里面遍历,由于遍历次数过多,会出现超出游标最大值异常//遍历31次,与下面遍历bidr对象时,存储信息时的ps相对应
for(BIDR bidr : collction) {
Timestamp login_d=bidr.getLogin_date();
String s_date=login_d.toString();
String[] str1= s_date.split(" ");
String[] str2= str1[0].split("-");
i_date= Integer.parseInt(str2[2]);
aaa_login_name=bidr.getAAA_login_name();
login_ip=bidr.getLogin_ip();
login_date= newjava.sql.Date(bidr.getLogin_date().getTime());
logout_date= newjava.sql.Date(bidr.getLogout_date().getTime());//通过PreparedStatement将信息存储到数据库中
ps[i_date].setString(1, aaa_login_name);
ps[i_date].setString(2, login_ip);
ps[i_date].setDate(3, login_date);
ps[i_date].setDate(4, logout_date);
ps[i_date].setString(5, nas_ip);
ps[i_date].setString(6, time_duration);//执行sql
ps[i_date].executeUpdate();
i++;if (i == 200) {
i= 1 / 0;
}
list.add(bidr);
System.out.println("插入数据成功!");
}
log.info("入库数据的个数" +list.size());
collction.removeAll(list);
log.info("未入库数据的个数" +collction.size());
}catch(Exception e) {
connection.rollback();
bi.store(pathName, list, BackUP.STORE_OVERRIDE);//对象.字段会出现黄色波浪线,可以改成类名.字段
log.debug("备份数据为"+list.size());
log.debug("未入库数据的个数" +collction.size());
log.debug("插入个数为:"+i);
}
}/*public static void main(String[] args) throws Exception {
GatherImpl ga = new GatherImpl();
new DBStoreImpl().saveToDB(ga.gather());
System.out.println("备份数据的个数为:" + list.size());
System.out.println(i);
}*/@Overridepublic voidsetConfiguration(Configuration co) {//TODO Auto-generated method stub
this.conf=co;
}
}