Discussions

Web tier: servlets, JSP, Web frameworks: Servlet-JDBC Resultset,Statement Object

  1. Servlet-JDBC Resultset,Statement Object (1 messages)

    Hi

    I want to use multiple select queries in my servlet. Using one select works .. When I use the other select statement inside the first one it doesnt work.. throws exception. Is it that I have to use multiple Statement objects?

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

    public class jdbc2 extends HttpServlet
    {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
    {
    PrintWriter pw;
    response.setContentType("text/html");
    pw=response.getWriter();

    Connection connection;
    Statement stmt,stmt2;
    ResultSet rs;
    String db,user,pass,query;
    String s1,s2,s3,s4;
    db="jdbc:odbc:dsnprac";
    user="";
    pass="";
    query="select * from studentprac";

    try
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    connection=DriverManager.getConnection(db,user,pass);
    stmt=connection.createStatement();
    rs=stmt.executeQuery(query);

    if(rs.next())
    {
    s1=rs.getString(1);
    s2=rs.getString(2);
    s3=rs.getString(3);

    pw.println("S1 :" +s1);
    pw.println("S2 :" +s2);
    pw.println("S3 :"+s3);

    // THIS IS THE PROBLEM AREA
    rs=stmt.executeQuery("select permit from studentprac");
    rs.next();
    s4=rs.getString(3);
    pw.println("Name:" +s4);
    }
    //PROBLEM ENDS

    rs.close();
    connection.close();
    }
    catch(Exception e)
    {
    pw.println("Error :");
    }

    }//get

    }

  2. inside your
    if ( rs.next() ){

    // THIS IS THE PROBLEM AREA
    rs.close();
    //
    rs=stmt.executeQuery("select permit from studentprac");
    rs.next();
    s4=rs.getString(3);
    pw.println("Name:" +s4);


    }