Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Connection;
- import Factory.FactoryCon;
- import Model.Autor;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import javax.swing.JOptionPane;
- /**
- *
- * @author IrlanJR
- */
- public class AutorDAO {
- private Connection con;
- private PreparedStatement pstmtInsert;
- private PreparedStatement pstmtFind;
- private PreparedStatement pstmtFindAll;
- private PreparedStatement pstmtDelete;
- private PreparedStatement pstmtAux;
- private Statement stmtRecords; // Pensei que usaria
- private ResultSet rsRecords;
- public AutorDAO(){
- try {
- con = FactoryCon.getConection(); // Conexão com o banco
- pstmtInsert = con.prepareStatement("INSERT INTO autor(nome,dataNasc) VALUES ( ?,? ); ");
- pstmtDelete = con.prepareStatement("DELETE FROM autor WHERE cod = ?; ");
- pstmtFind = con.prepareStatement("SELECT * FROM autor WHERE cod = ?; ");
- pstmtFindAll = con.prepareStatement("SELECT cod,nome,dataNasc FROM autor;");
- stmtRecords = con.createStatement();
- }
- // catch( Exception e){
- // System.out.println("Erro no carregamento dos statements: "+ e.getMessage());
- // }
- catch( SQLException e){
- System.out.println("Erro no carregamento dos statements: "+ e.getMessage());
- }
- };
- public int inserir(Autor autor) {
- //Retorna valor do código;
- int registro;
- try{
- // Casting que é sempre utilizado em java
- pstmtInsert.setString(1, autor.getNome());
- pstmtInsert.setDate(2, convertDate(autor.getDataNasc()));
- registro = pstmtInsert.executeUpdate();
- JOptionPane.showMessageDialog(null,"Cadastro concluido com sucesso!!");
- pstmtAux = con.prepareStatement("SELECT @@IDENTITY");
- rsRecords = pstmtAux.executeQuery();
- int codigo = 0;
- if((registro == 1) && (rsRecords != null )){
- rsRecords.next();
- codigo = rsRecords.getInt(1);
- return codigo; // Esse código é mostrado na tela.
- }else{
- return codigo; // Esse código é mostrado na tela.
- }
- }
- catch( SQLException e ){
- System.out.println("Erro em AutorDAO.inserir(): "+ e.getMessage());
- return 0;
- }
- }
- public void deletar(int cod) {
- try{
- pstmtDelete.setInt(1, cod);
- pstmtDelete.executeUpdate();
- JOptionPane.showMessageDialog(null,"Usuario excluido com sucesso!!");
- }
- catch( SQLException e ){
- System.out.println("Erro na deleção: "+ e.getMessage());
- }
- }
- public List<Autor> acharTodos() {
- List<Autor> listaAutor = new ArrayList<>();
- ResultSet rs = null;
- try {
- rs = pstmtFindAll.executeQuery();
- while(rs.next()){
- Autor autor = new Autor();
- autor.setCod(rs.getInt("Cod"));
- autor.setNome(rs.getString("Nome"));
- autor.setDataNasc(rs.getDate("dataNasc"));
- listaAutor.add(autor);
- }
- return listaAutor;
- } catch (SQLException ex) {
- JOptionPane.showMessageDialog(null," Erro no carregamento: "+ ex.getMessage());
- return null;
- }
- }
- public Autor achar(int cod){
- try{
- pstmtFind.setInt(1, cod);
- ResultSet rs = pstmtFind.executeQuery();
- if(rs.next()){
- Autor autor = new Autor();
- autor.setCod(rs.getInt("COD"));
- autor.setNome(rs.getString("NOME"));
- autor.setDataNasc(rs.getDate("DATANASC"));
- return autor;
- }else{
- throw new SQLException("Record Not Found");
- }
- }
- catch(SQLException e){
- System.out.println("Erro na busca: "+ e.getMessage());
- return null;
- }
- }
- public java.sql.Date convertDate(Date data) { // Método encontrado na GUJ que ajuda a converter em sql.date
- if(data == null){
- return null;
- }else{
- return new java.sql.Date(data.getTime());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement