gpt4 book ai didi

java - 动态显示结果集

转载 作者:行者123 更新时间:2023-12-01 05:45:22 25 4
gpt4 key购买 nike

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

我为每个插入、更新、删除查询创建了一种可以在项目中使用的方法。以同样的方式,我正在尝试开发一种可以获取任何查询并返回结果的选择方法。我尝试使用 ArrayList 来检索记录,现在它可以工作,但在这里显示记录而不是按顺序。代码如下:公共(public)ArrayList选择查询( 字符串列名,字符串表名, 字符串 whereClause,字符串 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

最佳答案

如果上面的注释正确地推测不同的结果集会返回不同数量的列,那么您必须能够判断哪些列具有值。使用列的名称来获取值。在没有空字符串的地方使用空字符串。

关于java - 动态显示结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6086918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com