Java 结果集以相反的顺序显示
Java resultset displayed in reverse order
我正在使用以下代码测试一个非常简单的 Java webapp doGet() 方法:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
try
{
Class.forName("oracle.jdbc.OracleDriver");
}
catch(ClassNotFoundException ex)
{
System.out.println(ex);
}
String sql_qrp="select * from HR.EMP_MGMT";
try
{
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/XE","HR","HR");
try(Statement stm=con.createStatement())
{
ResultSet rs=stm.executeQuery(sql_qrp);
while(rs.next())
{
int emp_ID=rs.getInt("EMP_ID");
String name=rs.getString("NAME");
String address=rs.getString("ADDRESS");
int tel=rs.getInt("TEL");
String email=rs.getString("EMAIL");
System.out.println(emp_ID+", "+name+", "+address+", "+tel+", "+email);
}
}
}
catch (SQLException ex) {
Logger.getLogger(Add_Employee.class.getName()).log(Level.SEVERE, null, ex);
}
}
它与 Oracle 数据库连接良好,并从代码中指定的 table 中获取所有 3 行,但顺序相反。我已经尝试强制 FETCH_FORWARD 但没有成功。所以它获取了如下记录:
编号 3.,2.,1。而不是 ID 1.、2.、3.
添加
ORDER BY id ASC
至sql_qrp
。
我正在使用以下代码测试一个非常简单的 Java webapp doGet() 方法:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
try
{
Class.forName("oracle.jdbc.OracleDriver");
}
catch(ClassNotFoundException ex)
{
System.out.println(ex);
}
String sql_qrp="select * from HR.EMP_MGMT";
try
{
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/XE","HR","HR");
try(Statement stm=con.createStatement())
{
ResultSet rs=stm.executeQuery(sql_qrp);
while(rs.next())
{
int emp_ID=rs.getInt("EMP_ID");
String name=rs.getString("NAME");
String address=rs.getString("ADDRESS");
int tel=rs.getInt("TEL");
String email=rs.getString("EMAIL");
System.out.println(emp_ID+", "+name+", "+address+", "+tel+", "+email);
}
}
}
catch (SQLException ex) {
Logger.getLogger(Add_Employee.class.getName()).log(Level.SEVERE, null, ex);
}
}
它与 Oracle 数据库连接良好,并从代码中指定的 table 中获取所有 3 行,但顺序相反。我已经尝试强制 FETCH_FORWARD 但没有成功。所以它获取了如下记录: 编号 3.,2.,1。而不是 ID 1.、2.、3.
添加
ORDER BY id ASC
至sql_qrp
。