获取异常';java.sql.SQLException:游标状态无效';
我正在使用Javaservlet创建定制的博客。我想获得数据库表中的行数。在得到它之后,我使用获取异常';java.sql.SQLException:游标状态无效';,java,sql,servlets,Java,Sql,Servlets,我正在使用Javaservlet创建定制的博客。我想获得数据库表中的行数。在得到它之后,我使用for()loop来获取每条消息的数据,然后在循环中逐个加入所有消息。然后,将其传递到blogs.jsp页面。这是代码 try { ses=request.getSession(true); String ses_val=(String)ses.getAttribute("username"); Class.forName("sun.jdbc.odbc
for()
loop来获取每条消息的数据,然后在循环中逐个加入所有消息。然后,将其传递到blogs.jsp页面。这是代码
try {
ses=request.getSession(true);
String ses_val=(String)ses.getAttribute("username");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:harshan_web");
if (ses_val==null)
{
response.sendRedirect("error.html");
}
else
{
if (con!=null)
{
SimpleDateFormat blog_time1=new SimpleDateFormat("HH:mm:ss");
Date blog_time2=new Date();
StringBuilder blog_time = new StringBuilder( blog_time1.format( blog_time2 ) );
username=ses_val;
blog_message=request.getParameter("memo1");
if ((username!=null)&&(blog_message!=null))
{
st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
st2=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
st2.executeUpdate("insert into blog(username,blog_message,blog_time) values('"+username+"','"+blog_message+"','"+blog_time+"')");
ResultSet rs=st.executeQuery("select count(*) as tot_count from blog");
if (rs.next()){
//rs.last();
//int count=rs.getRow();
int count=rs.getInt("tot_count");
String blogs="";
for (int i=1; i<=count; i++)
{
ResultSet rs2=st.executeQuery("select * from blog where ID="+i+"");
String user1=rs2.getString("username");
String blog_m=rs2.getString("blog_message");
String blog_t=rs2.getString("blog_time");
blogs="<div><table border='0' bgcolor='#6ca6cd' cellpadding='20' cellspacing='20'><tr><td>"+user1+"<br><br>"+blog_t+"</td><td>"+blog_m+"</td></tr></table></div>"+blogs;
}
response.sendRedirect("blogs.jsp?blogs="+blogs+"");
}
else
{
String no_blog="There are 0 blogs in this page!!!";
response.sendRedirect("blogs.jsp?invalid_login="+no_blog+"");
}
}
else
{
st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
st2=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs=st.executeQuery("select count(*) as tot_count from blog");
if (rs.next()){
//rs.last();
//int count=rs.getRow();
int count=rs.getInt("tot_count");
String blogs="";
for (int i=1; i<=count; i++)
{
ResultSet rs2=st.executeQuery("select * from blog where ID="+i+"");
String user1=rs2.getString("username");
String blog_m=rs2.getString("blog_message");
String blog_t=rs2.getString("blog_time");
blogs="<div><table border='0' bgcolor='#6ca6cd' cellpadding='20' cellspacing='20'><tr><td>"+user1+"<br><br>"+blog_t+"</td><td>"+blog_m+"</td></tr></table></div>"+blogs;
}
response.sendRedirect("blogs.jsp?blogs="+blogs+"");
}
else
{
String no_blog="There are 0 blogs in this page!!!";
response.sendRedirect("blogs.jsp?invalid_login="+no_blog+"");
}
}
}
else
{
response.sendRedirect("error.html");
}
} catch (Exception e) {System.out.print(e);} finally {out.close();}
我无法理解ResultSet游标可能是什么。在数据库中输入新值。但是,问题是当试图通过请求blogs的值来在.jsp文件中显示blog值时
我该怎么做才能使它正确。我们可以自定义博客代码,还是应该强制使用博客引擎 这
int count=rs.getRow()代码>根本没有做您认为它正在做的事情。阅读javadoc。这不是迭代ResultSet
的惯用方法。您永远不应该使用catch(Exception e){System.out.print(e);}
,因为它隐藏了发生异常的行号,这会在尝试调试时给您带来不必要的困难。如果您必须捕获异常,请使用e.printStackTrace()
。此外,“我们可以自定义博客代码,还是强制使用博客引擎?”我无法理解导致这个问题的混乱想法。
java.sql.SQLException: invalid cursor state;