Using java application I tried to fill 3 combo boxes (A,B and C) from my SQL database. A is populated in initial components, B is populated when an item in A is selected and C is populated when B is populated. I used a below way and its working only for two combo boxes, but when I tried to do it for three combo boxes its failed and it just initialized one value on each combo box. Can any one check my code and determine the issue?
and I would like to confirm this code working for two combo boxes successfully?
---some notes
that's not whole code I removed generated codes by NetBeans,and I made it on jfram as sample to be concentrated for my point
database table name is (MySQLTable)
consists of three columns ( A_Items, B_Items, C_Items)
my class name (fillthreecombo)
I am using (Netbeans 8.0.2)(SQL Server Management 2008)
and for who concerned I uploaded whole sample in this link
TinyUpload.com - best file hosting solution, with no limits, totaly free
but with this names
class name (ManageUserTasksNew)
combo boxes A, B and C will be jComboBox1, jComboBox2 and jComboBox3
database table name is tasks
consists of three columns (TeamName, UserName, TaskName)
HTML Code:
package PackageName;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
public class fillthreecombo extends javax.swing.JFrame {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rst = null;
String comboBox1text;
String comboBox2text;
String comboBox3text;
public fillthreecombo() {
initComponents();
fillComboA();
conn = Connect.connect();
}
// fill first combo box
private void fillComboA(){
try{
conn = Connect.connect();
String sql = "SELECT DISTINCT A_Items FROM MySQLTable ";
pst = conn.prepareStatement(sql);
rst = pst.executeQuery();
while(rst.next()){
String AItems = rst.getString("A_Items");
jComboBox1.addItem(AItems);
}
} catch(Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
// remove items on B combo box for each selection in A combobox
int itemCount = jComboBox2.getItemCount();
for(int i=0;i<itemCount;i++){
jComboBox2.removeItemAt(0);
}
//fill second combo box
comboBox1text = String.valueOf(jComboBox1.getSelectedItem());
if (!comboBox1text.isEmpty() || comboBox1text != null) {
// get data from DataBase with Distinct
try {
String sql =
"SELECT Distinct B_Items FROM MySQLTable WHERE A_Items = '"
+ comboBox1text + "' ";
pst = conn.prepareStatement(sql);
rst = pst.executeQuery();
while(rst.next()){
String BItems = rst.getString("B_Items");
jComboBox2.addItem(BItems);
}
} catch(Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
}
private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {
// remove items from last selection
int itemCount = jComboBox3.getItemCount();
for(int i=0;i<itemCount;i++){
jComboBox3.removeItemAt(0);
}
//fill third combo box
comboBox2text = String.valueOf(jComboBox2.getSelectedItem());
if (!comboBox2text.isEmpty() || comboBox2text != null) {
// get data from DataBase with Distinct
try{
String sql =
"SELECT Distinct C_Items FROM tasks WHERE B_Items = '"
+ comboBox2text + "' ";
pst = conn.prepareStatement(sql);
rst = pst.executeQuery();
while(rst.next()){
String CItems = rst.getString("C_Items");
jComboBox3.addItem(CItems);
}
}catch(Exception ex){
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
}
private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt) {
}
}