我在java应用程序的下拉列表中有所有表名。 我想在JLabel上显示表中的记录数。 但我收到以下错误
java.sql.SQLSyntaxErrorException:ORA-00903:表名无效
我试过这个:
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);
}
答案 0 :(得分:4)
在Oracle中,引号('
s)用于表示字符串文字。对象名称(例如表)不应该被它们包围。丢失报价,你应该没问题:
pst = (OraclePreparedStatement) con.prepareStatement
("select count(*) as num from " + tableName);
答案 1 :(得分:2)
您正在将字符串作为表名传递。 Oracle中的表名可以在``qoutes里面,也可以没有任何引号。
pst = (OraclePreparedStatement) con.prepareStatement("select count(*) as num from " + tableName );
或
pst = (OraclePreparedStatement) con.prepareStatement("select count(*) as num from `" + tableName + "`");