Pessoal seguinte, sou novo em Java, gostaria de saber o seguinte.
Quero separar tudo em packages, (tipo dentro de Cadatros manter apenas o que refere a cadastro.)
So que tem um detalhe, estou quebrando cabeça para fazer um unico arquivo que guarde a conexão, e depois apenas chamar esta conexão para inserir mostrar, ou alterar.
Mas acabei parando de cara ja na inserção porque qdo tento colocar o metodo CallableStatment, ele da erro, dis que eu preciso criar metodo PrepareCall em conexao, ai tentei mas num fui feliz…
Sera que vcs podem me ajudar… Segue codigo abaixo…
[code]import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class frmteste extends javax.swing.JFrame {
/** Creates new form frmteste */
public frmteste() {
initComponents();
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
numero = new javax.swing.JTextField();
nome = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null},
{null, null},
{null, null},
{null, null}
},
new String [] {
"Numero", "Nome"
}
) {
Class[] types = new Class [] {
java.lang.Integer.class, java.lang.String.class
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
jScrollPane1.setViewportView(jTable1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(89, 89, 89)
.addComponent(jButton1)
.addGap(79, 79, 79)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(numero)
.addComponent(nome, javax.swing.GroupLayout.DEFAULT_SIZE, 58, Short.MAX_VALUE))))
.addContainerGap(15, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(28, 28, 28)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(numero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(nome, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(29, 29, 29))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (numero.getText().equals(""))
{
JOptionPane.showMessageDialog(null,"O campo Numero não poder Null");
}else{
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/agenda?useUnicode=true", "root", "mysql");
CallableStatement cal = conn.prepareCall("{call sp_insereContato(?, ?)}");
cal.setInt(1,Integer.parseInt(numero.getText()));
cal.setString(2,nome.getText());
cal.executeQuery();
CallableStatement stm = conn.prepareCall("{call sp_mostraContato()}");
ResultSet res = stm.executeQuery();
jTable1.getColumnModel().getColumn(0).setPreferredWidth(20);
jTable1.getColumnModel().getColumn(1).setPreferredWidth(20);
DefaultTableModel modelo = (DefaultTableModel)jTable1.getModel();
modelo.setNumRows(0);
try
{
while (res.next())
modelo.addRow(new Object []
{
res.getString("numero"),
res.getString("nome")
});
res.first();
}
catch (SQLException erro)
{
JOptionPane.showMessageDialog(null,"Erro ao listar no JTable "+erro);
}
}
catch (ClassNotFoundException ex) {
Logger.getLogger(frmteste.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException er )
{
JOptionPane.showMessageDialog(null,"Este Valor ja existe");
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new frmteste().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField nome;
private javax.swing.JTextField numero;
// End of variables declaration [/code]