4

I have all table names in a drop down list in a java application. I want display the number of records in a table on JLabel. but I'm getting the following error

java.sql.SQLSyntaxErrorException: ORA-00903: invalid table name

I have tried this:

try {
        String tableName = LoginFrame.userName + "." +    this.ddlTableName.getSelectedItem().toString();
        JOptionPane.showMessageDialog(null, tableName);
        pst = (OraclePreparedStatement) con.prepareStatement("select count(*) as num from '" + tableName + "'");
        rs = pst.executeQuery();
        while (rs.next()) {
            this.lblRecordStat.setText(rs.getString("num"));
        }
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null, ex);
        System.out.println(ex);
    }
1
  • 1
    console print table name and check if it exists. Commented Apr 10, 2016 at 7:20

2 Answers 2

5

In Oracle, quotes ('s) are used to denote string literals. Object names (such as tables) should not be surrounded by them. Lose the quotes and you should be OK:

pst = (OraclePreparedStatement) con.prepareStatement
          ("select count(*) as num from " + tableName);
Sign up to request clarification or add additional context in comments.

Comments

2

You are passing string as table name. Table names in Oracle can be either inside `` qoutes or without any quotes.

pst = (OraclePreparedStatement) con.prepareStatement("select count(*) as num from " + tableName );

or

pst = (OraclePreparedStatement) con.prepareStatement("select count(*) as num from `" + tableName + "`");

Comments

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.