我用JAVA连接数据库后想做个查询,结果显示在表格或文本框中,但未能实现!需要实现结果如下:
部份代码如下:说明,这个是用文本框显示,结果总是出现:com.ibm.db2.jcc.a.h@1ddebc3
public void widgetSelected(SelectionEvent arg0) {
String inPut = text_1.getText();
String sqlquery = "";
sqlquery = "select mc from dj_zt where mc like '%"+inPut+"%'";
try {
Connection db2Conn = getDB2Connection();
Statement stmt = db2Conn.createStatement();
String result = stmt.executeQuery(sqlquery).toString();
text.append(result);
} catch (Exception e) {
// TODO: handle exception
}
}
});
请说明出现以上问题原因!如果能把结果显示到表格中更好,谢谢各位!
部份代码如下:说明,这个是用文本框显示,结果总是出现:com.ibm.db2.jcc.a.h@1ddebc3
public void widgetSelected(SelectionEvent arg0) {
String inPut = text_1.getText();
String sqlquery = "";
sqlquery = "select mc from dj_zt where mc like '%"+inPut+"%'";
try {
Connection db2Conn = getDB2Connection();
Statement stmt = db2Conn.createStatement();
String result = stmt.executeQuery(sqlquery).toString();
text.append(result);
} catch (Exception e) {
// TODO: handle exception
}
}
});
请说明出现以上问题原因!如果能把结果显示到表格中更好,谢谢各位!
public void widgetSelected(SelectionEvent arg0) {
String inPut = text_1.getText();
String sqlquery = "";
sqlquery = "select mc from dj_zt where mc like '%"+inPut+"%'";
try {
Connection db2Conn = getDB2Connection();
Statement stmt = db2Conn.createStatement();
RecordSet rs = stmt.executeQuery(sqlquery);//这里返回的是记录集
text.append(rs.getString("mc"));//如果mc是一个String字段的话,通过getString方法获取值 } catch (Exception e) {
// TODO: handle exception
}
}
});
我再添点
public List widgetSelected(SelectionEvent arg0) { //在jsp,servlet或main方法里调用都可以获得结果集
String inPut = text_1.getText();
String sqlquery = "";
List list=new ArrayList();
sqlquery = "select mc from dj_zt where mc like '%"+inPut+"%'";
try {
Connection db2Conn = getDB2Connection();
Statement stmt = db2Conn.createStatement();
RecordSet rs = stmt.executeQuery(sqlquery);//这里返回的是记录集
while(rs.next){
list.add(rs.getString("mc"));//把查询结果放到集合中
}
} catch (Exception e) {
// TODO: handle exception
}
return list;
}
}