簡體   English   中英

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

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

我在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);
    }

在Oracle中,引號( ' s)用於表示字符串文字。 對象名稱(例如表)不應該被它們包圍。 丟失報價,你應該沒問題:

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

您將字符串作為表名傳遞。 Oracle中的表名可以在``qoutes里面,也可以沒有任何引號。

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

要么

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM