package sqlite;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class testData {
static Connection conn = null;
static PreparedStatement ppsm = null;
public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException, ParseException {
Properties prop = new Properties();
//需要外部属性配置文件的路径
FileInputStream inputStream = new FileInputStream("jdbc.properties");
prop.load(inputStream);
String driverClass = prop.getProperty("driverClass");
String urlOld = prop.getProperty("urlOld");
String urlNew = prop.getProperty("urlNew");
Class.forName(driverClass);
createnewtable();
conn = DriverManager.getConnection(urlOld);
readFile(conn);
/*conn = DriverManager.getConnection(urlNew); readFile(conn);*/
}
public static void readFile(Connection conn) throws ClassNotFoundException, SQLException{
try {
Statement statement = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
statement.setQueryTimeout(30);
//PreparedStatement ps = conn.prepareStatement("SELECT productId,updateRegionId,testId,details FROM failedTestResultTable", ResultSet.CONCUR_READ_ONLY);
ResultSet rst = statement.executeQuery("SELECT productId,updateRegionId,testId,details FROM failedTestResultTable");
rst.setFetchSize(1000);
//ResultSet rst = ps.executeQuery();
String sql = "insert into presentationTest values(?,?,?,?,?)";
conn = DriverManager.getConnection("jdbc:sqlite:presentation.sq3");
//不自动提交
conn.setAutoCommit(false);
//批量提交
ppsm = conn.prepareStatement(sql);
while(rst.next()){
System.out.println();
String productId = rst.getString("productId");
String updateRegionId = rst.getString("updateRegionId");
String testId = rst.getString("testId");
String details = rst.getString("details");
String catnum = "";
//空 = ""
if (productId == null || "".equals(productId)) {
productId = "";
}
if (updateRegionId == null || "".equals(updateRegionId)) {
updateRegionId = "";
}
// 检测details中有无#Cat?
int catPos = details.toLowerCase().indexOf("#cat");
if (catPos != -1) {
//保留#cat后面的字符串
String first = details.substring(catPos + 4, details.length());
//寻找#cat后面第一次出现字母的位置
Pattern p = Pattern.compile("[A-Za-z]");
Matcher m = p.matcher(first);
if (m.find()) {
//m.start()位cat后面第一次出现字母的位置,并切割
String resultString = first.substring(0, m.start());
//去掉切割后的空格和英文标点符号
catnum = Pattern.compile("[.,\"\\?!:' //s//p{Zs}]").matcher(resultString).replaceAll("");
}
}
ppsm.setString(1, productId);
ppsm.setString(2, updateRegionId);
ppsm.setString(3, testId);
ppsm.setString(4, details);
ppsm.setString(5, catnum);
ppsm.addBatch();
if(rst.getRow()==10000){//可以设置不同的大小;如50,100,500,1000等等
ppsm.executeBatch();
conn.commit();
ppsm.clearBatch();
}
}
/* //批量执行上面的语句,全部提交
//批量执行预定义SQL,即把以上添加到批处理命令中的所有命令一次过提交给数据库来执行
ppsm.executeBatch();
//Commit it 咽下,到肚子(DB)里面
//前面设置connection.setautocommit(false);只有程序调用connection.commit()的时候才会将先前执行的语句一起提交到数据库
conn.commit();*/
} catch (SQLException e) {
System.err.println(e.getMessage());
} finally {
try {
if(conn != null)
conn.close();
} catch(SQLException e) {
System.err.println(e);
}
};
}
//创建新表
public static void createnewtable() throws ClassNotFoundException, SQLException, ParseException{
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:sqlite:presentation.sq3");
Statement statement = conn.createStatement();
statement.setQueryTimeout(30);
//创建新表
statement.executeUpdate("drop table if exists presentationTest");
statement.executeUpdate("create table presentationTest (productId string, updateRegionId string, testId string, details string,Cat string)");
} catch (SQLException e) {
System.err.println(e.getMessage());
} finally {
try {
if(conn != null)
conn.close();
} catch(SQLException e) {
System.err.println(e);
}
}
}
}