1

I am trying to find max no from tr_id (primary key) in transaction table.
Here is the table and it's layout.
enter image description here


enter image description here


Here is my cord.

             try {
                ResultSet rs = db.getData("SELECT MAX(tr_id) FROM transaction");
                ResultSetMetaData meta = rs.getMetaData();
                for (int index = 1; index <= meta.getColumnCount(); index++) {
                    System.out.println("Column " + index + " is named " +    meta.getColumnName(index));
                }
                if (rs.first()) {
                    int tr_id = rs.getInt("tr_id");
               }


I'm using JDBC connection. When I'm running this cord I'm getting this error.

        Column 1 is named MAX(tr_id)
        java.sql.SQLException: Column 'tr_id' not found.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
    at com.mysql.jdbc.ResultSet.findColumn(ResultSet.java:955)
    at com.mysql.jdbc.ResultSet.getInt(ResultSet.java:2570)
    at Controler.InvoiceFinalising.saveInvoice(InvoiceFinalising.java:57)
  etc..


The thing is when I'm searching out as "tr_id" column name goes to Max(tr_id)

1 Answer 1

4

This is because the column name in your sql query is max(tr_id). You can write it as

ResultSet rs = db.getData("SELECT MAX(tr_id) as tr_id FROM transaction");

now you will be able to get it.

Sign up to request clarification or add additional context in comments.

1 Comment

is that like this? db.putData("INSERT INTO invoice (inid,itid, cuid, itname, qty, sprice, total, isdate, redate,tr_id) VALUES ('" + txtInvoiceID.getText() + "','" + itemid + "', '" + txtCuID.getText() + "', '" + name + "','" + qty + "', '" + up + "', '" + total + "', '" + date1.trim() + "', '" + date2.trim() + "', '" + rs.getInt("tr_id") + "') "); now I'm getting tr_id not found error again. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.