Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

reading a text document




I have following text data in a pipe | delimitted text file

999|5555|6666|PPP|9/9/2009 15:49|1444-AAA_111|FINISH
998|5554|6666|PPP|9/9/2009 15:49|1444-AAA_111|FINISH
997|5553|6666|PPP|9/9/2009 15:49|1444-AAA_111|FINISH
996|5552|6666|PPP|9/9/2009 15:49|1444-AAA_111|FINISH
I am reading above data into String array called rows.

Then i am calling following method like below from main method
      checkDatabases(rows);

The implementation code of checkDatabases(rows) is as below



private static void checkDatabases(String[] rows) {
System.out.println("rows.length : "+rows.length);
//above line printing 4 correctly as there are four lines in the text file
                  String username="";//this is first cell value of each line of the text
                    String password="";//this is second cell value of each line of the text
            PreparedStatement pstmt = null;      
            List<HashMap<String,String>> missingList = new ArrayList<HashMap<String,String>>();




// i need to manipulate array of 4*6 I believe here to read the values specifically first and second value to pass to below query(as username and password) to check againest database which I am not sure how to do it. ???



try{
                        Class.forName("com.mysql.jdbc.Driver").newInstance();
                        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
                        Statement stmt = con.createStatement();


                        String usernameColumn = genericImporterProp.getString("usernameColumn");
                        String passwordColumn = genericImporterProp.getString("passwordColumn");
                        ResultSet rs1 = stmt.executeQuery("select * from login where "  + usernameColumn + "='"+username+"' and " + passwordColumn + "='"+Double.parseDouble(stringCellValue)+"'");


                        boolean isExist = false;
                        if (rs1!=null) {

                              while (rs1.next()){
                                    isExist=true;
                                    String rec1 = rs1.getString(1);
                                    String rec2 = rs1.getString(2);
                                    System.out.println("rec1 is--"+rec1+"---rec2 is---"+rec2);
                              }
                        }

                        if (!isExist) {
                              HashMap<String,String> mapDetail =new HashMap<String,String>();
                              mapDetail.put("username",username);
                              mapDetail.put("password",password);




                              for(HashMap<String,String> newMapDetail : missingList) {
                                    String myUseridToPass = newMapDetail.get("username");
                                    String myPasswordToPass = newMapDetail.get("password");
                                    System.out.println("myUseridToPass-->"+myUseridToPass);
                                    System.out.println("myPasswordToPass-->"+myPasswordToPass);
                                    
                              }


                              missingList.add(mapDetail);

                        }


                        //pstmt.close();
                        con.close();
                  }
                  catch(Exception e){

                        System.out.println("e-->"+e);
                  }
            }

            System.out.println("Missing List : "+missingList);

            generateCsvFile("c://MissingList.csv", missingList);
            sendErrorEmail("c://MissingList.csv");

      }


Preferably without using java 1.5 generics is much good as server is running on java 1.4.
Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

I'd probably handle the file something like:

import java.io.*;

import java.util.*;


public class ReadIt {
    public static void main(String[] args) throws Exception {
        Scanner s = new Scanner(new File("data.txt"));

        while (s.hasNextLine()) {
            String[] creds = s.nextLine().split("\\|", 3);
            ReadIt.process(creds);
        }

        s.close();
    }

    static void process(String[] row) {
        System.out.printf("username=%s,password=%s\n", row[0], row[1]);
    }
}

Open in new window

Avatar of gudii9

ASKER

i cannot use scanner as server running on java 1.4 not java 1.5. please advise
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial