Read data from text file store in mysql

/*
@Author: M. Lakshmikar Reddy
@Designation: Software Engineer
@Date:22/07/2011
*/

import java.io.*;
import java.sql.*;
import java.util.ArrayList;

/*
@ This is used to read the content form text file or plate file
@ To compile this we must set the classpath to mysql-connector-java-5.0.4-bin.jar file,Bcoz i am using MySQL DB.
# Rules of Text File:
* 1. Must be Text file start with Header with /t distence
* 2. In this example each row of the contain 3 words, and space b/w them is " "
* 3. If in the middle of the file contain empty line it will not insert into db.finally it will print this line no
* 4. Here i am checking given user name is exists or not in db, If exists it will update the record other wise insert the record
* 5. Finally display the how many records are inserted and updated , fail to read lines also.
*/
public class FileRead
{
public static void main(String args[])
{
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream(args[0]);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;//to print entire line on console
ArrayList insertList = new ArrayList(); //to print Inserted records
ArrayList updateList = new ArrayList();//to print updated records
ArrayList errorsList = new ArrayList(); //to print failure read lines
int count=0;//to count the no.of lines
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
count++;
// Print the content on the console
//System.out.println (strLine);
//split the content based on " "
String str[] = strLine.split(" ");
try{
//Load the mysql driver
Class.forName("com.mysql.jdbc.Driver").newInstance();
//Get the connection
Connection con= DriverManager.getConnection("jdbc:mysql://localhost/test","root","root");
Statement stmt = con.createStatement();
// To Check the User_name field in Database if It is exist then it will update other wise it will insert the record.
ResultSet rs = stmt.executeQuery("select USER_NAME from emp where USER_NAME='"+str[0]+"'");
if(rs.next()){
stmt.executeUpdate("update emp set USER_NAME='"+str[0]+"',PASS_WORD='"+str[1]+"',EMP_ID='"+str[2]+"' where USER_NAME='"+str[0]+"' ");
//add the updated records usernames into arrayList
updateList.add(str[0]);
}else{
stmt.executeUpdate("INSERT INTO emp(USER_NAME,PASS_WORD,EMP_ID) VALUES('"+str[0]+"','"+str[1]+"','"+str[2]+"')");
//add the Inserted records usernames into arrayList
insertList.add(str[0]);

}
}catch(Exception e){
errorsList.add(count);
e.printStackTrace();
System.err.println("Error: " + e.getMessage());
}

}
//Print the arraylists
System.out.println("Inserted List:"+insertList);
System.out.println("Updated List:"+updateList);
System.out.println("Fail to read Lines:"+errorsList);
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}

No comments: