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