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

rs.getString问题?????

为什么执行出现奇怪问题呢》》》
如果用out.print(rs.getString(1)+ "   ");那就没问题,可以输出,但用
String   user1=rs.getString( "user ");
程序就出现
javax.servlet.ServletException:   Illegal   operation   on   empty   result   set.
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
org.apache.jsp.admindata_jsp._jspService(admindata_jsp.java:136)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root   cause  

java.sql.SQLException:   Illegal   operation   on   empty   result   set.
com.mysql.jdbc.ResultSet.checkRowPos(ResultSet.java:693)
com.mysql.jdbc.ResultSet.getStringInternal(ResultSet.java:4832)
com.mysql.jdbc.ResultSet.getString(ResultSet.java:4810)
com.mysql.jdbc.ResultSet.getString(ResultSet.java:4826)
org.apache.jsp.admindata_jsp._jspService(admindata_jsp.java:118)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


<%

  String   sql= "select   *   from   user_admin   where   user= ' "+user+ " ' ";
  ResultSet   rs=statement.executeQuery(sql);
rs.next();
//out.print(rs.getString(1)+ "   ");
String   user1=rs.getString( "user ");


  rs.close();
    statement.close()   ;
    connection.close();
      %>    

<%=   user1   %>

------解决方案--------------------
你的表字段是咋样的?
------解决方案--------------------
empty result set
------解决方案--------------------
rs.next()有返回值的啊,如果为false说明没有任何记录,你还getString当然出错
------解决方案--------------------
<%

String sql= "select * from user_admin where user= ' "+user+ " ' ";
ResultSet rs=statement.executeQuery(sql);
String user1 = null;
//改下这里
if (rs.next()) {
//out.print(rs.getString(1)+ " ");
user1=rs.getString( "user ");
}


rs.close();
statement.close() ;
connection.close();
%>

<%= user1 %>
------解决方案--------------------
user1在外面定义呀
------解决方案--------------------
[user]这样写试试 user在sql中是关键字
------解决方案--------------------
会不会是关键字的问题,USER也是数据库里的一个关键字~有时会有问题的,你把这个字段该以下,比如USERS什么的!