我正在开发Web应用程序以了解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) {
}
}
我在结果集中只获得了64行,仅用于dbo模式。但如果我在ssms中执行给定的查询,我得到1701行。如果我们考虑dbo下的表格,那么也有1219个表格。我不明白为什么结果集中只有64行?
您是否尝试过从INFORMATION_SCHEMA.TABLES中选择count(*),其中TABLE_TYPE ='base table'在Java/JSP中?结果是什么? – hflzh
是你的用户有没有正确的权限? – Sanjeev
@ zhliu03我没有尝试select count(*),因为我需要表和模式名称作为输出。我只专注于计数器,看看有多少表格丢失。 –