有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 1 楼答案

    您需要将手机号码作为参数传递给load函数,否则无法将其用作搜索参数

    您需要以某种方式获取返回的值,可以通过返回,也可以使用字段或smt

    您可以对任何可自动关闭的语句(如Connection或PreparedStatement)使用try with resources

    我在代码中添加了更多注释

    // I use hashmap as a return value, the hashmap contains the values from the db
    public static HashMap<String, String> load(String mobile) { // You need to pass the number you're looking for
        String sql = "SELECT email, name, address FROM people WHERE mobile=?"; // You only need to return the columns you need
        try (
                Connection conn = DriverManager.getConnection(LOCATION);
                PreparedStatement pre = conn.prepareStatement(sql)) { // try with ressources so that the connection and preparedstatement close automatically at the end
    
            pre.setString(1, mobile); // enter the passed mobile variable in the query
            ResultSet result = pre.executeQuery();
            if (result.next()) {
                // You need to return the values from the db somehow
                HashMap<String, String> information = new HashMap<>();
                information.put("email", result.getString("email")); // Get the value in the "email" column and store it with the "email" key
                information.put("address", result.getString("address")); // Get the value in the "address" column and store it under "address" key
                information.put("name", result.getString("name")); // Get the value in the "name" column and store it under "name" key
                return information;
    
                // You don't need to check if the values exist. If they don't exist it will just return null and you can check that when you call
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null; // return null if the values don't exist
    }
    
    // Sample method to build GUI
    // I just used this to test everything was working, if you already have a GIU you don't need it
    public void GUI() {
        JFrame frame = new JFrame();
        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
    
        JLabel label = new JLabel("enter mobile number and click search");
        JTextField name = new JTextField("name");
        JTextField mobile = new JTextField("mobile");
        JTextField address = new JTextField("address");
        JTextField email = new JTextField("email");
        JButton button = new JButton("Search");
        button.addActionListener(e -> {
            try { // try in case the method returns null
                HashMap<String, String> information = load(mobile.getText());
                // set the textfields with the returned values
                email.setText(information.get("email"));
                address.setText(information.get("address"));
                name.setText(information.get("name"));
            } catch (NullPointerException ex) { // catch the exception for when the entered number was invalid
                label.setText("Invalid number"); // set error message
            }
        });
        contentPane.add(label);
        contentPane.add(mobile);
        contentPane.add(name);
        contentPane.add(email);
        contentPane.add(address);
        contentPane.add(button);
    
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }