Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
    }
like image 289
Mehroz Irshad Avatar asked Mar 12 '23 15:03

Mehroz Irshad


1 Answers

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);
like image 199
Mureinik Avatar answered Mar 15 '23 15:03

Mureinik