public static List<AtividadeComercial> getListOfAtividadeComercial() { List<AtividadeComercial> lista = new ArrayList<AtividadeComercial>(); Connection connection = ConnectionFactory.getConnection("mydb", "root", "root"); String sql = "select * from AtividadeComercial"; ResultSet rs = null; PreparedStatement stmt = null; try { stmt = connection.prepareStatement(sql); rs = stmt.executeQuery(); while (rs.next()) { AtividadeComercial atividade = new AtividadeComercial(); atividade.setIdAtividadeComercial(rs.getInt("idAtividadeComercial")); atividade.setNomeAtividade(rs.getString("nomeAtividade")); lista.add(atividade); } rs.close(); stmt.close(); return lista; } catch (SQLException ex) { Logger.getLogger(AtividadeComercialDAO.class.getName()).log(Level.SEVERE, null, ex); return null; } }
public static void adiciona(AtividadeComercial atividade) { Connection connection = ConnectionFactory.getConnection("mydb", "root", "root"); String sql = "insert into AtividadeComercial " + "(nomeAtividade)" + " values (?)"; try { // prepared statement para inserção PreparedStatement stmt = connection.prepareStatement(sql); // seta os valores stmt.setString(1, atividade.getNomeAtividade()); // executa stmt.execute(); stmt.close(); } catch (SQLException e) { throw new RuntimeException(e); } }