Results 1 to 4 of 4
  1. #1
    smacker is offline Member
    Join Date
    Jan 2011
    Posts
    46
    Rep Power
    0

    Default hello all , a begginners question :

    sorry if it's too simple , i'm having trouble grasping how it works
    just started sqling in java
    i have a Books [with fields id , name ] in order to add a new book i want to get the max id
    from it and add 1 to it for the next id this is what i did
    Java Code:
            try{
            Connection con=DriverManager.getConnection("jdbc:derby://localhost:1527/sample","root","root");
            Statement stmt= con.createStatement();  
           ResultSet rs=stmt.executeQuery("SELECT MAX(id) FROM lib.BOOKS ");
           int i=rs.    <----
            stmt.executeUpdate("insert into Books (id,name) values"+i+_name);
            }catch(Exception e){}
    my trouble is in the marked because i get a table back not a simple int
    any ideas on how to improve this code ?
    i'm pretty new to the sql world and this code seems very messed up there must be something simpler

    thanks to all the helpers !

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: hello all , a begginners question :

    You should obtain the value from the ResultSet instance. See Retrieving and Modifying Values from Result Sets in Oracle's Tutorial. The surrounding pages provide a fairly comprehensive introduction to using a database.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: hello all , a begginners question :

    Please go through the Forum Rules -- particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: hello all , a begginners question :

    And once you've managed to do what pbrockway2 suggests, I'll suggest looking into the Derby documentation for generated ids.
    That handles this situation in a more reliable way than finding the max(id).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 2
    Last Post: 05-25-2012, 05:00 PM
  2. Java Question [Beginner Question]
    By joker760 in forum New To Java
    Replies: 3
    Last Post: 12-13-2011, 05:01 PM
  3. question posted by indissa: library question.
    By Fubarable in forum New To Java
    Replies: 2
    Last Post: 11-18-2011, 02:14 AM
  4. Question concerning question marks and colons
    By jim01 in forum New To Java
    Replies: 17
    Last Post: 01-14-2011, 01:05 AM
  5. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 09:49 AM

Posting Permissions

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