The example below uses Prepared Statement to fetch data from database.

Java Code:
String id = cust_id.getText();
try {
  PreparedStatement prepstmt;
  boolean found = false;
  prepstmt = theConn.prepareStatement
    ("select custName, CustAddr from tCust where custId = ?");
  prepstmt.setString(1, id); 
      
  ResultSet rs;
  rs = prepstmt.executeQuery();
      
  found = rs.next();
  if (found)
      System.out.println(rs.getString(1));
  else
      System.out.println("Customer " + id + " not found!");
   prepstmt.close();
   }
catch (Exception e) {
   e.printStackTrace();
}