Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: multiple resultsets

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default multiple resultsets

    This code is probably totally wrong, but I'm in the learning process.
    Im trying to figure out how to use multiple resultsets in 1 jsp page.

    If I find the userID from Users table to match a userID in the usersType table, then the selectbox should be checked.
    But it's not working until now.
    Does somebody know an easier way to accomplish such a thing?

    <%@page contentType="text/html; charset=iso-8859-1" language="java"%>
    <%@ page import="java.sql.*" %>
    <%@ page import="java.io.*" %>
     
    <!DOCTYPE html>
    <html>
        <head>
            <title>Successfully Login</title>
        </head>
        <body bgcolor="#0099CC">
            <div id="navigation">
                <ul class="top-level">
                    <li><a href="success.jsp">Home</a></li>
                    <li><a href="preferences.jsp">Preferences</a></li>
                    <li><a href="recommendations.jsp">Recommendations</a></li>
                </ul>
            </div>
            <div id="wrapper">
                <%
                     Connection connection = null;
                     Statement statement = null;
                     Statement statement2 = null;
                     ResultSet rs = null;
                     ResultSet rs2 = null;
                     ResultSet rs3 = null;
                     Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
                     connection = DriverManager.getConnection("jdbc:derby://localhost:1527/recommendation;create=true;user=root;password=root");
                     statement = connection.createStatement();
                     statement2 = connection.createStatement();
     
                     String QueryString3 = "SELECT * FROM app.usersType WHERE userID = " + session.getAttribute("ID");
                     rs3 = statement2.executeQuery(QueryString3);
                %>
                <form name="frmPreferences" action="updateDatabase.jsp" method="post">
                    <table>
                        <tr>
                            <td>Do you like to watch movies or series? Or both?</td>
                        </tr>
                        <%
                            String QueryString = "SELECT * FROM app.type";
                            rs = statement.executeQuery(QueryString);
     
                            while(rs.next()){
                                if(rs.getInt(1) == rs3.getInt(2)){
                        %>
                        <tr>
                            <td><input type="checkbox" name="type" checked="checked" value="<%=rs.getString(2)%>" /><%=rs.getString(2)%></td>
                        </tr>
                        <%
                            }else{
                        %>
                        <tr>
                            <td><input type="checkbox" name="type" value="<%=rs.getString(2)%>" /><%=rs.getString(2)%></td>
                        </tr>
                        <%
                            }
                            }
                            rs.close();
                        %>
                        <tr>
                            <td>&nbsp;</td>
                        </tr>
                        <tr>
                            <td>What genre do you like?</td>
                        </tr>
                        <%
                            String QueryString2 = "SELECT * FROM app.genre";
                            rs2 = statement.executeQuery(QueryString2);
     
                            while(rs2.next()){
                        %>
                        <tr>
                            <td><input type="checkbox" name="type" value="<%=rs2.getString(2)%>" /><%=rs2.getString(2)%></td>
                        </tr>
                        <%
                            }
                        %>
                        <tr>
                            <td>&nbsp;</td>
                        </tr>
                        <tr>
                            <td><input type="submit" name="submit" value="Opslaan" /></td>
                        </tr>
                    </table>
                </form>
                        <%
                            // close all connections
                            rs2.close();
                            statement.close();
                            connection.close();
                        %>
                <br /><br />
                <a href="logout.jsp"><b>Logout</b></a>
            </div>
        </body>
    </html>
    Last edited by kney; November 28th, 2011 at 10:43 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: multiple resultsets

    But it's not working until now
    Define 'not working'.

    Read the API for Statement Statement (Java Platform SE 6). By default, only one ResultSet per Statement can be open at a single time .
    Last edited by copeg; November 28th, 2011 at 10:05 AM.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: multiple resultsets

    I come from PHP background, and there you can compare 2 items from different resultsets with eachother.
    So you are saying it's not possible here.

    With "not working" i mean i can't get it to compare 2 items from different recordsets

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: multiple resultsets

    Quote Originally Posted by kney View Post
    So you are saying it's not possible here
    I said no such thing. Read the API: one Statement is associated with a single ResultSet. So then, how would you then create more than a single ResultSet?

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: multiple resultsets

    So I changed the first post to what I have now and i get this

    "An Error Occurred:
    java.sql.SQLException: Invalid operation at current cursor position."

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: multiple resultsets

    The cursor positions of a ResultSet must be set to the appropriate position (calling next or some other function defined by the API). Your code never does this on the rs3 variable.

Similar Threads

  1. multiple jvms
    By gaikwaddeepali111 in forum Member Introductions
    Replies: 0
    Last Post: September 10th, 2011, 12:22 AM
  2. use multiple gui displays
    By jonwymore in forum AWT / Java Swing
    Replies: 1
    Last Post: November 19th, 2010, 10:11 AM
  3. Multiple Multishuffles
    By dino2dy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 10th, 2010, 01:36 PM
  4. multiple tcp connection?
    By nasser in forum Java Networking
    Replies: 4
    Last Post: July 31st, 2010, 07:35 AM
  5. Multiple Queues
    By fh84 in forum Threads
    Replies: 1
    Last Post: December 3rd, 2009, 02:28 PM