动态显示结果集

时间:2011-05-22 08:50:11

标签: java jdbc

   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();        

我为每个插入,更新,删除可以在项目周围使用的查询创建了一个方法。以同样的方式,我试图开发一个可以获得任何查询并返回结果的select方法。我尝试使用ArrayList来查看记录,现在它正在工作但是在这里显示记录不是按顺序。代码如下: public ArrayList selectQuery(                 String columNames,String tableNames,                 String whereClause,String whereValues)抛出异常{

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

我正在调用以下方法

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;
}

它可以很好地处理结果,但是在这里显示记录就像在这个链接上一样:http://www.onlinenics.com/images/error.jpg

1 个答案:

答案 0 :(得分:0)

如果上述注释在推测不同结果集返回不同数量的列时是正确的,则必须能够确定哪些列具有值。使用列的名称来获取值。使用空字符串,但没有。