
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);
}
Leave a Reply