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);
}
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);
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 + "`");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.