Victor G. Victor G. - 1 month ago 13
Java Question

Doubts to input data from DAO functions to JComboBox

It's my first time here and consequently my first question, i saw a demo application through the net learning to pick a option from the JCombobox and according with it get his corresponding ID but all his database queries, preparedstatements and resultSet methods are directly did on the JCombobox actionPerformed events, and now i'm trying to use this idea on my college final paper application.
I'm trying to do all your code inside the DAO class, for this i did two functions comboboxUse() and getCodPrioridade(), one to return all the itens from the database to JCombobox and this another one to get the id from each item but it's returning a incomplete JCombobox with only the last row, i did like that:

package br.com.jdbc.victor.dao.entidadesdao;

import br.com.jdbc.dao.DAOException;
import br.com.jdbc.victor.transicao.ChamadoDAO;
import br.com.jdbc.victor.vo.Chamado;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

private Connection con;
private PreparedStatement pstm;
private Statement stm = null;
private ResultSet rs = null;


@Override
public String comboboxUse() throws DAOException{
Chamado c = null;
String getpri = null;

try{
getpri = "SELECT * FROM prioridadedetalhe";
stm = con.createStatement();
rs = stm.executeQuery(getpri);

while(rs.next()){
c = new Chamado();
c.setPrioridade(rs.getString("prioridade"));
c.toString();
}

} catch(SQLException ex){
throw new DAOException("Erro no SQL", ex);
} finally {
if(pstm!=null){
try {
pstm.close();
} catch (SQLException ex) {
throw new DAOException("Erro ao fechar conexão", ex);
}
} if(rs!=null){
try {
rs.close();
} catch (SQLException ex) {
throw new DAOException("Erro ao fechar conexão", ex);
}
}
}
return c.getPrioridade();
}


And after i'm trying to call that function in this form:

package br.com.jdbc.victor.view;

import br.com.jdbc.dao.DAOException;
import br.com.jdbc.victor.dao.entidadesdao.MySQLChamadoDAO;
import br.com.jdbc.victor.dao.entidadesdao.MySQLDaoManager;
import br.com.jdbc.victor.vo.Chamado;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;


public class FormNovaChamada extends javax.swing.JDialog {
private Chamado chamado;
private boolean editavel;
private MySQLChamadoDAO chamdao;
private FormInicio frmini;
private MySQLDaoManager man;
private ResultSet rs = null;

int cont = 0;
int i = 0;

public Chamado getChamado() {
return chamado;
}

public void setChamado(Chamado chamado) {
this.chamado = chamado;
}

public boolean isEditavel() {
return editavel;
}


public FormNovaChamada(java.awt.Frame parent, boolean modal) throws SQLException {
super(parent, modal);
initComponents();
desabilitarTextField();
this.cbPrioridade.removeAllItems();
try {
MySQLDaoManager man = new MySQLDaoManager("root", "", "localhost", "atendimentos", 3306);
this.cbPrioridade.addItem(man.getChamadoDAO().comboboxUse());
cont++;
} catch (DAOException ex) {
Logger.getLogger(FormNovaChamada.class.getName()).log(Level.SEVERE, null, ex);
}
}


And it's my database:

+---------------------+------------+
| idPrioridadeDetalhe | prioridade |
+---------------------+------------+
| 1 | alta |
| 2 | média |
| 3 | baixa |
+---------------------+------------+


enter image description here

Apparently it didn't give any errors or something like that, it gets the last data of the column rather than get all the database values to insert in the JCombobox... i'd like to know what could i do?! I don't know if my question was clear, thanks anyway!!

Answer

assuming cbPrioridade is your JComboBox component, check why you add just one item there:

 this.cbPrioridade.addItem(man.getChamadoDAO().comboboxUse());

and before that check why you return just string from db

public String comboboxUse() throws DAOException{

you return just last element from db instead of returning list