Olá boa noite, estou tendo um problema para povoar uma Jtable com um relacionamento que eu fiz.
Criei um CRUD de aluno e outro de Turma, logo em seguida criei uma janela onde vou adicionar alunos em turmas. Fiz da seguinte forma.
Model
package br.com.jeff.model.bean;
/**
*
-
@author jeff-
*/
public class ModelTurmaAluno {private AlunoModel aluno;
private TurmaModel turma;public AlunoModel getAluno() {
return aluno;
}public void setAluno(AlunoModel aluno) {
this.aluno = aluno;
}public TurmaModel getTurma() {
return turma;
}public void setTurma(TurmaModel turma) {
this.turma = turma;
}
}
class DAO
package br.com.jeff.model.dao;
import br.com.jeff.factory.ConnectionFactory;
import br.com.jeff.model.bean.ModelTurmaAluno;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
-
@author jeff-
*/
public class AlunoTurmaDAO {public void create(ModelTurmaAluno mta) {
Connection con = ConnectionFactory.getConnection();
PreparedStatement stmt = null;
try {
stmt = con.prepareStatement(“insert into aluno_turma(id_aluno_turma,id_turma_aluno)values(?,?)”);
stmt.setInt(1, mta.getAluno().getCodigo());
stmt.setInt(2, mta.getTurma().getCodigo());
stmt.executeUpdate();
JOptionPane.showMessageDialog(null, “Dados inseridos com sucesso!”);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
} finally {
ConnectionFactory.closeConnection(con, stmt);
}
}public List read1() {
Connection con = ConnectionFactory.getConnection();
PreparedStatement stmt = null;
ResultSet rs = null;List<ModelTurmaAluno> turmaalunos = new ArrayList<>(); try { stmt = con.prepareStatement("select * from aluno_turma order by id_aluno_turma"); rs = stmt.executeQuery(); while (rs.next()) { ModelTurmaAluno model = new ModelTurmaAluno(); model.setAluno(rs.getString("id_aluno_turma")); Aqui nessa parte que estou com dificuldade } } catch (SQLException ex) { Logger.getLogger(AlunoTurmaDAO.class.getName()).log(Level.SEVERE, null, ex); } return null;
}
}
View
package br.com.jeff.view;
import br.com.jeff.model.bean.AlunoModel;
import br.com.jeff.model.bean.ModelTurmaAluno;
import br.com.jeff.model.bean.TurmaModel;
import br.com.jeff.model.dao.AlunoDAO;
import br.com.jeff.model.dao.AlunoTurmaDAO;
import br.com.jeff.model.dao.TurmaDAO;
import java.util.ArrayList;
import java.util.List;
/**
*
-
@author jeff-
*/
public class FrmAlunoTurma extends javax.swing.JFrame {public FrmAlunoTurma() {
initComponents();List<AlunoModel> listaAluno = new ArrayList<>(); AlunoDAO alunoDao = new AlunoDAO(); listaAluno = alunoDao.read(); for (AlunoModel al : listaAluno) { cb_aluno.addItem(al.getNome()); } List<TurmaModel> listaTurma = new ArrayList<>(); TurmaDAO turmaDao = new TurmaDAO(); listaTurma = turmaDao.read(); for (TurmaModel tm : listaTurma) { cb_turma.addItem(tm.getDescricao()); }
}
/**
-
This method is called from within the constructor to initialize the form.
-
WARNING: Do NOT modify this code. The content of this method is always
-
regenerated by the Form Editor.
*/
@SuppressWarnings(“unchecked”)
//
private void initComponents() {cb_aluno = new javax.swing.JComboBox<>();
cb_turma = new javax.swing.JComboBox<>();
bt_salvar = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
tabelaTurmaAluno = new javax.swing.JTable();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
bt_salvar.setText(“Salvar”);
bt_salvar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt_salvarActionPerformed(evt);
}
});jLabel1.setText(“Aluno.:”);
jLabel2.setText(“Turma.:”);
tabelaTurmaAluno.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null},
{null, null},
{null, null},
{null, null}
},
new String [] {
“Title 1”, “Title 2”
}
));
jScrollPane1.setViewportView(tabelaTurmaAluno);javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(34, 34, 34)
.addComponent(bt_salvar)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(cb_turma, 0, 153, Short.MAX_VALUE)
.addComponent(cb_aluno, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addGap(35, 35, 35)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addComponent(jLabel2)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 62, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(33, 33, 33))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(42, 42, 42)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(cb_aluno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel2)
.addGap(10, 10, 10)
.addComponent(cb_turma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
.addComponent(bt_salvar)
.addGap(39, 39, 39))
);pack();
}//
private void bt_salvarActionPerformed(java.awt.event.ActionEvent evt) {
//Recuperar ID de Turma
String itemTurma = (String) cb_turma.getSelectedItem();
TurmaModel modelTurma = new TurmaModel();
TurmaDAO turmaDao = new TurmaDAO();
modelTurma.setDescricao(itemTurma);
modelTurma = turmaDao.buscarTurma(modelTurma);//Recuperar ID de Aluno String itemAluno = (String) cb_aluno.getSelectedItem(); AlunoModel modelAluno = new AlunoModel(); AlunoDAO alunoDao = new AlunoDAO(); modelAluno.setNome(itemAluno); modelAluno = alunoDao.buscarAluno(modelAluno); ModelTurmaAluno turmaaluno = new ModelTurmaAluno(); AlunoTurmaDAO daoAlunoTurma = new AlunoTurmaDAO(); modelAluno.setCodigo(modelAluno.getCodigo()); modelTurma.setCodigo(modelTurma.getCodigo()); turmaaluno.setAluno(modelAluno); turmaaluno.setTurma(modelTurma); daoAlunoTurma.create(turmaaluno);
}
-
Meu problema seria na Classe Dao quando eu instacio o ModelTurmaAluno e depois nao consigo adicionar o result set no modelo criado.