3 Replies - 222 Views - Last Post: 31 July 2013 - 09:11 AM Rate Topic: -----

#1 VIPERHlr  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 85
  • Joined: 05-August 11

Java odbc display lowest

Posted 31 July 2013 - 02:26 AM

Hi, I have a program that connects to my database. I want to display the name of the person with the lowest number of votes. The problem is that their are 2 names that are tied with the lowest votes. The code works perfectly but it is only displaying one name in the textfield but when I run it in my database it displays both so nothing wrong with the sql. How would I get it to display both names?

public void LeastVotedFor()
{
    try
    {
        conn = getConnection();
        String sql3 = "SELECT RclNames,[Total number of votes] FROM Votes WHERE [Total number of votes]=(SELECT MIN([Total number of votes]) FROM Votes)";
        Statement stmt = conn.createStatement();

           ResultSet rs = stmt.executeQuery(sql3);
           
           
           if(rs.next())
           {
               this.jTextField3.setText(rs.getString("RclNames")); // Displays the name of the person with the lowest votes. Only displays one when there is two

               int vote = rs.getInt("Total number of votes");
               jTextField6.setText(Integer.toString(vote)); // Displays the votes for that person
               
           }    
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, e.getMessage());
    }
}

This post has been edited by VIPERHlr: 31 July 2013 - 02:27 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Java odbc display lowest

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java odbc display lowest

Posted 31 July 2013 - 03:53 AM

   if(rs.next()) {


will only return the name of the first person. You will need something like

    while(rs.next()) {


to pass throught all the rows in the ResultSet

Evidently, you will need more than one JTextField or a JTextArea into which you will append the rows returned by the database
Was This Post Helpful? 0
  • +
  • -

#3 VIPERHlr  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 85
  • Joined: 05-August 11

Re: Java odbc display lowest

Posted 31 July 2013 - 03:59 AM

View Postpbl, on 31 July 2013 - 03:53 AM, said:

Evidently, you will need more than one JTextField or a JTextArea into which you will append the rows returned by the database


Is there any way I could print them on one TextField?

This post has been edited by VIPERHlr: 31 July 2013 - 04:00 AM

Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,197
  • Joined: 20-September 08

Re: Java odbc display lowest

Posted 31 July 2013 - 09:11 AM

Quote

Is there any way I could print them on one TextField?
You'd need to concatenate them
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1