Olá pessoal!
Tudo certo?
Gostaria de saber o que acham do meu código em DAO…
Se está conforme as “convenções”, etc…
Gostaria de dizer que estou fazendo em netbeans e é só um teste para “treinar”… portanto nada de WEB…
Segue os códigos das minhas classes, quem puder dar uma olhada ficarei muito grata…
1. Classe: AutorDAO
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package autor;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author D908
*/
public class AutorDAO {
public Connection getConnection() throws SQLException{
//Código para recuperar conexão com o Banco de Dados
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(AutorDAO.class.getName()).log(Level.SEVERE, null, ex);
}
Connection con = DriverManager.getConnection("jdbc:postgresql://localhost/livraria", "postgres", "admin");
return con;
}
public void adicionarAutor(AutorModel autor) throws SQLException {
Connection con = this.getConnection();
String sql = ("INSERT INTO autor (nome, titulacao, curriculo) VALUES(?,?,?)") ;
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, autor.getNome());
pst.setString(2, autor.getTitulacao());
pst.setString(3, autor.getCurriculoResumido());
pst.executeUpdate();
pst.close();
con.close();
}
public void excluirAutor(AutorModel autor) throws SQLException {
Connection con = this.getConnection();
String sql = ("DELETE FROM autor WHERE(nome=?)") ;
PreparedStatement pst = con.prepareCall(sql);
pst.setString(1, autor.getNome());
pst.executeUpdate();
pst.close();
con.close();
}
public void consultarAutor(AutorModel autor) throws SQLException{
Connection con = this.getConnection();
String sql = ("SELECT * from autor where (nome=?)");
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, autor.getNome());
ResultSet rs = pst.executeQuery();
rs.next();
autor.setNome(rs.getString("nome"));
autor.setTitulacao(rs.getString("titulacao"));
autor.setCurriculoResumido(rs.getString("curriculo"));
rs.close();
pst.close();
con.close();
}
public void atualizarAutor(AutorModel autor) throws SQLException{
Connection con = this.getConnection();
String sql = sql = ("UPDATE autor set titulacao = ?, curriculo = ? where nome =?");
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, autor.getTitulacao());
pst.setString(2, autor.getCurriculoResumido());
pst.setString(3, autor.getNome());
pst.executeUpdate();
pst.close();
con.close();
}
}
2. Classe: Autor
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Autor.java
*
* Created on 12/03/2010, 19:46:59
*/
package autor;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Proprietário
*/
public class Autor extends javax.swing.JFrame {
/** Creates new form Autor */
public Autor() {
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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btnSalvar = new javax.swing.JButton();
btnExcluir = new javax.swing.JButton();
btnConsultar = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
btnLimpar = new javax.swing.JButton();
btnAtualizar = new javax.swing.JButton();
FormListener formListener = new FormListener();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Autor");
btnSalvar.setText("Salvar");
btnSalvar.addActionListener(formListener);
btnExcluir.setText("Excluir");
btnExcluir.addActionListener(formListener);
btnConsultar.setText("Consultar");
btnConsultar.addActionListener(formListener);
jLabel1.setText("Nome:");
jLabel2.setText("Titulação:");
jLabel3.setText("Currículo Resumido:");
btnLimpar.setText("Limpar");
btnLimpar.addActionListener(formListener);
btnAtualizar.setText("Atualizar");
btnAtualizar.addActionListener(formListener);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField3)
.addComponent(jTextField2, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addComponent(btnLimpar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnSalvar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnExcluir)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnConsultar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnAtualizar)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnSalvar)
.addComponent(btnExcluir)
.addComponent(btnConsultar)
.addComponent(btnLimpar)
.addComponent(btnAtualizar))
.addContainerGap(60, Short.MAX_VALUE))
);
pack();
}
// Code for dispatching events from components to event handlers.
private class FormListener implements java.awt.event.ActionListener {
FormListener() {}
public void actionPerformed(java.awt.event.ActionEvent evt) {
if (evt.getSource() == btnSalvar) {
Autor.this.btnSalvarActionPerformed(evt);
}
else if (evt.getSource() == btnExcluir) {
Autor.this.btnExcluirActionPerformed(evt);
}
else if (evt.getSource() == btnConsultar) {
Autor.this.btnConsultarActionPerformed(evt);
}
else if (evt.getSource() == btnLimpar) {
Autor.this.btnLimparActionPerformed(evt);
}
else if (evt.getSource() == btnAtualizar) {
Autor.this.btnAtualizarActionPerformed(evt);
}
}
}// </editor-fold>
private void limparCampos(){
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
}
private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {
AutorModel autor = new AutorModel();
autor.setNome(jTextField1.getText());
autor.setTitulacao(jTextField2.getText());
autor.setCurriculoResumido(jTextField3.getText());
AutorDAO autorBD = new AutorDAO();
try {
autorBD.adicionarAutor(autor);
} catch (SQLException ex) {
Logger.getLogger(Autor.class.getName()).log(Level.SEVERE, null, ex);
}
limparCampos();
}
private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {
AutorModel autor = new AutorModel();
autor.setNome(jTextField1.getText());
AutorDAO autorBD = new AutorDAO();
try {
autorBD.excluirAutor(autor);
} catch (SQLException ex) {
Logger.getLogger(Autor.class.getName()).log(Level.SEVERE, null, ex);
}
limparCampos();
}
private void btnLimparActionPerformed(java.awt.event.ActionEvent evt) {
limparCampos();
}
private void btnConsultarActionPerformed(java.awt.event.ActionEvent evt) {
AutorModel autor = new AutorModel();
autor.setNome(jTextField1.getText());
AutorDAO autorBD = new AutorDAO();
try {
autorBD.consultarAutor(autor);
} catch (SQLException ex) {
Logger.getLogger(Autor.class.getName()).log(Level.SEVERE, null, ex);
}
jTextField1.setText(autor.getNome());
jTextField2.setText(autor.getTitulacao());
jTextField3.setText(autor.getCurriculoResumido());
}
private void btnAtualizarActionPerformed(java.awt.event.ActionEvent evt) {
AutorModel autor = new AutorModel();
autor.setNome(jTextField1.getText());
autor.setTitulacao(jTextField2.getText());
autor.setCurriculoResumido(jTextField3.getText());
AutorDAO autorBD = new AutorDAO();
try {
autorBD.atualizarAutor(autor);
} catch (SQLException ex) {
Logger.getLogger(Autor.class.getName()).log(Level.SEVERE, null, ex);
}
limparCampos();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Autor().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnAtualizar;
private javax.swing.JButton btnConsultar;
private javax.swing.JButton btnExcluir;
private javax.swing.JButton btnLimpar;
private javax.swing.JButton btnSalvar;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
// End of variables declaration
}
3. Classe: AutorModel
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package autor;
/**
*
* @author D908
*/
public class AutorModel {
private String nome;
private String titulacao;
private String curriculoResumido;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getTitulacao() {
return titulacao;
}
public void setTitulacao(String titulacao) {
this.titulacao = titulacao;
}
public String getCurriculoResumido() {
return curriculoResumido;
}
public void setCurriculoResumido(String curriculoResumido) {
this.curriculoResumido = curriculoResumido;
}
}
[color=red]Está rodando sem problemas, gostaria apenas de saber se a forma que fiz é uma considerada uma boa forma, e caso não seja, gostaria de saber o motivo…
Estou treinando isso para poder fazer/aplicar essas técnicas em um sistema maior do meu TCC…[/color]
[color=violet]Agradeço desde já,
Dina[/color]
Eba! Isso que eu quero, saber o que tem que mudar pra ficar melhor… 
