我在mysql数据库中插入数据,如 -
create table details(loginid varchar(55),interser1 char,interser2 char,interser3 char);
insert into details values("1",'y','n','y');
当我在jsp中重温时 -
ResultSet rs=st.executeQuery("select * from interest where loginid='1'");
String interest1=rs.getString(2);//but at index-2 value is character.
String interest2=rs.getString(3);
String interest3=rs.getString(4);
这给了我错误 -
SEVERE: Servlet.service() for servlet [jsp] in context with path [/more_value_in_param] threw exception [An exception occurred processing JSP page /display.jsp at line 19
16: Connection con=CommonUtil.getConnection();
17: Statement st=con.createStatement();
18: ResultSet rs=st.executeQuery("select * from interest where loginid='1'");
19: String interest1=rs.getString(2);
20: String interest2=rs.getString(3);
21: String interest3=rs.getString(4);
22: ResultSet rs1=st.executeQuery("select * from details where loginid='1'");
Stacktrace:] with root cause
java.sql.SQLException: Before start of result set
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:841)
at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5650)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5570)
at org.apache.jsp.display_jsp._jspService(display_jsp.java:84)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
如何从mysql中的表中检索字符值?
答案 0 :(得分:1)
我认为问题是你需要调用rs.next()
以确保在检索任何结果之前先得到结果。所以你需要:
ResultSet rs=st.executeQuery("select * from interest where loginid='1'");
while (rs.next()) {
String interest1=rs.getString(2);
String interest2=rs.getString(3);
String interest3=rs.getString(4);
}
对于你的问题,你可以通过调用rs.getString(2).charAt(0)
来获得角色。顺便说一句,如果可能的话,请尝试在控制器或操作中使用此逻辑,而不是在JSP中。