简体   繁体   中英

Retrieve maximum value from database and displays in JTextfield in java eclipse

I want to extract maximum value from database and displays in JTextfield in Java. I tried the code shown below. But I get error _No such column 'Enquiry No'.

But 'Enquiry No' colum name is available in enquiry table

Enquiry No is column name

textField = new JTextField();
textField.addKeyListener(new KeyAdapter() {
    @Override
    public void keyPressed(KeyEvent arg0) {
        try {
            String query = "Select max(`Enquiry No`) from enquiry ";
            PreparedStatement pst = conn.prepareStatement(query);
            //  pst.setString(1,textField.getText());
            ResultSet rs = pst.executeQuery();

            while (rs.next()) {
                textField.setText(rs.getString("Enquiry No"));
            }
        } catch (Exception f) {
            f.printStackTrace();
        }
    }
}

To get max or min , avg , count or any aggregate functions you have to :

  1. put your result as a value and get it like this :
String query="Select max(`Enquiry No`) as max from enquiry ";
    ...
    textField.setText(rs.getString("max"));
  1. Or get the first result like this :
String query="Select max(`Enquiry No`) from enquiry ";
    ...
    textField.setText(rs.getString(1););

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM