简体   繁体   English

java.sql.SQLSyntaxErrorException:ORA-00903:表名无效

[英]java.sql.SQLSyntaxErrorException: ORA-00903: invalid table name

I have all table names in a drop down list in a java application. 我在java应用程序的下拉列表中有所有表名。 I want display the number of records in a table on JLabel. 我想在JLabel上显示表中的记录数。 but I'm getting the following error 但我收到以下错误

java.sql.SQLSyntaxErrorException: ORA-00903: invalid table name java.sql.SQLSyntaxErrorException:ORA-00903:表名无效

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. 在Oracle中,引号( ' s)用于表示字符串文字。 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. Oracle中的表名可以在``qoutes里面,也可以没有任何引号。

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

or 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM