SQL Server의 테이블 계층 구조를 알기 위해 웹 응용 프로그램을 개발 중입니다. 다음 코드는 데이터베이스에있는 모든 테이블의 목록을 보여줍니다.결과 집합이 실제 결과보다 적은 행을 반환합니다.
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) {
}
}
결과 집합에서 dbo 스키마에 대해서만 64 행이 표시됩니다. 그러나 내가 ssms에서 주어진 쿼리를 실행하면 나는 1701 개의 행을 얻고있다. dbo 아래의 테이블을 고려하면 1219 개의 테이블도 있습니다. 결과 집합에 64 열 밖에없는 이유를 이해하지 못합니다.
INFORMATION_SCHEMA.TABLES에서 선택 개수 (*)를 사용해 보셨습니까? Java/JSP에서 TABLE_TYPE = '기본 테이블'입니까? 그 결과는 무엇입니까? – hflzh
사용자에게 올바른 권한이 있습니까? – Sanjeev
@ zhliu03 출력으로 테이블 및 스키마 이름이 필요하기 때문에 select count (*)를 시도하지 않았습니다. 나는 얼마나 많은 테이블이 빠졌는지를보기 위해 카운터에 집중하고있다. –