[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 450
  • Last Modified:

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
0
gudii9
Asked:
gudii9
  • 2
1 Solution
 
CEHJCommented:
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

0
 
gudii9Author Commented:
i cannot use scanner as server running on java 1.4 not java 1.5. please advise
0
 
CEHJCommented:
    public static void main(String[] args) throws Exception {
        BufferedReader in = null;

        try {
            in = new BufferedReader(new FileReader(new File("data.txt")));

            String line = null;

            while ((line = in.readLine()) != null) {
                String[] creds = line.split("\\|", 3);
                ReadIt.process(creds);
            }
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) { /* ignore */
                }
            }
        }
    }

Open in new window

0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now