@Override public void atualizar(Editora editora) { System.out.println(editora.getCnpj()); Connection conexao = connectionFactory.conectar(); String sql = "UPDATE tbEditora " + "SET cnpj=?, nome=?, logo=? WHERE cnpj=?"; PreparedStatement ps = null; try { ps = conexao.prepareStatement(sql); ps.setString(1, editora.getCnpj()); ps.setString(2, editora.getNome()); ps.setBlob(3, editora.getLogoBlob()); ps.setString(4, editora.getCnpj()); ps.executeUpdate(); ps.close(); conexao.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); } finally { try { ps.close(); conexao.close(); } catch (SQLException e) { e.printStackTrace(); } } }
@Override public void inserir(Editora editora) { Connection conexao = connectionFactory.conectar(); String sql = "INSERT INTO tbEditora(cnpj, nome, logo) " + "VALUES (?,?,?)"; PreparedStatement ps = null; try { ps = conexao.prepareStatement(sql); ps.setString(1, editora.getCnpj()); ps.setString(2, editora.getNome()); ps.setBlob(3, editora.getLogoBlob()); ps.executeUpdate(); ps.close(); conexao.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); } finally { try { ps.close(); conexao.close(); } catch (SQLException e) { e.printStackTrace(); } } }
@Override public Editora getPorId(String cnpj) { Connection conexao = connectionFactory.conectar(); String sql = "SELECT * FROM tbEditora " + "WHERE cnpj=?"; Editora editora = null; PreparedStatement ps = null; ResultSet rs = null; try { ps = conexao.prepareStatement(sql); ps.setString(1, cnpj); rs = ps.executeQuery(); while (rs.next()) { editora = new Editora(); editora.setCnpj(rs.getString("cnpj")); editora.setNome(rs.getString("nome")); editora.setLogoBlob(rs.getBlob("logo")); } } catch (SQLException e) { e.printStackTrace(); } return editora; }
@Override public List<Editora> listar() { Connection conexao = connectionFactory.conectar(); String sql = "SELECT * FROM tbEditora"; List<Editora> editoras = new ArrayList<Editora>(); Statement stmt = null; ResultSet rs = null; try { stmt = conexao.createStatement(); rs = stmt.executeQuery(sql); while (rs.next()) { Editora editora = new Editora(); editora.setCnpj(rs.getString("cnpj")); editora.setLogoBlob(rs.getBlob("logo")); editora.setNome(rs.getString("nome")); editora.setLogo( new DefaultStreamedContent( new ByteArrayInputStream( editora.getLogoBlob().getBytes(1, (int) editora.getLogoBlob().length())))); editoras.add(editora); } stmt.close(); rs.close(); conexao.close(); } catch (SQLException e) { e.printStackTrace(); } finally { try { stmt.close(); rs.close(); conexao.close(); } catch (SQLException e) { e.printStackTrace(); } } return editoras; }