【问题标题】:java.sql.SQLSyntaxErrorException: ORA-00903: invalid table namejava.sql.SQLSyntaxErrorException: ORA-00903: 无效的表名
【发布时间】:2016-07-31 08:01:48
【问题描述】:

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

【问题讨论】:

  • 控制台打印表名并检查是否存在。

标签: java sql oracle jdbc syntax-error


【解决方案1】:

在 Oracle 中,引号 ('s) 用于表示字符串文字。对象名称(例如表格)不应被它们包围。丢掉引号,你应该没问题:

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

【讨论】:

    【解决方案2】:

    您将字符串作为表名传递。 Oracle 中的表名可以在 ``qoutes 内或不带任何引号。

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

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

    【讨论】:

      猜你喜欢
      • 2016-12-19
      • 1970-01-01
      • 1970-01-01
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-16
      相关资源
      最近更新 更多