hercioneto

Movimento.java

Oct 26th, 2022
1,258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.63 KB | None | 0 0
  1.  
  2. package com.mycompany.javacomsql;
  3.  
  4. import java.sql.Connection;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.Date;
  9.  
  10. /**
  11.  *
  12.  * @author user
  13.  */
  14. public class Movimento {
  15.     private int codMovimento;
  16.     private int codCategoria;
  17.     private String descricao;
  18.     private Date data = new Date();
  19.     private String local;
  20.     private double valor;
  21.  
  22.     public int getCodMovimento() {
  23.         return codMovimento;
  24.     }
  25.  
  26.     public void setCodMovimento(int codMovimento) {
  27.         this.codMovimento = codMovimento;
  28.     }
  29.  
  30.     public int getCodCategoria() {
  31.         return codCategoria;
  32.     }
  33.  
  34.     public void setCodCategoria(int codCategoria) {
  35.         this.codCategoria = codCategoria;
  36.     }
  37.  
  38.     public String getDescricao() {
  39.         return descricao;
  40.     }
  41.  
  42.     public void setDescricao(String descricao) {
  43.         this.descricao = descricao;
  44.     }
  45.  
  46.     public Date getData() {
  47.         return data;
  48.     }
  49.  
  50.     public void setData(Date data) {
  51.         this.data = data;
  52.     }
  53.  
  54.     public String getLocal() {
  55.         return local;
  56.     }
  57.  
  58.     public void setLocal(String local) {
  59.         this.local = local;
  60.     }
  61.  
  62.     public double getValor() {
  63.         return valor;
  64.     }
  65.  
  66.     public void setValor(double valor) {
  67.         this.valor = valor;
  68.     }
  69.    
  70.    
  71.     public void listar() {
  72.  
  73.         Conexao conexao = new Conexao();
  74.         Connection conn = conexao.conectar();
  75.  
  76.         try {
  77.             String sql = "SELECT m.codMovimento,m.codCategoria,m.descricao,m.data,m.local,m.valor, c.categoria FROM `movimentos` m INNER join categoria c on c.codCategoria = m.codCategoria;";
  78.  
  79.             Statement stmt = conn.createStatement();
  80.  
  81.             ResultSet rs = stmt.executeQuery(sql);
  82.            
  83.             while (rs.next()) {
  84.                int codMovimento = rs.getInt("codMovimento");
  85.                 int codigoCategoria = rs.getInt("codCategoria");
  86.                 String descricao = rs.getString("descricao");
  87.                 String categoria = rs.getString("categoria");
  88.                 Date data  = rs.getDate("data");
  89.                 String local = rs.getString("local");
  90.                 Double valor = rs.getDouble("valor");
  91.                 System.out.println("Movimento: "+ codMovimento +" Categoria: " + codigoCategoria + " - " + categoria  + " Descrição: " + descricao + " Data: " + data + " Local: "+ local + " Valor: "+ valor);
  92.  
  93.             }
  94.             stmt.close();
  95.             conexao.desconectar(conn);
  96.         } catch (SQLException ex) {
  97.             System.out.println("Erro " + ex);
  98.         }
  99.     }
  100.  
  101.     public void inserir() {
  102.  
  103.         Conexao conexao = new Conexao();
  104.         Connection conn = conexao.conectar();
  105.  
  106.         try {
  107.             String sql = "INSERT INTO 'movimentos' ('codMovimento', 'codCategoria', 'descricao', 'data', 'local', 'valor') VALUES"+
  108.                     "(NULL, '"+this.codCategoria+"', '"+this.descricao+"', '"+this.data+"', '"+this.local+"', '"+this.valor+"')";
  109.  
  110.             Statement stmt = conn.createStatement();
  111.  
  112.             ResultSet rs = stmt.executeQuery(sql);
  113.  
  114.             stmt.close();
  115.             conexao.desconectar(conn);
  116.  
  117.         } catch (SQLException ex) {
  118.             System.out.println("Erro " + ex);
  119.         }
  120.  
  121.     }
  122.    
  123.     public void excluir() {
  124.  
  125.         Conexao conexao = new Conexao();
  126.         Connection conn = conexao.conectar();
  127.        
  128.  
  129.         try {
  130.             String sql = "Delete from `movimentos`  WHERE `codMovimento`.`codMovimento` = "+this.codMovimento;
  131.  
  132.             Statement stmt = conn.createStatement();
  133.  
  134.             ResultSet rs = stmt.executeQuery(sql);
  135.  
  136.             stmt.close();
  137.             conexao.desconectar(conn);
  138.            
  139.  
  140.         } catch (SQLException ex) {
  141.             System.out.println("Erro " + ex);
  142.         }
  143.  
  144.     }
  145.    
  146.    
  147.     public void alterar() {
  148.  
  149.         Conexao conexao = new Conexao();
  150.         Connection conn = conexao.conectar();
  151.        
  152.  
  153.         try {
  154.             String sql = "UPDATE 'movimentos' SET "
  155.                     + "'codCategoria' = '"+this.codCategoria+"', 'descricao' = '"+this.descricao+"', "
  156.                     + "'data' = '"+this.data+"', 'local' = '"+this.local+"', "
  157.                     + "'valor' = '"+this.valor+"' WHERE 'movimentos'.'codMovimento' = "+this.codMovimento+"";
  158.  
  159.             Statement stmt = conn.createStatement();
  160.  
  161.             ResultSet rs = stmt.executeQuery(sql);
  162.  
  163.             stmt.close();
  164.             conexao.desconectar(conn);
  165.            
  166.  
  167.         } catch (SQLException ex) {
  168.             System.out.println("Erro " + ex);
  169.         }
  170.  
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment