I need to insert many sql rows into oracle database very fast. IndexData is the class which contains save method to insert into oracle database.
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();
}
}
}
And this is my IndexData class-
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 ();
}
Is this the best way to insert into oracle database having several multiple sql statement for insertion. As I am opening IndexData class again and again for inserting only one row into DB for each information. Is there any other way that is faster than this. Any suggestions will be appreciated..
Have a look at
PreparedStatement.addBatchandPreparedStatement.executeBatch– you can create a single prepared statement, add several sets of parameters for of execution, and then execute the batch. You probably want to choose some number of inserts per batch though, limiting the amount of “pending” work building up in memory.