Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FabricanteDAO {
- ConexaoFactory cf;
- PreparedStatement pst;
- ArrayList<Fabricante> lista = new ArrayList<Fabricante>();
- ArrayList<Fabricante> lista2 = new ArrayList<Fabricante>();;
- Fabricante retorno = null;
- public void salvar(Fabricante f){
- StringBuilder sql = new StringBuilder();
- sql.append("INSERT INTO fabricante");
- sql.append("(descricao) ");
- sql.append("VALUES (?)");
- try {
- Connection conexao = cf.conectar();
- PreparedStatement pst = conexao.prepareStatement(sql.toString());
- pst.setString(1,f.getDescricao());
- pst.executeUpdate();
- JOptionPane.showMessageDialog(null,"Salvo com Sucesso !");
- }catch(SQLException ex) {
- JOptionPane.showMessageDialog(null,"Erro ao Salvar: "+ex.getMessage());
- }
- }
- public void excluir(Fabricante f){
- StringBuilder sql = new StringBuilder();
- sql.append("DELETE FROM fabricante ");
- sql.append("WHERE codigo = ? ");
- try {
- Connection conexao = cf.conectar();
- PreparedStatement pst = conexao.prepareStatement(sql.toString());
- pst.setLong(1,f.getCodigo());
- pst.executeUpdate();
- JOptionPane.showMessageDialog(null,"Salvo com Sucesso !");
- }catch(SQLException ex) {
- JOptionPane.showMessageDialog(null,"Erro ao Salvar: "+ex.getMessage());
- }
- }
- public void editar(Fabricante f){
- StringBuilder sql = new StringBuilder();
- sql.append("UPDATE fabricante ");
- sql.append("SET descricao = ?");
- try {
- Connection conexao = cf.conectar();
- PreparedStatement pst = conexao.prepareStatement(sql.toString());
- pst.setString(1,f.getDescricao());
- pst.setLong(2,f.getCodigo());
- pst.executeUpdate();
- JOptionPane.showMessageDialog(null,"Salvo com Sucesso !");
- }catch(SQLException ex) {
- JOptionPane.showMessageDialog(null,"Erro ao Salvar: "+ex.getMessage());
- }
- }
- public Fabricante consulta(Fabricante f){
- StringBuilder sql = new StringBuilder();
- sql.append("SELECT codigo, descricao ");
- sql.append("FROM fabricante ");
- sql.append("WHERE codigo = ?");
- try {
- Connection conexao = cf.conectar();
- PreparedStatement pst = conexao.prepareStatement(sql.toString());
- pst.setLong(1,f.getCodigo());
- ResultSet rs = pst.executeQuery();
- if(rs.next()){
- retorno = new Fabricante();
- retorno.setCodigo(rs.getLong("codigo"));
- retorno.setDescricao(rs.getString("descricao"));
- }
- }catch(SQLException ex) {
- JOptionPane.showMessageDialog(null,"Erro ao Salvar: "+ex.getMessage());
- }
- return retorno;
- }
- public ArrayList<Fabricante> listar() throws SQLException{
- StringBuilder sql = new StringBuilder();
- sql.append("SELECT codigo, descricao ");
- sql.append("FROM fabricante ");
- sql.append("WHERE BY descricao ASC ");
- Connection conexao = cf.conectar();
- PreparedStatement pst = conexao.prepareStatement(sql.toString());
- ResultSet rs = pst.executeQuery();
- while(rs.next()){
- Fabricante f = new Fabricante();
- f.setCodigo(rs.getLong("codigo"));
- f.setDescricao(rs.getString("descricao"));
- lista.add(f);
- }
- return lista;
- }
- public ArrayList<Fabricante> listarDesc(Fabricante f){
- StringBuilder sql = new StringBuilder();
- sql.append("SELECT codigo, descricao ");
- sql.append("FROM fabricante ");
- sql.append("WHERE descricao LIKE = ? ");
- sql.append("ORDER BY descricao ASC ");
- try {
- cf.conn = cf.conectar();
- PreparedStatement pst = cf.conn.prepareStatement(sql.toString());
- pst.setString(1, "%"+f.getDescricao()+"%");
- ResultSet rs = pst.executeQuery();
- while(rs.next()){
- Fabricante item = new Fabricante();
- item.setCodigo(rs.getLong("codigo"));
- item.setDescricao(rs.getString("descricao"));
- lista2.add(item);
- }
- }catch(SQLException ex) {
- JOptionPane.showMessageDialog(null,"Erro ao Salvar: "+ex.getMessage());
- }
- return lista2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement