开发者

Displaying result set dynamically

   String query="select dept_id,deptName from dept";
    PreparedStatement ps=cnn.prepareStatement(query);
    rs=ps.executeQuery();    
    while(rs.next()){           
    String st1 = rs.getString(1);
    String str2= rs.getString(2);
    }       
    ps.close();        

I made one method for each insert, update, delete queries that could be used around the project. In the same way I am trying to develop one select method that could get any query and return the results. I tried ArrayList to retriew the record and now its working but displaying records here there not in a sequence. The code is as follow: public ArrayList selectQuery( String columNames,String tableNames, String whereClause,String whereValues)throws Exception{

ArrayList<ArrayList> resultList = new ArrayList<ArrayList>();        
    ArrayList<String> resultRecord = null;  

try{
    if ((cnn==null)||cnn.isClosed()){
    cnn=DB.getOracleConnection();
    }

    if(whereClause==null || whereClause.equals("")){
        whereClause="";
    }

    String query = "select "+columNames+" from "+tableNames+" "+whereClause;

            PreparedStatement ps=cnn.prepareStatement(query); 

            if(whereValues==null || whereValues.equals("")){
    //do nothing
    }
    else{
        int j=1;
        String[] words = whereValues.split (",");
        for (int i=0; i < words.length; i++){   
            words[i]=removeDot(whereValues);
            ps.setString(j, words[i]);
            j+=1;
        }
    }//whereValuesNullCheck

            rs=ps.executeQuery();     
            ResultSetMetaData rsmd = rs.getMetaData();     

            while(rs.next()){                           

        resultRecord= new ArrayList<String>(); 

                    for(int i=1;i<=rsmd.getColumnCount();i++){        
                    resultRecord .add(rs.getString(i));   
                    }        
                resultList.add(resultRecord); 
            }     
    ps.close();      

        } catch (Exception e) {   
System.out.println("ERROR: "+e.toString());
SendEmail.send(mail,"raakh5@gmail.com","Problem in ASC.DAOMethods.select()", "+e.printStackTrace()+"); 
        }     

return resultList;

}//methodClosed

and I am calling in the following method

public String viewPatients(ArrayList<ArrayList> result,String option)throws Exception{

PatientDAO pd = new PatientDAO();
DAOMethods dm = new DAOMethods();

String dataSet1="",dataSet2="",dataSet3="",records="",data="",country="",nationality="",source="",center="";

for(ArrayList record : result){

String patientId    =(String) record.get(0);
String rDate        =(String) record.get(1);
String title        =(String) record.get(2);
String firstName    =(String) record.get(3);
String lastName     =(String) record.get(4);
String country1     =(String) record.get(5);
String nationality1     =(String) record.get(6);
String source1      =(String) record.get(7);
String center1      =(String) record.get(8);

String title1 = cm.title(((String)title));
String name     = title1+" "+firstName+" "+lastName;

country         = c.displaySepecificCountry(((String)country1));
nationality         = c.displaySepecificCountry(((String)nationality1));
source          = cm.source(((String)source1));
String centerLocation   = pd.centerLocation(((String)center1));
center          = c.displaySepecificCountry(((String)centerLocation));
int age=9;

dataSet1+=
    "<tr "+cm.rowColor()+">"+
    "<td width=110  height=32 align=center class=normalFont>"+
    "<a href=/servlet/PatientManager?option=5&patientId="+patientId+">"+
    rDate+"</a></td>"+

    "<td width=110  align=center class=normalFont>"+
    "<a href=/servlet/PatientManager?option=5&patientId="+patientId+">"+
    "<font color=blue><b>"+patientId+"</b></font></a></td>";    

dataSet3+=
    "<td height=32 class=normalFont>"+
    "<a href=/servlet/PatientManager?option=5&patientId="+patientId+">"+
    name+"</a></td>"+

    "<td width=75 align=center class=normalFont>"+
    "<a href=/servlet/PatientManager?option=5&patientId="+patientId+"><font color=blue><b>"+
    age+"</b></font></a></td>"+

    "<td width=140 align=center class=normalFont>"+
    "<a href=/servlet/PatientManager?option=5&patientId="+patientId+">"+country+"</a></td>"+

    "<td width=75  align=center class=normalFont>"+
    "<a href=/servlet/PatientManager?option=5&patientId="+patientId+">"+nationality+"</a></td>"+    

    "<td width=75  align=center class=normalFont>"+
    "<a href=/servlet/PatientManager?option=5&patientId="+patientId+">"+source+"</td>"+

    "<td width=45>"+
    "<a href=/servlet/PatientManager?option=6&patientId="+patientId+">"+
    "<img src=/images/edit1.png border=0></a></td>"+
    "</tr>";
}
records=dataSet1+dataSet2+dataSet3;

return records;
}

Its working with the results 开发者_开发知识库fine but displaying records here there like on this link: http://www.onlinenics.com/images/error.jpg


If the comments above are correct in speculating that different result sets get different numbers of columns back, you have to be able to tell which columns have values. Use the names of the columns to get the values. Use an empty string where you have none.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜