1

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();
ZygD
  • 22,092
  • 39
  • 79
  • 102
  • Could you share the query as well, or the `Statement` you use for creating `rs`? – Mick Mnemonic Apr 16 '15 at 19:43
  • Check your select SQL query. Make sure u have included PassengerName column in select column list. also check with database table & make sure u don't have any typo in ur select SQL query. – Bacteria Apr 16 '15 at 19:51
  • I have select * from payment ! There's no typo, nothing :/ – Nilesh Bhunjun Apr 16 '15 at 19:54
  • If you comment out the line with `pass.setSelectedItem(...` does everything work okay? I.e. is the problem only occurring on that one column? – Mick Mnemonic Apr 16 '15 at 19:55
  • The problem persists with all the columns ! I have even commented each line, but the problem persists – Nilesh Bhunjun Apr 16 '15 at 19:57
  • Check the column names you're getting from [the resultset metadata](http://stackoverflow.com/a/696794/905488), e.g. using a debugger. – Mick Mnemonic Apr 16 '15 at 20:01
  • @Nilesh Bhunjun I am telling u to check the "PassengerName" which u have used to get the value from resultset and the actual column name in the table are same or not. Could u plz share with us the table structure? – Bacteria Apr 16 '15 at 20:05

4 Answers4

1

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

Andrea
  • 2,714
  • 3
  • 27
  • 38
  • I have the following column names in my table: The exact words. InvoiceNo PassengerName CardNo Name Price – Nilesh Bhunjun Apr 16 '15 at 19:55
  • No not in your table i mean in your SELECT statement. You're not seldcting that column – Andrea Apr 16 '15 at 19:56
  • SELECT (column names) FROM (table) WHERE (condition) but you should already do something like that...the problem is that (column names) doesn't mention PassengerName – Andrea Apr 16 '15 at 20:07
  • Nope. It does not work.. However when I implemented the Next button functionality. Instead of looping over each record, it directly says no more records without displaying anything on my JTextfields. – Nilesh Bhunjun Apr 16 '15 at 20:09
  • It's a bit hard to talk widely in this context...but I see some design issue in your app: a previous or next gui button that directly triggers an iteration over an open resultset? Your client sounds really coupled with the database, and in general this isn't a good thing. – Andrea Apr 17 '15 at 06:34
1

SELECT PassengerName FROM payment

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

Alaa Abuzaghleh
  • 1,023
  • 6
  • 11
1
  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'
Yaroslav Kovbas
  • 430
  • 4
  • 7
0

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 ().