非常快速地向Oracle数据库插入许多行

ars*_*nal 0 java sql oracle jdbc

我需要非常快速地将许多sql行插入到oracle数据库中.IndexData是包含要插入到oracle数据库的save方法的类.

while ((line = in.readLine()) != null) {
    if(line.contains("numDocs")) {
        numDocs = in.readLine().trim();
//For Inserting 
IndexData id = new IndexData(timeStamp, 1, 2, numcDocs);
id.save();
    }  else if(line.contains("indexSize")) {
        indexSize = in.readLine().trim();
//For Inserting 
IndexData id = new IndexData(timeStamp, 1, 3, indexSize);
id.save();
        } else if(line.contains("indexReplicatedAt")) {
   replicationTime = in.readLine().trim();           
//For Inserting         
IndexData id = new IndexData(timeStamp, 1, 4, replicationTime );
id.save();
    } 
    }

    BufferedReader inUrl   = new BufferedReader (new InputStreamReader (isUrl));
    String lineUrl;
    Pattern regex = Pattern.compile("<str name=\"status\">(.*?)</str>");

    while ((lineUrl = inUrl.readLine()) != null) {
    if(lineUrl.contains("str name=\"status\"")) {
        Matcher regexMatcher = regex.matcher(lineUrl);
        if (regexMatcher.find()) {
        status = regexMatcher.group(1);
//For Inserting
IndexData id = new IndexData(timeStamp, 1, 5, status);
id.save();
        }                   


}  
}
Run Code Online (Sandbox Code Playgroud)

这是我的IndexData类 -

public IndexData(String timestamp, String serveId, String informationId, String value ) {
this.timestamp=timestamp;
this.serverId=serverId;
this.informationId=informationId;
this.value=value;
}
//For Inserting
public void save() throws SQLException {

ps = DataSource.conn.prepareStatement (
"INSERT INTO table(timestamp, serverId, informationId, value) VALUES(?,?,?,?)");

ps.setString (1, timestamp);
ps.setString (2, serverId);
ps.setString (3, informationId);
ps.setString (4, value);

ps.executeUpdate();
ps.close ();
}
Run Code Online (Sandbox Code Playgroud)

这是插入到具有多个多个sql语句以进行插入的oracle数据库的最佳方式.因为我一次又一次地打开IndexData类,因为每个信息只向DB插入一行.有没有比这更快的其他方式.任何建议将不胜感激..

Jon*_*eet 6

看看PreparedStatement.addBatch并且PreparedStatement.executeBatch- 您可以创建一个预准备语句,添加几组执行参数,然后执行批处理.您可能希望每批选择一定数量的插入,限制在内存中构建的"待处理"工作量.