I want to the admin user to select an Id from a MySql database table and show an id list of the other columns that are assigned to the original Id that the person selected in a JComboBox and JTextField(based id the person selected in the other combobox field). It is a Foreign Key that references to another table. I think it is easyer to understand looking at it than descrybing it:
visual description on what I’m trying to do
My DAO select operation is:
public Alarme[] obterAlarme() throws Exception{
String sql = "SELECT * FROM tb_timer";
try (Connection conn = ConexaoBD.obterConexao(); PreparedStatement ps = conn.prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY); ResultSet rs = ps.executeQuery()) {
int totalDeAlarmes = rs.last() ? rs.getRow() : 0;
Alarme[] alarme = new Alarme[totalDeAlarmes];
rs.beforeFirst();
int contador = 0;
while (rs.next()) {
int idHabito = rs.getInt("fk_habito_id");
int id = rs.getInt("id");
String horaToque = String.valueOf(rs.getTime("hora_toque"));
alarme[contador++] = new Alarme(idHabito, id, horaToque);
}
return alarme;
}
}
My JFrame methods:
public TelaAlarmes() {
super("Alarme"); //class with constructors of the tb_timer fields
initComponents();
obterAlarmes();
setLocationRelativeTo(null);
}
//this is initially assignning the "alarme" data to the foreign key field (not what I want, if you got it right)
private void obterAlarmes(){
try {
DAO dao = new DAO();
Alarme[] alarme = dao.obterAlarme();
cbbIdHabito.setModel(new DefaultComboBoxModel<>(alarme));
}
catch(Exception e){
JOptionPane.showMessageDialog(null, "Alarmes indisponíveis. Tente novamente mais tarde!");
e.printStackTrace();
}
}
I think I would need to do another DAO operation and assign the values just as I did with the “cbbIdHabito” field, but I don’t know how to do it properly.
Iann Schmith is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.