我正在Eclipse中执行一个项目。我的导航按钮运行良好。不过,我的表单中还有JTable
来执行搜索,我可以显示从Jtable
到Jtextfields
的记录。问题是,在我执行搜索并点击任何导航按钮后,它会显示"没有更多记录"。
这是我的搜索代码:
txtsearch = new JTextField();
txtsearch.setBounds(723, 85, 150, 25);
add(txtsearch);
txtsearch.addKeyListener(new KeyAdapter(){
public void keyReleased(KeyEvent e){
String Query3 = "Select * from customer where CustCompanyName = ?";
try {
stt = con.prepareStatement(Query3);
stt.setString(1, txtsearch.getText());
rs = stt.executeQuery();
table2.setModel(DbUtils.resultSetToTableModel(rs));
} catch (SQLException e1) {
e1.printStackTrace();
}
}
});
这是我在文本字段中显示的代码:
table2.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
try {
int Row = table2.getSelectedRow();
String ccode = (table2.getModel().getValueAt(Row, 0).toString());
String Query4 = "Select * from customer where CustomerCode = '"+ccode+"'";
stt = con.prepareStatement(Query4);
rs=stt.executeQuery();
while(rs.next()) {
txtcode.setText(rs.getString("CustomerCode"));
txtcompname.setText(rs.getString("CustCompanyName"));
txtaddress.setText(rs.getString("CustAddress"));
txtpnumber.setText(rs.getString("CustPhoneNumber"));
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
});
答案 0 :(得分:0)
您的程序逻辑未正确实现。
这里table2结果集可能有100行。
String Query3 = "Select * from customer where CustCompanyName = ?";
try {
stt = con.prepareStatement(Query3);
stt.setString(1, txtsearch.getText());
rs = stt.executeQuery();
table2.setModel(DbUtils.resultSetToTableModel(rs));
这里table2结果集可能只有1行。
String ccode = (table2.getModel().getValueAt(Row, 0).toString());
String Query4 = "Select * from customer where CustomerCode = '"+ccode+"'";
stt = con.prepareStatement(Query4);
rs = stt.executeQuery();
如果更改结果集rs
,分配给table2的所有行都会突然消失
假设resultset
现在只有one row
导航按钮是多余的。
如果您不再希望显示消息"不再显示记录"。
next
和previous
等所有导航按钮。while(rs.next()){
如果结果有很多行
为什么一遍又一遍地填写所有JTextField()
?
只有最后一行/ CustomerCode位于txtcode
!!
一个解决方案使用rs.next()
而if
代替while
。
if (rs.next()){
txtcode.setText(rs.getString("CustomerCode"));
txtcompname.setText(rs.getString("CustCompanyName"));
txtaddress.setText(rs.getString("CustAddress"));
txtpnumber.setText(rs.getString("CustPhoneNumber"));
}
在您的代码中填写文本字段只有一个事件。
table2.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
....
如果通过next or prior
等导航按钮更改了数据集光标,则必须还有一个事件。
<强>解决方案强>
String Query3 = "Select * from customer where CustCompanyName = ?";
try {
stt = con.prepareStatement(Query3);
stt.setString(1, txtsearch.getText());
rsCustCompany = stt.executeQuery();
table2.setModel(DbUtils.resultSetToTableModel(rsCustCompany));
String ccode = (table2.getModel().getValueAt(Row, 0).toString());
String Query4 = "Select * from customer where CustomerCode = '"+ccode+"'";
stt = con.prepareStatement(Query4);
rsCustomerCode = stt.executeQuery();