Can any one help me in generalizing the mysql query
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Pramy_143
Posted On:   Wednesday, September 16, 2009 04:07 AM

hi, please can any one help me in generalizing the mysql query for getting result set, here i am passing values to DBManager.java and getting the result in jsp page this code works for individual field but i need to perform generalized query means select * from table name; Record rec=new Record(); rec.setTableName("group_master"); ArrayList fldNameList = new ArrayList (); fldNameList.add("group_id"); ArrayList fldNameQueryList = new ArrayList (); fldNameQueryList.add("group_name"); ArrayList fldTypeQueryList = new ArrayList (); fldTypeQueryList.add("string"); ArrayList    More>>

hi,
please can any one help me in generalizing the mysql query for getting result set, here i am passing values to DBManager.java and getting the result in jsp page this code works for individual field but i need to perform generalized query means select * from table name;


			




Record rec=new Record();
rec.setTableName("group_master");
ArrayList fldNameList = new ArrayList ();
fldNameList.add("group_id");

ArrayList fldNameQueryList = new ArrayList ();
fldNameQueryList.add("group_name");

ArrayList fldTypeQueryList = new ArrayList ();
fldTypeQueryList.add("string");

ArrayList fldValueQueryList = new ArrayList ();
fldValueQueryList.add("General");

rec.setFieldNameList(fldNameList);
rec.setQueryFieldNameList(fldNameQueryList);
rec.setQueryFieldTypeList(fldTypeQueryList);
rec.setQueryFieldValueList(fldValueQueryList);

DBManager sel=new DBManager();
sel.obtainConnection();
System.out.println("RETURNING...");
ArrayList list=sel.selectRecords(rec);
System.out.println("list.size..."+list.size());
for (int i=0;i
{
System.out.println("Inside FOR lop");
Hashtable tbl = (Hashtable)list.get(i);
System.out.println("Inside FOR lop22222222222"+tbl.get("group_id"));
String tblGrpName=(String)tbl.get("group_id");
System.out.println("GrpName is "+tblGrpName);

}


and my java file has
public ArrayList selectRecords(Record rec)
{
java.sql.Statement stmt=null;
ArrayList recList=new ArrayList ();
try
{
System.out.println("INside selectRecords");
String tblName=rec.getTableName();
ArrayList fldNameList=rec.getFieldNameList();
ArrayList qryFldNameList=rec.getQueryFieldNameList();
ArrayList qryFldTypeList=rec.getQueryFieldTypeList();
ArrayList qryFldValueList=rec.getQueryFieldValueList();
System.out.println("after array list selectRecords");
String sql="SELECT ";
sql=formSQLStringPart1(sql,fldNameList);
sql+=" FROM "+tblName +" WHERE ";
sql=formSQLStringPart2(sql,qryFldNameList,qryFldTypeList,qryFldValueList);
System.out.println(sql);
stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
int recCount=0;
System.out.println("Resultset Executed......");
while (rs.next())
{
System.out.println("recCount="+fldNameList.get(recCount));
String fName=fldNameList.get(recCount);
System.out.println(fName);
Hashtable tuple = new Hashtable ();
String val=rs.getString(fName);
tuple.put(fName,val);
System.out.println("Key-"+fName +"######## Value-"+val);
recList.add(tuple);
recCount++;
}
System.out.println("Resultset Execution OVer......");
}


catch (Exception e)
{
e.printStackTrace();
}

finally{
try{
stmt.close();
con.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
System.out.println("Returning List size "+recList.size());
return recList;
}

private String formSQLStringPart1(String sql,ArrayList fldNameList)
{
try
{
for(String fldName:fldNameList)
{
sql += fldName+",";
}
sql=sql.substring(0,sql.length()-1);
return sql;
}
catch (Exception e)
{
e.printStackTrace();
return "";
}
}
   <<Less
About | Sitemap | Contact