我在 SQL Server 表中有 558 行。如果我写"SELECT * FROM table"
,我会得到空的结果集。但是,如果我写,"SELECT TOP 100 * FROM table"
我会得到一个包含 100 行的结果集。有时 TOP600
有效,有时只有 TOP 200
,真的很奇怪,比如一些内存问题......我已经在代码中关闭了 rs/ps/conn 。
Jboss 7.1.3、Java 1.7 都尝试了这两种驱动程序(net.sourceforge.jtds.jdbc.Driver, com.microsoft.sqlserver.jdbc.SQLServerDriver 4.1)
。
错误日志中没有任何内容(bot Jboss 和 SQL Server)。
代码:
<%
@ page trimDirectiveWhitespaces="true" %>
<%@ page pageEncoding="UTF-8" %>
<% response.setContentType("text/html;charset=utf-8"); %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="org.json.*" %>
<%
String table = "tblTable";
JSONObject result = new JSONObject();
JSONArray array = new JSONArray();
int amount = 10;
int start = 0;
int echo = 0;
int col = 0;
int total = 0;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=dbTables", "sa", "sa");
try {
String sql = "SELECT count(*) As num FROM "+table;
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if(rs.next()){
total = rs.getInt("num");
}
} catch(Exception e){
}
try {
String sql = "SELECT TOP 500 * FROM "+table;
Statement ps = conn.createStatement();
//ps.setFetchSize(200);
//int fetchSize = ps.getFetchSize();
ResultSet rs = ps.executeQuery(sql);
while (rs.next()) {
JSONArray ja = new JSONArray();
ja.put(rs.getString("colOne"));
ja.put(rs.getString("colTwo"));
array.put(ja);
}
rs.close();
ps.close();
//out.print(fetchSize);
result.put("draw", "1");
result.put("recordsTotal", total);
result.put("recordsFiltered", total);
result.put("data", array);
response.setContentType("application/json");
response.setHeader("Cache-Control", "no-store");
out.print(result.toString(4));
} catch (Exception e) {
} finally {
conn.close();
}
%>