Oi Pessoal.
Estou com problema nesse código, quando coloco a condição ‘Where status = 0’ do método ‘Carrega Tabela’, o Jtable não carrega. Alguem saberia me dizer onte está o problema.
public class cadastroCliente extends JInternalFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JButton incluir = null;
private JButton excluir = null;
private JScrollPane jScrollPane = null;
private JTable tabaloca = null;
public int usuario;
public String grupo_usuario;
DefaultTableModel modelo_tabela = new DefaultTableModel(0,3);
Vector ctr_cod_cliente = new Vector(); // @jve:decl-index=0:
Vector ctr_id_cc = new Vector(); // @jve:decl-index=0:
Vector ctr_nome_cliente = new Vector(); // @jve:decl-index=0:
Vector ctr_desc_cliente = new Vector(); // @jve:decl-index=0:
eventoTabela ctr_alteracoes = null; // @jve:decl-index=0:
private JButton btngravar = null;
private JButton btnsair = null;
private DefaultTableModel modelo_tabela1 = null;
private JTextField txtCodCliente = null;
private JTextField txtCliente = null;
private JLabel lblCodCliente = null;
private JLabel lblClienteCadastro = null;
/**
* This is the default constructor
*/
public cadastroCliente() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(800, 280);
this.setContentPane(getJContentPane());
this.setTitle("Cadastro de Cliente");
carrega_tabela();
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
lblClienteCadastro = new JLabel();
lblClienteCadastro.setBounds(new Rectangle(162, 13, 56, 22));
lblClienteCadastro.setText("Cliente:");
lblCodCliente = new JLabel();
lblCodCliente.setBounds(new Rectangle(7, 13, 76, 26));
lblCodCliente.setText("Cód. Cliente:");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getIncluir(), null);
jContentPane.add(getExcluir(), null);
jContentPane.add(getJScrollPane(), null);
jContentPane.add(getBtngravar(), null);
jContentPane.add(getBtnsair(), null);
jContentPane.add(getTxtCodCliente(), null);
jContentPane.add(getTxtCliente(), null);
jContentPane.add(lblCodCliente, null);
jContentPane.add(lblClienteCadastro, null);
}
return jContentPane;
}
//Método para Inclusão de Cliente
private JButton getIncluir() {
if (incluir == null) {
incluir = new JButton();
incluir.setBounds(new Rectangle(470, 13, 128, 25));
incluir.setText("Incluir Cliente");
incluir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
adicionarLinhaInfo();
txtCodCliente.setText("");
txtCliente.setText("");
}
});
}
return incluir;
}
private JButton getExcluir() {
if (excluir == null) {
excluir = new JButton();
excluir.setBounds(new Rectangle(616, 77, 128, 25));
excluir.setText("Excluir");
excluir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
excluiSelecionada();
}
});
}
return excluir;
}
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(6, 48, 594, 171));
jScrollPane.setViewportView(getTabaloca());
}
return jScrollPane;
}
private JTable getTabaloca() {
if (tabaloca == null) {
tabaloca = new JTable(modelo_tabela){
public boolean isCellEditable(int row, int col) {
if (col < 1) {
return false;
} else {
return true;
}
}
};
ctr_alteracoes=new eventoTabela(tabaloca);
TableColumn coluna_cod = tabaloca.getColumnModel().getColumn(0);
TableColumn coluna_cod_cliente = tabaloca.getColumnModel().getColumn(1);
try{
coluna_cod.setCellEditor(new DefaultCellEditor(new JFormattedTextField(new MaskFormatter("********************"))));
coluna_cod_cliente.setCellEditor(new DefaultCellEditor(new JFormattedTextField(new MaskFormatter("****************************************************************************************************"))));
}catch(Exception e){
System.out.println("Erro na criacao da máscara");
}
tabaloca.getColumnModel().getColumn(2).setMaxWidth(0);
tabaloca.getColumnModel().getColumn(2).setMinWidth(0);
tabaloca.getColumnModel().getColumn(2).setPreferredWidth(0);
tabaloca.getColumnModel().getColumn(0).setPreferredWidth(15);
tabaloca.getColumnModel().getColumn(1).setPreferredWidth(200);
tabaloca.getColumnModel().getColumn(0).setHeaderValue("Cod.Cliente");
tabaloca.getColumnModel().getColumn(1).setHeaderValue("Nome do Cliente");
tabaloca.getColumnModel().getColumn(0).setResizable(false);
tabaloca.getColumnModel().getColumn(1).setResizable(false);
tabaloca.getTableHeader().setReorderingAllowed(false);
tabaloca.getModel().addTableModelListener(ctr_alteracoes);
tabaloca.getColumnModel().getColumn(1).equals(false);
}
return tabaloca;
}
public void adicionarLinhaInfo(){
String sql=new String();
ResultSet rs;
PreparedStatement stmt;
sql="insert into cliente(cod_cc, nome) " +
"values ('"+txtCodCliente.getText()+ "','" +txtCliente.getText()+ "')";
try
{
//Conecta com banco
stmt = Principal.ConectaBanco.conn.prepareStatement(sql);
rs = stmt.executeQuery();
}
catch(Exception e)
{
System.out.println("AQUI 1");
}
carrega_tabela();
}
public void excluiSelecionada(){
String sql=new String();
PreparedStatement stmt;
sql="delete from cliente where cod_cc='"+tabaloca.getValueAt(tabaloca.getSelectedRow(), 0)+"'";
try
{
//Conecta com banco
stmt = Principal.ConectaBanco.conn.prepareStatement(sql);
stmt.execute();
modelo_tabela.removeRow(tabaloca.getSelectedRow());
}
catch(Exception e)
{
System.out.println("AQUI 1");
}
}
[b]public void carrega_tabela(){
String sql=new String();
ResultSet rs;
PreparedStatement stmt;
modelo_tabela.setNumRows(0);
sql = "select cod_cc, nome from cliente where status = 0 order by cod_cc";
try
{
//Conecta com banco
stmt = Principal.ConectaBanco.conn.prepareStatement(sql);
rs = stmt.executeQuery();
while(rs.next())
{
modelo_tabela.addRow(new Object[]{rs.getString(1),rs.getString(2)});
}
}
catch(Exception e)
{
System.out.println("AQUI 1");
}
}[/b]
private JButton getBtngravar() {
if (btngravar == null) {
btngravar = new JButton();
btngravar.setText("Gravar");
btngravar.setBounds(new Rectangle(616, 122, 128, 25));
btngravar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
grava_alteracoes();
}
});
}
return btngravar;
}
private void grava_alteracoes(){
String sql;
int linha;
PreparedStatement stmt;
if (!ctr_alteracoes.y.isEmpty()){
for (int cont=0;cont<ctr_alteracoes.y.size();cont++){
linha=Integer.parseInt(ctr_alteracoes.y.get(cont).toString());
sql="update cliente set cod_cc='"+tabaloca.getValueAt(linha, 0)+"'"
+", nome='"+tabaloca.getValueAt(linha, 1)+"'"
+" where cod_cc='" + tabaloca.getValueAt(linha, 0)+"'";
try
{
//Conecta com banco
stmt = Principal.ConectaBanco.conn.prepareStatement(sql);
stmt.execute();
}
catch(Exception e)
{
System.out.println("AQUI 1");
}
}
carrega_tabela();
ctr_alteracoes.y.clear();
}
}
private JButton getBtnsair() {
if (btnsair == null) {
btnsair = new JButton();
btnsair.setText("Sair");
btnsair.setBounds(new Rectangle(616, 169, 128, 25));
btnsair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if(ctr_alteracoes.y.isEmpty()){
dispose();
}
else{
int opcao=JOptionPane.showConfirmDialog(null,"Existem dados nao salvos, deseja sair?","Finalizar",JOptionPane.YES_NO_OPTION);
if (opcao==0) dispose();
}
}
});
}
return btnsair;
}
private DefaultTableModel getModelo_tabela1() {
if (modelo_tabela1 == null) {
modelo_tabela1 = new DefaultTableModel(0, 8);
}
return modelo_tabela1;
}
private JTextField getTxtCodCliente() {
if (txtCodCliente == null) {
try
{
MaskFormatter formato_cod_cliente= new MaskFormatter("**********");
txtCodCliente = new JFormattedTextField(formato_cod_cliente);
txtCodCliente.setBounds(new Rectangle(86, 13, 54, 26));
}
catch (Exception e)
{
JOptionPane.showMessageDialog(this,"Número de caracteres excedido, max. 10.","alerta",JOptionPane.ERROR_MESSAGE);
}
}
return txtCodCliente;
}
private JTextField getTxtCliente() {
if (txtCliente == null) {
try
{
MaskFormatter formato_cliente= new MaskFormatter("****************************************************************************************************");
txtCliente = new JFormattedTextField(formato_cliente);
txtCliente.setBounds(new Rectangle(222, 13, 231, 26));
}
catch (Exception e)
{
JOptionPane.showMessageDialog(this,"Número de caracteres excedido, max. 100.","alerta",JOptionPane.ERROR_MESSAGE);
}
}
return txtCliente;
}
public void IncluirCliente(){
String sql=new String();
ResultSet rs;
PreparedStatement stmt;
sql="insert into cliente(cod_cc, nome) " +
"values ('"+txtCodCliente.getText()+"', '"+ txtCliente.getText()+"')";
try
{
//Conecta com banco
stmt = Principal.ConectaBanco.conn.prepareStatement(sql);
stmt.execute();
txtCodCliente.setText("");
txtCliente.setText("");
}
catch(Exception e)
{
System.out.println("AQUI 1");
}
}
}