title: Getting one value from SQL select statement in Java
body: <p>I'm trying to return a value from a select statement. Its only one value because the value I'm returning is from the primary key column. </p>
  
  <p>The SQL statement is <code>SELECT itemNo FROM item WHERE itemName = 'astringvalue';</code></p>
  
  <p>My method for getting the value looks like this:</p>
  
  <pre><code>private String viewValue(Connection con, String command) throws SQLException 
      {
          String value = null;
          Statement stmt = null;
  
          try 
          {
              stmt = con.createStatement();
              ResultSet rs = stmt.executeQuery(command);
  
              while (rs.next())
                  value = rs.toString();
          } 
  
          catch (SQLException e ) 
          {
              e.printStackTrace();
          } 
  
          finally
          {
              if (stmt != 
              null) { stmt.close(); }
          }
  
          return value;
  
      }
  </code></pre>
  
  <p>I do have a <code>getConnection()</code> method too.</p>
  
  <p>Here is what im using to call the <code>viewValue</code>method:</p>
  
  <pre><code>if((action.getSource() == btnSave) ||(action.getSource() == btnSavePrint) )
          {
              String findItemNoCommand = "SELECT itemNo FROM `item` WHERE itemName = '" + itemList.getSelectedItem() + "'";
  
              try 
              {
                  itemNo = viewValue(conn, findItemNoCommand);
              }
              catch (SQLException e) 
              {
                  e.printStackTrace();
              }
  
              System.out.println(itemNo);
          }
  </code></pre>
  
  <p>The code above was written for a <code>ButtonHandler</code></p>
  
  <p>Right now, for the <code>println</code> I'm getting a "<code>com.mysql.jdbc.JDBC4ResultSet@1e72cae</code>" .. I don't understand how it is so.. but I'm assuming that ResultSet is the wrong choice here. </p>
  
  <p>My question is.. what can i use there that can work?</p>
  
  <p>Any help or clue as to what im doing wrong is much appreciated. </p>