good morning companions
I have a code that is responsible for processing a .txt file and inserting it into a SQL database; all this in java using netbeans
my code is as follows:
String line = null;
int exito=0;
int numeroBatch=0;
int counter = 0;
DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd");
Date date = new Date();
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, -3);
date = c.getTime();
String fechaBuscar=dateFormat.format(date);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("Tmp\unzip\"+tempRead+".txt"),"iso-8859-1"));
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
cadenaConexion="jdbc:sqlserver://localhost\MSSQLSERVER;databaseName=test";
usuarioDB="test";
passDB="testing123";
Connection con=DriverManager.getConnection(cadenaConexion,usuarioDB,passDB);
Statement stmt=con.createStatement();
boolean empiezaArchivo=false;
while((line = bufferedReader.readLine()) != null) {
counter++;
if(counter >= 4&&!"EOF".equals(line)&&empiezaArchivo!=false)
{
String lineaParsea=line;
String[] data = lineaParsea.split("\|");
System.out.println("generando registro "+(counter-3)+" de archivo "+(tempRead+1)+" para base de datos...");
try
{
con=DriverManager.getConnection(cadenaConexion,usuarioDB,passDB);
String query="insert into datos (dato,fecha_bajada) values('"+data[0]+"','"+fechaBuscar+"')";
stmt.addBatch(query);
if(numeroBatch==2000)
{
stmt.executeBatch();
numeroBatch=0;
stmt=con.createStatement();
}
numeroBatch++;
exito= 1;
}
catch(Exception ex)
{
Logger.getLogger(BajarArchivosYProcesa.class.getName()).log(Level.SEVERE, null, ex);
exito= 0;
}
if(exito==0)
{
break;
}
}
else if("EOF".equals(line))
{
System.out.println("Se termino de procesar el registro del archivo "+(tempRead+1));
stmt.executeBatch();
numeroBatch=0;
stmt=con.createStatement();
break;
}
else
{
//nada... con las primeras lineas no hacemos nada..
String inicio="DATOQUENECESITO|OTRO|OTRO2";
if(line.toLowerCase().contains(inicio.toLowerCase()))
{
empiezaArchivo=true;
}
}
}
As you will see the txt file I have to walk to get only the data I want (the record comes like this, I DO NOT NEED ANOTHER | ANOTHER and I only take the first data) and then I use a batch of 2000 records to process the insert.
The code as such serves me; but the problem is that it takes a lot, the txt file has more than 15 million records, and a total of 5 txt files ; for each file it takes more less a day.
Is there a better way to process the txt and insert them in the SQL database?
Thanks for your support