2016-04-10 219 views
2

我在Java應用程序的下拉列表中擁有所有表名。 我想要顯示JLabel表中的記錄數。 但我發現了以下錯誤java.sql.SQLSyntaxErrorException:ORA-00903:表名無效

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); 
    } 
+1

控制檯打印表名並檢查它是否存在。 – Naruto

回答

4

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

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

你傳遞字符串作爲表名。嘗試刪除單引號

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