public TaxCategory findByName(int taxId) {
TaxCategory taxCategory=null;
Statement stmt = null;
ResultSet rst = null;
Connection acceDB = conexion.getConexion();
String query ="Select * from tax_categories where tax_category_id=" + taxId;
try{
stmt = acceDB.createStatement();
rst = stmt.executeQuery(query);
if(rst.next()){
taxCategory= new TaxCategory();
taxCategory.setTaxCategoryId(rst.getInt("tax_category_id"));
taxCategory.setTaxCategoryName(rst.getString("tax_category_name"));
} else {
}
}catch(SQLException | java.lang.NullPointerException ex ){
JOptionPane.showMessageDialog(null,ex.getMessage());
} finally{
try {
if(rst !=null){
rst.close();
}
} catch (SQLException ex) {
Logger.getLogger(TaxCategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
}
if(stmt != null){
try {
stmt.close();
} catch (SQLException ex) {
Logger.getLogger(TaxCategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return taxCategory;
}
Now I'm going to use the function with a JList
frmTax.jList1.addListSelectionListener((ListSelectionEvent e) ->{
Tax tax = null;
TaxCategory taxCategory = null;
frmTax.jList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tax = (Tax) frmTax.jList1.getSelectedValue();
taxCategory = modeloTaxCategory.findByName(tax.getTax_category_id());
frmTax.jComboBox1.setSelectedItem(taxCategory);
});