您的位置:首页 > 数据库

自己写的一个使用jdbc prepare存储数据的例子

2014-04-13 15:34 411 查看
public class TestSqlite {
public static void main(String []args)throws Exception{
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:D:\\cdkey.db");
Statement stat = conn.createStatement();
File file = new File("C:\\Users\\Administrator\\Desktop\\cdkey");
String tableName="cdkeys";
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file));
BufferedReader reader = new BufferedReader(new InputStreamReader(fis,"utf-8"),5*1024*1024);// 用5M的缓冲读取文本文件
int count=0;
PreparedStatement ps = null;
String sql = "insert into "+tableName+" values (?)";
ps=conn.prepareStatement(sql);
long startTime=System.currentTimeMillis();
try{
while(true){
++count;
String str=reader.readLine();
if(str==null) break;
conn.setAutoCommit(false);
ps.setString(1,str);
ps.addBatch();
if(count%100000==0){
conn.commit();
}
ps.executeBatch();
ps.clearBatch();
}
}
catch(SQLException e){
e.printStackTrace();
try{
if(!conn.isClosed()){
conn.rollback();
System.out.println("提交失败,回滚!");
conn.setAutoCommit(true);
}
}catch (SQLException e1){
e1.printStackTrace();
}
finally {
conn.close();

}

}
conn.setAutoCommit(true);
long endTime=System.currentTimeMillis();
System.out.println(endTime-startTime);
System.out.print(count);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sqlite
相关文章推荐