hercioneto

RSTI - Movimento.java

Oct 27th, 2022
1,331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.66 KB | None | 0 0
  1. package com.mycompany.javacomsql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.Date;
  8.  
  9. /**
  10.  *
  11.  * @author user
  12.  */
  13. public class Movimento {
  14.     private int codMovimento;
  15.     private int codCategoria;
  16.     private String descricao;
  17.     private Date data = new Date();
  18.     private String local;
  19.     private double valor;
  20.  
  21.     public int getCodMovimento() {
  22.         return codMovimento;
  23.     }
  24.  
  25.     public void setCodMovimento(int codMovimento) {
  26.         this.codMovimento = codMovimento;
  27.     }
  28.  
  29.     public int getCodCategoria() {
  30.         return codCategoria;
  31.     }
  32.  
  33.     public void setCodCategoria(int codCategoria) {
  34.         this.codCategoria = codCategoria;
  35.     }
  36.  
  37.     public String getDescricao() {
  38.         return descricao;
  39.     }
  40.  
  41.     public void setDescricao(String descricao) {
  42.         this.descricao = descricao;
  43.     }
  44.  
  45.     public Date getData() {
  46.         return data;
  47.     }
  48.  
  49.     public void setData(Date data) {
  50.         this.data = data;
  51.     }
  52.  
  53.     public String getLocal() {
  54.         return local;
  55.     }
  56.  
  57.     public void setLocal(String local) {
  58.         this.local = local;
  59.     }
  60.  
  61.     public double getValor() {
  62.         return valor;
  63.     }
  64.  
  65.     public void setValor(double valor) {
  66.         this.valor = valor;
  67.     }
  68.    
  69.    
  70.     public void listar() {
  71.  
  72.         Conexao conexao = new Conexao();
  73.         Connection conn = conexao.conectar();
  74.  
  75.         try {
  76.             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;";
  77.  
  78.             Statement stmt = conn.createStatement();
  79.  
  80.             ResultSet rs = stmt.executeQuery(sql);
  81.            
  82.             while (rs.next()) {
  83.                int codMovimento = rs.getInt("codMovimento");
  84.                 int codigoCategoria = rs.getInt("codCategoria");
  85.                 String descricao = rs.getString("descricao");
  86.                 String categoria = rs.getString("categoria");
  87.                 Date data  = rs.getDate("data");
  88.                 String local = rs.getString("local");
  89.                 Double valor = rs.getDouble("valor");
  90.                 System.out.println("Movimento: "+ codMovimento +" Categoria: " + codigoCategoria + " - " + categoria  + " Descrição: " + descricao + " Data: " + data + " Local: "+ local + " Valor: "+ valor);
  91.  
  92.             }
  93.             stmt.close();
  94.             conexao.desconectar(conn);
  95.         } catch (SQLException ex) {
  96.             System.out.println("Erro " + ex);
  97.         }
  98.     }
  99.  
  100.     public void inserir() {
  101.  
  102.         Conexao conexao = new Conexao();
  103.         Connection conn = conexao.conectar();
  104.  
  105.         try {
  106.            
  107.            
  108.             String sql = "INSERT INTO movimentos (codMovimento, codCategoria, descricao, data, local, valor) VALUES (NULL, '"+this.codCategoria+"', '"+this.descricao+"', '"+this.data+"', '"+this.local+"', '"+this.valor+"')";
  109.             // System.out.println(sql);
  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