Results 1 to 6 of 6
  1. #1
    gokul1242 is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default problem in setString method

    I have two tables "Table1" with columns user_name,Password and course ID and another table "course" with columns course_id,course_name.I have used the following code to display the course ID from Table1 according to the user_name received from the login page.using ResultSet rs1.

    Now i want to retrieve the course_name from the table "course" according to the course ID receieve from "Table1".for that in the second query pstmt2.setString(1, ); what parameter i should use to get the course_id value from the previous query

    code:
    HttpSession sess=request.getSession();

    String a=(String)sess.getAttribute("user");
    String b=(String)sess.getAttribute("pass");

    try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con=DriverManager.getConnection("jdbc:odbc:ggg");
    Statement st = con.createStatement();



    String query="select * from Table1 where user_name=?";
    PreparedStatement pstmt=con.prepareStatement(query);
    pstmt.setString(1,a);
    ResultSet rs1=pstmt.executeQuery();
    while(rs1.next())

    out.println("<h3>COURSE ID:&nbsp;"+rs1.getString("course ID")+"<h3>");


    String query2="SELECT * from course where course_id=?";
    PreparedStatement pstmt2=con.prepareStatement(query2);

    pstmt2.setString(1,); <---(this place what parameter to use da)

    ResultSet rs2=pstmt2.executeQuery();

    while(rs2.next())
    {
    out.println("<h3>course name:&nbsp;"+rs2.getString("course_name")+"<h3>");
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    Store the result of this:
    Java Code:
    rs1.getString("course ID")
    in a variable, which you can then use in the next query.

    However...
    If all you need from this is the course id and course name for the user supplied then this is a single query, not two as you have.

  3. #3
    gokul1242 is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    are u asking to do like this ?
    rs1.getString("course ID")=d;

    and use the variable 'd' in
    pstmt2.setString(1,d);

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    Quote Originally Posted by gokul1242 View Post
    are u asking to do like this ?
    rs1.getString("course ID")=d;

    and use the variable 'd' in
    pstmt2.setString(1,d);
    Java Code:
    rs1.getString("course ID")=d
    Other way around. You're assigning the result of getString to the variable (though the compiler errors would have shown you the problem).

    And you really ought to give your variables proper names...a, b, c, d are rubbish and provide no information at all to anyone reading that code (which includes us here).

    But still, the best solution is to write a single SQL query that gets the data you want. Do as much in a single query as you can, because any Java code you write that mimics SQL is going to be slower (never mind increased traffic to/from the db server).

  5. #5
    gokul1242 is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    ya thanks.i changed the query as follows

    String query2="SELECT * from course where course_id =(select course_id from Table1 where user_name=?)";
    and finally got the problem solved

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    That's not how you do a join...it might work, but it's not good SQL.

Similar Threads

  1. problem with method
    By lyes_djaml in forum New To Java
    Replies: 6
    Last Post: 01-21-2011, 11:04 PM
  2. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 11:40 AM
  3. How can i setString when i use radio buttons?
    By Tahorn in forum AWT / Swing
    Replies: 4
    Last Post: 02-16-2010, 02:03 PM
  4. Method problem
    By Jeff6461 in forum New To Java
    Replies: 1
    Last Post: 03-02-2009, 06:48 AM
  5. Replies: 29
    Last Post: 09-25-2008, 07:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •