-2
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");
}

I created 4 text fields in JFrame and I want when user put the mobile num then click on search button - display other information (name, email, address) but the it is display

slavoo
  • 5,798
  • 64
  • 37
  • 39
mimi
  • 1
  • Review your formatting a bit, you commented part of the code which is kinda confusing, and then you wrote the same sentence 4 times. – Touniouk Nov 21 '17 at 14:07
  • `} catch (Exception e) { }` change that to `} catch (Exception e) { e.printStackTrace(); }` Then see [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) – Andrew Thompson Nov 21 '17 at 14:07

1 Answers1

0

You need to pass the mobile number to the load function as argument, otherwise you can't use it as a parameter for your search.

You need to somehow get the returned values back, either via return or with fields or smt.

You can use try with resources blocks for any autocloseable, like Connection or PreparedStatement.

I added more comments in the code.

// 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);
}
Touniouk
  • 375
  • 1
  • 13