日期:2014-05-18  浏览次数:20840 次

在线等,马上给分
原代码:
<%
String   name=request.getParameter( "username ");
System.out.println(name);
String   pass=request.getParameter( "pass ");

Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");   //载入驱动程序类别
Connection   con   =   DriverManager.getConnection( "jdbc:odbc:login ");  
Statement   stmt   =   con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet   rs   =   stmt.executeQuery( "select   *   from   d ");
 
%>
<%=   rs.getString(2)   %>
<%  

rs.close();
stmt.close();  
con.close();  
%>
错误提示:

org.apache.jasper.JasperException:   Exception   in   JSP:   /test.jsp:27

24:   ResultSet   rs   =   stmt.executeQuery( "select   *   from   d ");
25:    
26:   %>
27:   <%=   rs.getString(2)   %>
28:   // <%  
29:  
30:   //rs.close();

------解决方案--------------------
加上 rs.next();
------解决方案--------------------
<%= rs.getString(2) %> --> 改成 <%= rs.getInt(2) %>
------解决方案--------------------
while(rs.next)
{
out.print();
}
------解决方案--------------------
改成:

<%
String name=request.getParameter( "username ");
System.out.println(name);
String pass=request.getParameter( "pass ");

Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver "); //载入驱动程序类别
Connection con = DriverManager.getConnection( "jdbc:odbc:login ");
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery( "select * from d ");

rs.next();
%>
<%= rs.getString(2) %>
<%

rs.close();
stmt.close();
con.close();
%>

------解决方案--------------------
刚刚查询的时候,rs游标还是停止在结果集第一条记录的前面一个地址上,所以要用rs.next()来让游标移动到第一条数据记录上 才可以读数据。
------解决方案--------------------
就算查询的记录只有一条也要加rs.next();的切记!