java搜索按钮不适用于我
public Boolean load() {
Connection con = null;
PreparedStatement pre = null;
Boolean isexist = false;
try {
con = DBManager.getConnection();
String sql = "SELECT * FROM contacts WHERE mobile=?";
pre = con.prepareStatement(sql);
pre.setString(1, this.mobile);
ResultSet result = pre.executeQuery();
if (result.next()) {
isexist = true;
}
} catch (Exception e) {
} finally {
if (pre != null) {
try {
pre.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (Exception e) {
}
}
}
return isexist;
}
// and in actionListner `
Contacts objj=new Contacts(this.mobile.getText());
if (objj.load())
{
this.name.setText("njkn");
this.mobile.setText("jbnlj");
this.address.setText("jnl");
this.email.setText("knkl");
} else
{
JOptionPane.showMessageDialog(null," error message");
}
我在JFrame
中创建了4个文本字段,我希望当用户输入mobile num时,单击搜索按钮-显示其他信息(姓名、电子邮件、地址),但它是显示的
# 1 楼答案
您需要将手机号码作为参数传递给load函数,否则无法将其用作搜索参数
您需要以某种方式获取返回的值,可以通过返回,也可以使用字段或smt
您可以对任何可自动关闭的语句(如Connection或PreparedStatement)使用try with resources块
我在代码中添加了更多注释