我正在使用Java Servlet来创建自定义博客。我想获取数据库表中的行数。在我得到它之后,我使用for()
循环来获取每条消息的数据,然后在循环中逐个加入。然后,将其传递到 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();}
我认为一切都是对的。但是当我运行它时,我只得到以下异常。
java.sql.SQLException: invalid cursor state;
我无法理解ResultSet游标可能是什么。新值将输入数据库中。但是,问题在于尝试通过请求博客的值在.jsp文件中显示博客值。
我能做些什么来使它正确。我们可以制作自定义的博客代码,还是我们应该强制使用博客引擎?