How to bind JCombobox with MySQL database values in java NetBeans with source code

In this tutorial show, you How to bind JCombobox with MySQL database values in java. Follow this video tutorial and use bellow source codes complete this task.

Do you need help to remotely set up my any project on your machine or customize any project with your requirement please contact  syntech1994@gmail.com

A video tutorial to bind JCombobox with MySQL database values in java

Create database connection using these codes.

import java.lang.*;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;
/**
 *
 * @author Ruwan
 */
public class DBConnect {
public static Connection connect()
     {
         Connection con=null;
         try {
             Class.forName("com.mysql.jdbc.Driver");
             con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test?","root","");
            // JOptionPane.showConfirmDialog(null,"connect to data base");    
} catch (Exception e) {
        System.out.println("inter.DBConnect.connect()");
        JOptionPane.showConfirmDialog(null,e);
    }
   return con;
}
}

Use this codes to bind JCombobox with MySQL database values in java

cmbselect.removeAllItems();     
 String sq="SELECT `name` FROM `test02` ORDER BY id";
        try {
         PreparedStatement  pst=conn.prepareStatement(sq);
            ResultSet rs=pst.executeQuery();
         while(rs.next())
         {
         String name=rs.getString("name");
         cmbselect.addItem(name);
         }
     } catch (SQLException ex) {
        JOptionPane.showConfirmDialog(rootPane, ex);
     }

The technologies used in bind JCombobox with MySQL database values

  • Java: All codes have been written using the java programming language.
  • MySql: MySql has used as a database for this project.
  • Netbeans: This project has developed inside of the Netbeans IDE.

Check more projects>>

Be the first to comment

Leave a Reply

Your email address will not be published.


*