JAVA - How To Bind a JCombobox With Mysql Database Values In Java NetBeans

JAVA - How To Bind a JCombobox With Mysql Database Values In Java NetBeans

__________________________________________________________________________

In This Java Code We Will See How To To Populate A JComboBox With MySQL DataBase Data In Java Programming Language.



Project Source Code:

package JavaDB_001;
import javax.swing.*;
import java.sql.*;

public class Test extends JFrame{
    JComboBox jc = new JComboBox();
    JPanel panel = new JPanel();
    Connection con;
    Statement st;
    ResultSet rs;
    public Test()
    {
    this.setSize(400, 400);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    try{
      con = DriverManager.getConnection("jdbc:mysql://localhost/test_db","root","");
      st = con.createStatement();
      String s = "select * from users";
      rs = st.executeQuery(s);
        while(rs.next())
        {
            jc.addItem(rs.getString(1)+" === "+rs.getString(2));
        }
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, "ERROR");
    }finally{
        try{
            st.close();
            rs.close();
            con.close();
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "ERROR CLOSE");
        }
    }
    panel.add(jc);
    this.getContentPane().add(panel);
    this.setVisible(true);
    }

public static void main(String[] args){
            new Test();
  }
}

/////////////////  END



5 commentaires:

  1. Hi. I tried to do here in my code, but failed. Would you help me?

    RépondreSupprimer
  2. first you have to download and add the Mysql connector to your project

    see this article: http://1bestcsharp.blogspot.com/2015/01/java-and-mysql-how-to-connect-java-to.html

    in this line =>

    con = DriverManager.getConnection("jdbc:mysql://localhost/test_db","root","");

    change (test_db) with your database name

    and in this line =>

    String s = "select * from users";

    change (users) with table name

    if it doesn't work but your code and your database description here .

    RépondreSupprimer
    Réponses
    1. It is already connected to the database. I want to do just what is on your page. I created a vendor table. Now I want to, I created a form in java and I just fill the combobox catching database. But I can not. I've done everything but nothing.

      Supprimer
  3. This is not good, when you concatenated the id and the string at the end, it works only if the id is single digit. Otherwise, can you imagine having an ID like 100501, that would look ugly.
    Is there any other way to show only the second column and relate in some way the column 1 to update records?

    RépondreSupprimer
    Réponses
    1. "concatenated the id and the string" i just used them both but you can use any column you want

      - Is there any other way to show only the second column and relate in some way the column 1 ?

      you can see this post to populate a jcombobox with key and value
      http://1bestcsharp.blogspot.com/2015/06/java-how-to-populate-JCombobox-keys-values.html

      Supprimer