Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using Java Servlet to create customized blogs. I want to get the number of rows in a table of a database. After I get it, I use for() loop to get the data of each message and then join all one-by-one in the loop. Then, pass it to the blogs.jsp page. Here is the code.

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();}

I thought everything was right. But when I ran it, I get only the following exception.

java.sql.SQLException: invalid cursor state;

I can't understand what the ResultSet cursor could be. The new values are entered in the database. But, the problem is when trying to display the blog values in a .jsp file by requesting the value of blogs.

What can I do to make it right. Can we make self-customized blog code OR should we compulsarily use a blog engine?

share|improve this question
4  
This int count=rs.getRow(); is not doing what you think it is doing, at all. Read the javadoc. –  Sotirios Delimanolis Oct 26 '13 at 16:36
1  
this is not the idiomatic way to iterate across a ResultSet –  Jarrod Roberson Oct 26 '13 at 16:37
    
You should never use catch (Exception e) {System.out.print(e);} because it hides the line numbers where the exception occurs, which makes life unnecessarily difficult for you when trying to debug. If you must catch an exception, use e.printStackTrace() instead. –  Robin Green Oct 26 '13 at 20:35
    
also " Can we make self-customized blog code OR should we compulsarily use a blog engine?" I am unable to comprehend the confusion of ideas which must have led to this question. –  Robin Green Oct 26 '13 at 20:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.