I am writing a script wherein I am trying to update row values in SQL table which has millions of records. I have written the Update query using regex replace as I need to replace the string to a new string and update the row. So my questions are:
Can I use this update query using regex replace directly to update the records?
What is the difference in selecting few records in batches and bulk updating them?
Can someone provide me with a better solution where I can keep track of records updated, if for some reason update stops / script stops and can take user input based on which I can trigger the script time to be run between two timestamps?
Please help me with the script which basically updates the records in the best/fastest way possible keeping track of the records updated so far and can be run between specified timestamp.
PS: The script has to be created for JAVA-SQL project.
Answer: I worked on the following script. Thought to post it here so that it might be helpful for others. The following project can be converted to jar file and arguments can be passed.
package com.jcg.jdbc.batch.example;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
public class App {
// static String DB_DRIVER="com.mysql.jdbc.Driver" ;
// static String DB_CONNECTION="jdbc:mysql://127.0.0.1:3306/tablename?" + "useUnicode=true&characterEncoding=UTF-8" +
// "&rewriteBatchedStatements=true";
// static String DB_USER="root";
// static String DB_PASSWORD="root";
public static void main(String[] args) {
App app = new App();
try {
String DB_DRIVER="com.mysql.cj.jdbc.Driver" ;
String DB_CONNECTION=args[0] + "?useUnicode=true&characterEncoding=UTF-8" +
"&rewriteBatchedStatements=true";
String DB_USER=args[1];
String DB_PASSWORD=args[2];
Connection conn = ConnectionObject.getConnection(DB_DRIVER,DB_CONNECTION,DB_USER, DB_PASSWORD);
app.batchUpdateUsingPreparedStatement(args, conn);
//app.batchUpdateUsingStatement();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void batchUpdateUsingPreparedStatement(String[] args, Connection conn) throws SQLException{
int[] result=null;
Date datecreated = null;
Date datecreatedTill = null;
String created = args[3];
//String createdTill = args[4];
// String created = "2016-06-06 17:09:29";
// String createdTill = "2016-07-11 17:09:29";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
datecreated = format.parse(created);
Calendar cal = Calendar.getInstance();
cal.setTime(datecreated);
System.out.println(datecreated.getHours());
cal.set(Calendar.HOUR_OF_DAY, datecreated.getHours());
System.out.println(format.format(cal.getTime()));
cal.add(Calendar.MINUTE, Integer.parseInt(args[4]));
System.out.println(format.format(cal.getTime()));
String createdTill = format.format(cal.getTime());
System.out.println(createdTill);
datecreatedTill = format.parse(createdTill);
//datecreatedTill = format.parse(createdTill);
System.out.println(datecreated.toString());
System.out.println(datecreatedTill.toString());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String SQL="UPDATE query where created >= ? and created < ?";
try {
PreparedStatement stmt = conn.prepareStatement(SQL);
conn.setAutoCommit(false);
stmt.setDate(1, new java.sql.Date(datecreated.getTime()));
stmt.setDate(2, new java.sql.Date(datecreatedTill.getTime()));
stmt.addBatch();
System.out.println("Script start time: "+ new java.util.Date());
result = stmt.executeBatch();
conn.commit();
} catch (SQLException e) {
conn.rollback();
e.printStackTrace();
}finally{
if(conn!=null)
conn.close();
}
System.out.println("Number of rows affected: "+ result[0]);
System.out.println("Script end time: "+ new java.util.Date());
}
}
Referred this link for the project: https://examples.javacodegeeks.com/core-java/sql/jdbc-batch-update-example/
Thanks everyone who viewed and still didn't answer. :P