Bom. Consegui fazer uma aplicação q tá praticamente “MV” eu acho^^.
Pra ser sincero, eu tenho uma classe, ModeloDeAcesso, que tem os métodos de Conectar e Inserir dados no meu banco de dados.
E tenho uma classe Tela que cuida da parte gráfica com o usuário, recebendo as entradas de dados, e chamando os métodos da ModeloDeAcesso apartir de uma instância única (ModeloDeAcesso foi implementada com Singleton).
A pergunta é: O que falta para eu fazer minha aplicação se encaixar no modelo MVC?
Segue o fonte:
ModeloDeAcesso.java:
package MeuCadastro;
import java.sql.*;
import javax.swing.JOptionPane;
public class ModeloDeAcesso{
String url= "jdbc:derby://localhost:1527/BaseDeDados";
String usuario = "nbuser";
String senha = "nbuser";
Connection con;
private static ModeloDeAcesso ModeloBase;
private ModeloDeAcesso(){
}
public Connection Conecta(){
try
{
System.out.println("Iniciando a conexão com o BD...");
Class.forName("org.apache.derby.jdbc.ClientDriver");
con = DriverManager.getConnection(url,usuario,senha);
System.out.println("Banco de Dados Ok e Pronto...");
return con;
}
catch(ClassNotFoundException cnf)
{
System.out.println("Houve uma ClassNotFoundException: "+cnf);
return null;
}
catch(SQLException sql)
{
System.out.println("Houve uma SQLException: "+sql);
return null;
}
}
public void InsereDados(String nome, String sobrenome, String email, int telefone){
try {
PreparedStatement st2 = con.prepareStatement("insert into \"NBUSER\".\"Usuarios\" values(?,?,?,?)" );
st2.setString(1,nome);
st2.setString(2,sobrenome);
st2.setString(3,email);
st2.setInt(4,telefone);
st2.execute();
st2.close();
con.close();
System.out.println("Dados Adicionados Com Sucesso!");
}
catch(SQLException sql)
{
System.out.println("Erro! "+sql);
}
}
public static ModeloDeAcesso getInstance(){
if (ModeloBase==null){
ModeloBase = new ModeloDeAcesso();
}
return ModeloBase;
}
}
TelaInicial.java:
/**
*
* @author Douglas
*/
public class TelaInicial extends javax.swing.JFrame {
private String Nome,SobreN,EMail,Tel;
private int TelConvertido;
/** Creates new form TelaInicial */
public TelaInicial() {
initComponents();
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
lbNome = new javax.swing.JLabel();
lbSNome = new javax.swing.JLabel();
lbEMail = new javax.swing.JLabel();
lbTelefone = new javax.swing.JLabel();
tfNome = new javax.swing.JTextField();
tfSNome = new javax.swing.JTextField();
tfEMail = new javax.swing.JTextField();
tfTelefone = new javax.swing.JTextField();
btAdicionar = new javax.swing.JButton();
btConsultar = new javax.swing.JButton();
lbStatus = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Easy Cad");
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Cadastro / Consulta"));
lbNome.setText("Nome:");
lbSNome.setText("Sobrenome:");
lbEMail.setText("E-Mail");
lbTelefone.setText("Telefone");
btAdicionar.setText("Adicionar");
btAdicionar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btAdicionarActionPerformed(evt);
}
});
btConsultar.setText("Consultar");
org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(lbSNome)
.add(lbNome)
.add(lbEMail)
.add(lbTelefone))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
.add(btAdicionar)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(btConsultar))
.add(tfTelefone, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
.add(tfEMail, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
.add(tfNome, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
.add(tfSNome, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)))
.add(lbStatus))
.addContainerGap())
);
jPanel1Layout.linkSize(new java.awt.Component[] {btAdicionar, btConsultar}, org.jdesktop.layout.GroupLayout.HORIZONTAL);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(lbNome)
.add(tfNome, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(lbSNome)
.add(tfSNome, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(lbEMail)
.add(tfEMail, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(lbTelefone)
.add(tfTelefone, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(19, 19, 19)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(btConsultar)
.add(btAdicionar))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 36, Short.MAX_VALUE)
.add(lbStatus)
.addContainerGap())
);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void btAdicionarActionPerformed(java.awt.event.ActionEvent evt) {
Nome = tfNome.getText();
SobreN = tfSNome.getText();
EMail = tfEMail.getText();
Tel = tfTelefone.getText();
TelConvertido = Integer.parseInt(Tel);
ModeloDeAcesso acs = ModeloDeAcesso.getInstance();
acs.Conecta();
acs.InsereDados(Nome,SobreN,EMail,TelConvertido);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TelaInicial().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btAdicionar;
private javax.swing.JButton btConsultar;
private javax.swing.JPanel jPanel1;
private javax.swing.JLabel lbEMail;
private javax.swing.JLabel lbNome;
private javax.swing.JLabel lbSNome;
private javax.swing.JLabel lbStatus;
private javax.swing.JLabel lbTelefone;
private javax.swing.JTextField tfEMail;
private javax.swing.JTextField tfNome;
private javax.swing.JTextField tfSNome;
private javax.swing.JTextField tfTelefone;
// End of variables declaration
}
Sim eu utilizei o editor de GUI do netbeans; A parte gráfica do código eu não mexi em nada. Tudo apartir do editor.
Agradeço a quem puder ajudar. Um abraço a todos!