Wrox Home  
Search P2P Archive for: Go

  Return to Index  

servlets thread: [Microsoft][ODBC Driver Manager] Invalid cursor state


Message #1 by "aboamama" <khrtoto@h...> on Fri, 23 Aug 2002 18:55:19
The cursor starts before the first record, so you have to add

    result.next();

before you try to read fields.

Your query here is going to select every record in the table, is that what
you want?  If you're looping through the resultSet you need to use

    while result.next() {
	[...do your stuff...]
    }

Greg


-----Original Message-----
From: aboamama [mailto:khrtoto@h...]
Sent: Friday, August 23, 2002 6:55 PM
To: Servlets
Subject: [servlets] [Microsoft][ODBC Driver Manager] Invalid cursor
state


Hi again

I got this error and I can't get the right result from the database

SQLException caught: [Microsoft][ODBC Driver Manager] Invalid cursor state

this is my code :

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;


public class StudentLogin extends HttpServlet {


	  public void init(ServletConfig config) throws ServletException {
        super.init(config);

       }
    protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
      throws ServletException, java.io.IOException {


		String URL = "jdbc:odbc:css";
		Connection connection  = null;
		Statement statement = null;
		ResultSet rs   = null;
		String query;


		try {
         Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );

			//Get a Connection to the database
			connection 
            DriverManager.getConnection( URL, "", "" );

			//Create a Statement object
			statement = connection.createStatement();


			//Execute an SQL query, get a ResultSet
	query = "SELECT Student_id, Password FROM student ";
	ResultSet result = statement .executeQuery(query);

	String callNumResult = result.getString("Student_id");
	String courseNameResult = result.getString("Password");


	  response.setContentType("text/html");
        java.io.PrintWriter output = response.getWriter();
        String uName = request.getParameter("userid");
        String pWord = request.getParameter("password");
        if ( pWord == courseNameResult) {

	// create and send HTML page to client

	StringBuffer buf = new StringBuffer();


  buf.append( "<html><head><title>Manage Courses</title></head>\n" );
  buf.append( "<body bgcolor='#DCDCB8'><h2><font color='#0000FF'>\n" );
  buf.append( " Welcome " + uName + "! To Coursework Submission
System</font></h2>\n" );
    buf.append( " <form method='POST' action='StudentPage'> \n" );
     buf.append( " <input type='hidden' name='userid'
value='student'><h3>\n" );
buf.append( " <font color='#800080'>Select Course:</font></h3>\n " );
  buf.append( " <table border='0' width='65%' cellspacing='0'
cellpadding='2'><tr></tr></table> \n " );
buf.append( " <table border='1' width=;100%'><tr><td width='50%'
bgcolor='#CDCDE7'> \n" );
buf.append( " <center><h3><font color='#800080'>Make a Selection:
</font></h3></center>\n " );
 buf.append( " <select name='select' size='12' tabindex='1'>\n " );
buf.append( " <option value='go to home page'>go to Course home
page</option> \n" );
buf.append( " <option value='submit assignment'>submit
assignment</option>\n " );
      buf.append( " <option value='view submitted assignments'>view
submitted assignments</option>\n " );
buf.append( " <option value='view grades'>view grades</option>\n" );
buf.append( " <option value='read instructor conmeents'>read instructor
conmeents</option> \n " );
buf.append( " <option value='send instructor message'>send instructor
message</option>\n " );
buf.append( " <option value='report sytem error'>report sytem
error</option> \n" );
buf.append( " <option value='log out'>log out</option></select>\n " );
 buf.append( " <div align='center'><center><p><input type='submit'
value='Submit' name='submit'>\n " );
buf.append( " <input type='reset' value='Reset' name='reset'></td><td
width='50%' bgcolor='#CDCDE7'>\n" );
buf.append( " <textarea rows='20' name='message' value='Mon Jul 01
04:44:14 CST 2002' cols='50' tabindex='2'> \n " );
 buf.append( " </textarea></td></tr></table></form></body></html>\n " );

 output.println( buf.toString() );

   }


     else {
            // create and send HTML page to client

	StringBuffer buf = new StringBuffer();


   buf.append( "<html><head><title>Manage Courses</title></head>\n" );
   buf.append( "<body bgcolor='#DCDCB8'><h2><font color='#0000FF'>\n" );
			buf.append( " Invalid Login</font></h2>\n" );
   buf.append( " </body></html>\n " );

	 output.println( buf.toString() );
        }

       output.close();
    }

catch(ClassNotFoundException e) {


   System.out.println( "Could not load database driver: " + e.getMessage
());
		}

catch(SQLException e) {
  System.out.println("SQLException caught: " + e.getMessage());
		}

		finally {
			//close the database connection.
			try {
				if (connection!= null) connection.close();
				}
			catch (SQLException e) {}
		}
	}


    protected void doGet(HttpServletRequest request, HttpServletResponse
response)
      throws ServletException, java.io.IOException {
        processRequest(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse
response)
      throws ServletException, java.io.IOException {
        processRequest(request, response);
    }

}

can any body help please ?




  Return to Index