Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. Database question

Database question

Scheduled Pinned Locked Moved Java
htmldatabasecomhelpquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheMrProgrammer
    wrote on last edited by
    #1

    Hi folks! this is the code thats giving me problems.

    try{
    ResultSet rs = Main.dbase.search("select * from myTable");
    //rs.next()
    while(rs.next())
    {
    cmb.addItem(rs.getString(1).trim());
    }
    rs.close();
    } catch (Exception ex) {System.out.println(ex);}

    here dbase is an object executing Statement.executeQuery, and cmb is JComboBox. the problem is that even though the table has 10 rows, i get only single row in cmb. if I don't comment out the first rs.next(), i get the 2nd row in cmb. i tried to track the value of rs.next() and found that after while(rs.next()) it gives out false, always. My target is to get all the 10 values in cmb.

    TheMrProgrammer http://www.icbse.com/2009/funny-exam-answers-school-students http://download.cnet.com/TheCalcMan/3000-2094\_4-10958266.html

    N J 2 Replies Last reply
    0
    • T TheMrProgrammer

      Hi folks! this is the code thats giving me problems.

      try{
      ResultSet rs = Main.dbase.search("select * from myTable");
      //rs.next()
      while(rs.next())
      {
      cmb.addItem(rs.getString(1).trim());
      }
      rs.close();
      } catch (Exception ex) {System.out.println(ex);}

      here dbase is an object executing Statement.executeQuery, and cmb is JComboBox. the problem is that even though the table has 10 rows, i get only single row in cmb. if I don't comment out the first rs.next(), i get the 2nd row in cmb. i tried to track the value of rs.next() and found that after while(rs.next()) it gives out false, always. My target is to get all the 10 values in cmb.

      TheMrProgrammer http://www.icbse.com/2009/funny-exam-answers-school-students http://download.cnet.com/TheCalcMan/3000-2094\_4-10958266.html

      N Offline
      N Offline
      Nagy Vilmos
      wrote on last edited by
      #2

      Have a go at this:

      // you need to define the Statement outside of
      // the try so it can be cleaned up in finally
      Statement statement = null;
      try {
      // create and execute the query
      // rs is not yet pointing to anything
      statement = Main.dbase.createStatement();
      ResultSet rs = statement.executeQuery("select * from myTable");
      while (rs.next()) {
      cmb.addItem(rs.getString(1).trim());
      }
      } catch (SQLException e ) {
      System.out.println(ex);
      } finally {
      if (statement != null) {
      statement.close();
      }
      }


      Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

      J 1 Reply Last reply
      0
      • N Nagy Vilmos

        Have a go at this:

        // you need to define the Statement outside of
        // the try so it can be cleaned up in finally
        Statement statement = null;
        try {
        // create and execute the query
        // rs is not yet pointing to anything
        statement = Main.dbase.createStatement();
        ResultSet rs = statement.executeQuery("select * from myTable");
        while (rs.next()) {
        cmb.addItem(rs.getString(1).trim());
        }
        } catch (SQLException e ) {
        System.out.println(ex);
        } finally {
        if (statement != null) {
        statement.close();
        }
        }


        Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #3

        Myself I can't see how that answers the OPs question.

        N 1 Reply Last reply
        0
        • T TheMrProgrammer

          Hi folks! this is the code thats giving me problems.

          try{
          ResultSet rs = Main.dbase.search("select * from myTable");
          //rs.next()
          while(rs.next())
          {
          cmb.addItem(rs.getString(1).trim());
          }
          rs.close();
          } catch (Exception ex) {System.out.println(ex);}

          here dbase is an object executing Statement.executeQuery, and cmb is JComboBox. the problem is that even though the table has 10 rows, i get only single row in cmb. if I don't comment out the first rs.next(), i get the 2nd row in cmb. i tried to track the value of rs.next() and found that after while(rs.next()) it gives out false, always. My target is to get all the 10 values in cmb.

          TheMrProgrammer http://www.icbse.com/2009/funny-exam-answers-school-students http://download.cnet.com/TheCalcMan/3000-2094\_4-10958266.html

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          There are two parts. 1. Database code 2. Gui code. It seems apparent that you are in fact getting 10 rows from the database. Your statements about the commented rs.next() and your implicit comments about it. If you are not sure the verify it using a debugger or System.out.println (do NOT use gui code.) Consequently that means the problem is with your GUI code and not the database code. You can replace your database code entirely with a for/next loop that adds 10 fixed values (such as "xxx") to the combo box. Until that code works it is pointless to attempt to do anything with the database code. Creating a small complete example with JUST the gui code (no database code) might help to determine what the problem is.

          1 Reply Last reply
          0
          • J jschell

            Myself I can't see how that answers the OPs question.

            N Offline
            N Offline
            Nagy Vilmos
            wrote on last edited by
            #5

            It fixes the code...


            Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

            J 1 Reply Last reply
            0
            • N Nagy Vilmos

              It fixes the code...


              Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              So you are agreeing that it does not in fact answer the question?

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups