2016-07-29 15 views
0

SQL Serverのテーブル階層を知るWebアプリケーションを開発しています。 次のコードは、データベースに存在するすべてのテーブルのリストを表示します。実際の結果よりも少ない行数を返す結果セット

 String dbServer = session.getAttribute("dbServer").toString(); 
     String dbUsername = session.getAttribute("dbUsername").toString(); 
     String dbPassword = session.getAttribute("dbPassword").toString(); 
     String connectionUrl = session.getAttribute("connectionUrl").toString(); 

     String dbName = session.getAttribute("dbName").toString(); 

     out.println("<h2>Database : " + dbName + "</h2>"); 

     Connection con = null; 
     Statement stmt = null; 
     ResultSet rs = null; 

     try { 
      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
      con = DriverManager.getConnection(connectionUrl, dbUsername, dbPassword); 

      String listAllTablesQuery = "select TABLE_SCHEMA,TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_TYPE='base table' order by TABLE_SCHEMA, TABLE_NAME"; 

      stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); 

      rs = stmt.executeQuery(listAllTablesQuery); 

      rs.last(); 
      System.out.println("Count : "+rs.getRow()); 
      rs.beforeFirst(); 

    %> 

    <h3> 
     List of Tables present in 
     <%=dbName%> 
     Database 
    </h3> 
    <table border=3> 
     <tr> 
      <th>Schema Name</th> 
      <th>Table Name</th> 
     </tr> 
     <% 
      String tableInfoUrl = "TableInfo.jsp?tableName="; 

       while (rs.next()) { 
        out.println("<tr><td>" + rs.getString(1) + "</td><td><a target=_blank href=" + tableInfoUrl 
          + rs.getString(2) + "&schemaName=" + rs.getString(1) + ">" + rs.getString(2) 
          + "</a></td></tr>"); 
       } 

       rs = null; 
     %> 

    </table> 
    <% 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } finally { 
      if (rs != null) 
       try { 
        rs.close(); 
       } catch (Exception e) { 
       } 
      if (stmt != null) 
       try { 
        stmt.close(); 
       } catch (Exception e) { 
       } 
      if (con != null) 
       try { 
        con.close(); 
       } catch (Exception e) { 
       } 
     } 

結果セットでは64個の行が表示されますが、これはdboスキーマに対してのみです。しかし、私はssmsで指定されたクエリを実行する場合、私は1701行を取得しています。 dboのテーブルを考えると、1219個のテーブルもあります。結果セットに64行だけが存在するのはなぜですか?

+0

INFORMATION_SCHEMA.TABLESから 'select count(*)を試しましたか?Java/JSPではTABLE_TYPE = 'ベーステーブル'ですか?結果は何ですか? – hflzh

+1

あなたのユーザーは正しい権限を持っていますか? – Sanjeev

+0

@ zhliu03私は出力としてテーブルとスキーマ名が必要なので、select count(*)を試しませんでした。私は、テーブルに欠けているテーブルの数を調べるだけです。 –

答えて

0

データベース名がconnectionUrlに存在しなかったため、これが発生していました。 これは期待通りに機能しています。すべてありがとうございます:)

0

WebアプリケーションからDBに接続するために使用するユーザは、ssmsで使用するユーザと同じですか?異なる場合は、Webアプリケーションから接続するために使用するユーザーに、どのテーブルにアクセス権があるかを確認します。

+0

はい、私がアプリケーションで使用したユーザー名は、Dbをssms経由で接続するのと全く同じです –

関連する問題