简体   繁体   中英

Java jdbc SQLException in mysql

When I click my previous button, my textfields are not populated with the values from the DB. It gives me the error of java.sql.SQLException:

Column 'PassengerName' not found.

The column PassengerName exists in my DB. Can someone tell me what is wrong please?

Here's my code:

public PaymentForm() {

    try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection(url, user, password);
        stt = con.prepareStatement("SELECT * FROM payment");

        rs = stt.executeQuery();

        JOptionPane.showMessageDialog(null, "Connection Successful to Database", "Success", JOptionPane.INFORMATION_MESSAGE);

    }catch(Exception e) {
        JOptionPane.showMessageDialog(null, "Error in connecting to db", "Error", JOptionPane.ERROR_MESSAGE);
    }

    initialize();
    }
}

    JButton btnPrev = new JButton("Previous");
    btnPrev.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

             try {
             if (rs.previous()) {

                    pass.setSelectedItem(rs.getString("PassengerName"));

                    int d = rs.getInt("InvoiceNo");
                    in.setText(String.valueOf(d));
                    int d2 = rs.getInt("CardNo");
                    cn.setText(String.valueOf(d2));
                    double pr = rs.getDouble("Price");
                    tp.setText(String.valueOf(pr));

                    nc.setText(rs.getString("Name"));
             }else {
                 rs.next();
                 JOptionPane.showMessageDialog(null, "No more records");
             }

             }catch(Exception e1) {
                    JOptionPane.showMessageDialog(null, "Error");
                    e1.printStackTrace();

Your query doesn't include that column...when you do getString on the resultset you will get the error.

As a side note: you can read column values from ResultSet using directly getString(...) even if the underlying type isn't a string

SELECT PassengerName FROM payment

try to execute the above statement in mysql directly and see if this give you result

  1. Don't use query with * selector in code like 'SELECT * FROM payment' , just list all needed column like ' 'SELECT ID, PassengerName FROM payment' '.
  2. Make sure that table payment has column named 'PassengerName'

I have to create an update table method (). In the method I select * from the table. Then I call the method after the initialise method ().

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