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); }
Advertisement
Answer
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);