最后缩小了问题的范围,这就是我的问题,当我执行一个具有“排序依据”的 SQL 并尝试在 Java 中打印结果集时,结果集未排序。但是当我与我的 SQL 开发人员单独执行 SQL 时,我看到了有序的结果,这是为什么呢?有没有办法解决这个问题?
下面提到的是我正在使用的方法
List getDBValues(short orderBy) throws Exception {
Connection conn = null;
ResultSet rs = null;
List results = nmull;
String sqlStmt = new String();
PreparedStatement ps = null;
try {
conn = Utilities.getConnection(DB);
sqlStmt ="SELECT
COLUMN1,
COLUMN2,
COLUMN3,
FROM SAMPLE_TABLE
WHERE
COLUMN1 IN ('10','15') AND
COLUMN3 IN ('1','2') ORDER BY ? ";
try {
short orderByObj = 0;
if(orderBy < 1){
orderByObj = Short.parseShort(Math.abs(orderBy)+"");
sqlStmt += "DESC";
}else{
orderByObj = orderBy;
}
System.out.println("==================== SQL ==================== \n"+sqlStmt);
ps = conn.prepareStatement(sqlStmt);
ps.setLong(1, orderByObj);
rs = ps.executeQuery();
while (rs.next())
System.out.println(rs.getString(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3));
}
catch(SQLException e) {
e.printStackTrace();
}
}
catch (Exception e) {
e.printStackTrace()
}
return results;
}