Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package exercicio;
- 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 christian moreira
- github: github.com/crmon */
- public class ConnectionFactory {
- private static final String connectionUrl = "jdbc:sqlserver://localhost:1433;"
- +"databaseName=FEIRA;user=usuario;password=111";
- public static Connection getConnection(){
- try{
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- return DriverManager.getConnection(connectionUrl);
- }
- catch(ClassNotFoundException | SQLException ex){
- throw new RuntimeException("Erro na conexão", ex);
- }
- }
- public static void closeConnection(Connection con){
- try{
- if(con != null){
- con.close();
- }
- }catch(SQLException ex){
- Logger.getLogger(ProdutoDAO.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public static void closeConnection(Connection con, PreparedStatement stmt){
- closeConnection(con);
- try{
- if(stmt != null){
- stmt.close();
- }
- }catch(SQLException ex){
- Logger.getLogger(ProdutoDAO.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public static void closeConnection(Connection con, PreparedStatement stmt, ResultSet rs){
- closeConnection(con, stmt);
- try{
- if(rs != null){
- rs.close();
- }
- }catch(SQLException ex){
- Logger.getLogger(ProdutoDAO.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- /* ================================= Inicio Produto ================================= */
- package exercicio;
- /*@author christian moreira
- github: github.com/crmon */
- public class Produto {
- private int cod;
- private String nome;
- public Produto() {}
- public Produto(int cod, String nome) {
- this.cod = cod;
- this.nome = nome;
- }
- public Produto(String nome) {
- this.nome = nome;
- }
- public int getCod() {
- return cod;
- }
- public void setCod(int cod) {
- this.cod = cod;
- }
- public String getNome() {
- return nome;
- }
- public void setNome(String nome) {
- this.nome = nome;
- }
- }
- /* ================================= Inicio ProdutoDAO ================================= */
- package exercicio;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /*@author christian moreira
- github: github.com/crmon */
- public class ProdutoDAO {
- public void adicionar(Produto produto){
- Connection con = ConnectionFactory.getConnection();
- PreparedStatement stmt = null;
- try{
- stmt = con.prepareStatement("INSERT INTO PRODUTO(NOME) VALUES(?)");
- stmt.setString(1, produto.getNome());
- stmt.execute();
- }
- catch(SQLException ex) {
- Logger.getLogger(ProdutoDAO.class.getName()).log(Level.SEVERE, null, ex);
- }finally{
- ConnectionFactory.closeConnection(con, stmt);
- }
- }
- public ArrayList<Produto> listar(){
- Connection con = ConnectionFactory.getConnection();
- PreparedStatement stmt = null;
- ResultSet rs = null;
- ArrayList<Produto> produtos = new ArrayList<>();
- try{
- stmt = con.prepareStatement("SELECT * FROM PRODUTO");
- rs = stmt.executeQuery();
- while(rs.next()){
- Produto produto = new Produto(rs.getInt("COD"), rs.getString("NOME"));
- produtos.add(produto);
- }
- }
- catch(SQLException ex){
- Logger.getLogger(ProdutoDAO.class.getName()).log(Level.SEVERE, null, ex);
- }
- finally{
- ConnectionFactory.closeConnection(con, stmt, rs);
- }
- return produtos;
- }
- public void alterar(Produto produto){
- Connection con = ConnectionFactory.getConnection();
- PreparedStatement stmt = null;
- try{
- stmt = con.prepareStatement("UPDATE PRODUTO SET NOME = ? WHERE COD = ?");
- stmt.setString(1, produto.getNome());
- stmt.setInt(2, produto.getCod());
- stmt.executeUpdate();
- stmt.close();
- }
- catch(SQLException ex) {
- Logger.getLogger(ProdutoDAO.class.getName()).log(Level.SEVERE, null, ex);
- }finally{
- ConnectionFactory.closeConnection(con, stmt);
- }
- }
- public void deletar(Produto produto){
- Connection con = ConnectionFactory.getConnection();
- PreparedStatement stmt = null;
- try{
- stmt = con.prepareStatement("DELETE FROM PRODUTO WHERE COD = ?");
- stmt.setInt(1, produto.getCod());
- stmt.executeUpdate();
- stmt.close();
- }
- catch(SQLException ex) {
- Logger.getLogger(ProdutoDAO.class.getName()).log(Level.SEVERE, null, ex);
- }finally{
- ConnectionFactory.closeConnection(con, stmt);
- }
- }
- public void limpar(){
- Connection con = ConnectionFactory.getConnection();
- PreparedStatement stmt = null;
- try{
- stmt = con.prepareStatement("DELETE FROM PRODUTO");
- stmt.executeUpdate();
- stmt.close();
- }
- catch(SQLException ex) {
- Logger.getLogger(ProdutoDAO.class.getName()).log(Level.SEVERE, null, ex);
- }finally{
- ConnectionFactory.closeConnection(con, stmt);
- }
- }
- }
- /* ================================= Inicio MyList ================================= */
- package exercicio;
- import exercicio.ProdutoDAO;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.FlowLayout;
- import java.awt.Font;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import javax.swing.DefaultListCellRenderer;
- import javax.swing.DefaultListModel;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JList;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- /*@author christian moreira
- github: github.com/crmon */
- public class MyList extends JFrame {
- private JList<String> controllerLista;
- private DefaultListModel<String> modelLista;
- private DefaultListCellRenderer viewLista;
- private JPanel pnlCentral;
- private JTextField txtTarefa;
- private JButton btnAdicionar, btnLimpar, btnDeletar, btnAlterar;
- private ProdutoDAO produtoDAO = new ProdutoDAO();
- private int index;
- public MyList() {
- super("My List v1.0");
- setLayout(new BorderLayout());
- viewLista = new DefaultListCellRenderer();
- modelLista = new DefaultListModel();
- controllerLista = new JList<String>(modelLista);
- pnlCentral = new JPanel(new FlowLayout());
- btnAdicionar = new JButton("Adicionar");
- btnLimpar = new JButton("Limpar");
- btnDeletar = new JButton("Deletar");
- btnAlterar = new JButton("Alterar");
- txtTarefa = new JTextField(20);
- produtoDAO = new ProdutoDAO();
- }
- public void init() {
- setSize(640, 480);
- setLocationRelativeTo(null);
- JLabel lblTitulo = new JLabel("Minha Lista");
- getContentPane().add(lblTitulo, BorderLayout.NORTH);
- getContentPane().add(controllerLista, BorderLayout.CENTER);
- getContentPane().add(pnlCentral, BorderLayout.SOUTH);
- pnlCentral.add(txtTarefa);
- pnlCentral.add(btnAdicionar);
- pnlCentral.add(btnLimpar);
- pnlCentral.add(btnDeletar);
- pnlCentral.add(btnAlterar);
- atualizarModelList();
- controllerLista.setBackground(Color.black);
- controllerLista.setFont(new Font("Verdana", Font.BOLD, 15));
- controllerLista.setForeground(Color.white);
- lblTitulo.setFont(new Font("Verdana", Font.BOLD, 30));
- lblTitulo.setForeground(Color.black);
- controllerLista.addListSelectionListener((lse) -> {
- txtTarefa.setText(controllerLista.getSelectedValue());
- index = controllerLista.getSelectedIndex();
- });
- btnAdicionar.addActionListener((ae) -> {
- Produto produto = new Produto();
- produto.setNome(txtTarefa.getText());
- produtoDAO.adicionar(produto);
- atualizarModelList();
- txtTarefa.setText("");
- });
- btnLimpar.addActionListener((ae) -> {
- produtoDAO.limpar();
- atualizarModelList();
- txtTarefa.setText("");
- });
- btnDeletar.addActionListener((ae) -> {
- produtoDAO.deletar(produtoDAO.listar().get(index));
- atualizarModelList();
- txtTarefa.setText("");
- });
- btnAlterar.addActionListener((ae) -> {
- if(txtTarefa.getText() != null ){
- ArrayList<Produto> produtos = produtoDAO.listar();
- Produto produto = new Produto(produtos.get(index).getCod(), txtTarefa.getText());
- produtoDAO.alterar(produto);
- atualizarModelList();
- txtTarefa.setText("");
- }
- });
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setVisible(true);
- }
- public static void main(String[] args) {
- new MyList().init();
- }
- public void atualizarModelList(){
- modelLista.setSize(0);
- for(Produto p : produtoDAO.listar()){
- modelLista.addElement(p.getNome());
- }
- }
- }
- /* ================================= FIM MyList ================================= */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement