gudii9
asked on
using just arraylist by removing hashmap
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.l ength : "+rows.length);
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,S tring>>();
try{
Class.forName("com.mysql.j dbc.Driver ").newInst ance();
Connection con = DriverManager.getConnectio n("jdbc:my sql://loca lhost:3306 /test","ro ot", "root");
Statement stmt = con.createStatement();
String usernameColumn = genericImporterProp.getStr ing("usern ameColumn" );
String passwordColumn = genericImporterProp.getStr ing("passw ordColumn" );
ResultSet rs1 = stmt.executeQuery("select * from login where " + usernameColumn + "='"+username+"' and " + passwordColumn + "='"+Double.parseDouble(st ringCellVa lue)+"'");
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",u sername);
mapDetail.put("password",p assword);
for(HashMap<String,String> newMapDetail : missingList) {
String myUseridToPass = newMapDetail.get("username ");
String myPasswordToPass = newMapDetail.get("password ");
System.out.println("myUser idToPass-- >"+myUseri dToPass);
System.out.println("myPass wordToPass -->"+myPas swordToPas s);
}
missingList.add(mapDetail) ;
}
//pstmt.close();
con.close();
}
catch(Exception e){
System.out.println("e-->"+ e);
}
}
System.out.println("Missin g List : "+missingList);
generateCsvFile("c://Missi ngList.csv ", missingList);
sendErrorEmail("c://Missin gList.csv" );
}
I would like to enhance my program by just using ArrayList for usernames and passwords to query on databse by getting rid of HashMaps.
I need to cast the objects within the list when I refer to them.
I need to Create a class to hold your username and password strings, and then objects of that class can live in my missingList (the ArrayList).
How can I achieve it.I cannot use java 1.5 either. Only java 1.4 I have to use.
Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
checkDatabases(rows);
The implementation code of checkDatabases(rows) is as below
private static void checkDatabases(String[] rows) {
System.out.println("rows.l
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
try{
Class.forName("com.mysql.j
Connection con = DriverManager.getConnectio
Statement stmt = con.createStatement();
String usernameColumn = genericImporterProp.getStr
String passwordColumn = genericImporterProp.getStr
ResultSet rs1 = stmt.executeQuery("select * from login where " + usernameColumn + "='"+username+"' and " + passwordColumn + "='"+Double.parseDouble(st
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",u
mapDetail.put("password",p
for(HashMap<String,String>
String myUseridToPass = newMapDetail.get("username
String myPasswordToPass = newMapDetail.get("password
System.out.println("myUser
System.out.println("myPass
}
missingList.add(mapDetail)
}
//pstmt.close();
con.close();
}
catch(Exception e){
System.out.println("e-->"+
}
}
System.out.println("Missin
generateCsvFile("c://Missi
sendErrorEmail("c://Missin
}
I would like to enhance my program by just using ArrayList for usernames and passwords to query on databse by getting rid of HashMaps.
I need to cast the objects within the list when I refer to them.
I need to Create a class to hold your username and password strings, and then objects of that class can live in my missingList (the ArrayList).
How can I achieve it.I cannot use java 1.5 either. Only java 1.4 I have to use.
Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That's not the output from the code i asked for
ASKER
I am Here is the exact output and the text file
System.out.println("rows.l ength : "+rows.length);
4
System.out.println(Arrays. toString(r ows));
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
Test.txt
System.out.println("rows.l
4
System.out.println(Arrays.
999|5555|6666|PPP|9/9/2009
998|5554|6666|PPP|9/9/2009
997|5553|6666|PPP|9/9/2009
996|5552|6666|PPP|9/9/2009
Test.txt
It's impossible to call Arrays.toString without causing the output to be preceded by '[' and ended by ']' so i don't see how that can be the outputSystem.out.println(Arrays.toString(rows));
ASKER
public static void main( String [] args ) {
static ResourceBundle genProp = ResourceBundle.getBundle("
String fileTypeNameColumn = genProp.getString("fileTyp
if (fileTypeNameColumn.endsWi
System.out.println("Excel file");
Vector dataHolder=read(fileName);
checkDatabase(dataHolder);
}
else{
try {
String strContent = Storage.retrieveData("C:\\
String [] rows = strContent.split(genProp.g
checkDatabases(rows);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
private static List checkDatabases(String[] rows) {
if(rows!=null){
for (int idx = 0 ; idx < rows.length; idx++)
{
String [] stra = rows[idx].split(genProp.ge
System.out.println("stra.l
//here i have to get some how first two cell vaues of each line of the text attached file by calling some index method which i am not sure then pass to ArrayList without HashMap and query the database using first two values
like
999 5555
998 5554
997 5553
996 5552
999|5555|6666|PPP|9/9/2009
998|5554|6666|PPP|9/9/2009
997|5553|6666|PPP|9/9/2009
996|5552|6666|PPP|9/9/2009
>>>System.out.println("row
>> System.out.println("stra.l
System.out.println("Arrays
is
Arrays.toString(rows)-----
999|5555|6666|PPP|9/9/2009
998|5554|6666|PPP|9/9/2009
997|5553|6666|PPP|9/9/2009
996|5552|6666|PPP|9/9/2009