Advertisement
Guest User

Untitled

a guest
Oct 10th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.26 KB | None | 0 0
  1. package br.mp.mpf;
  2.  
  3. import java.sql.*;
  4. import br.mp.mpf.modelo.Dados;
  5.  
  6. public class Banco {
  7.  
  8.          public static void main(String[] args) throws Exception {
  9.  
  10.              Banco banco = new Banco();
  11.              //banco.listar();
  12.              
  13.              Dados xlsdados = new Dados();
  14.              xlsdados.setColA("OLAmunod");
  15.              //xlsdados.setColB("TesteB");
  16.              
  17. //           banco.limparTabela("apex_spo_sao.stg_rp_carga_tg");
  18.              banco.add(xlsdados);
  19.              banco.listar();
  20.  
  21.           }
  22.          
  23.          private int limparTabela(String nmTabela){
  24.              Connection conn = null;
  25.              Statement stmt = null;
  26.              ResultSet rs = null;    
  27.              int qtAfetados = 0;
  28.              
  29.              try {
  30.                  conn = obterConexao();                    
  31.                  stmt = conn.createStatement();
  32.                  String sql = "DELETE FROM "+nmTabela;
  33.                  qtAfetados = stmt.executeUpdate(sql);
  34.                  System.out.println("Excluídos: " + qtAfetados);
  35.              
  36.              } catch (Exception e) {
  37.                e.printStackTrace();
  38.              }  finally {
  39.                     try {
  40.                         if (rs != null){
  41.                             rs.close();
  42.                         }    
  43.                     } catch (SQLException sqle) {// log this
  44.                         try {
  45.                             if (stmt != null){
  46.                                 stmt.close();
  47.                             }
  48.                         } catch (SQLException sqle1) {// log this
  49.                             try {
  50.                                 if (conn != null){
  51.                                     conn.close();
  52.                                 }
  53.                             } catch (SQLException sqle2)  {// log this
  54.                                 System.out.println(sqle2);
  55.                             }
  56.                         }
  57.                     }
  58.              }
  59.              
  60.              
  61.              return 0;
  62.          }
  63.          
  64.          private int add(Dados dados){
  65.              
  66.              Connection conn = null;
  67.              Statement stmt = null;
  68.              ResultSet rs = null;
  69.              int qtDadosAdd = 0;
  70.              
  71.              
  72.              try {
  73.                  conn = obterConexao();                    
  74.                  stmt = conn.createStatement();
  75.                  String sql = "INSERT INTO apex_spo_sao.stg_rp_carga_tg(NM_RL) VALUES ('" + dados + "')";
  76.                  qtDadosAdd = stmt.executeUpdate(sql);
  77.                  System.out.println("Adicionados: " + qtDadosAdd);
  78.              
  79.              } catch (Exception e) {
  80.                e.printStackTrace();
  81.              }  finally {
  82.                     try {
  83.                         if (rs != null){
  84.                             rs.close();
  85.                         }    
  86.                     } catch (SQLException sqle) {// log this
  87.                         try {
  88.                             if (stmt != null){
  89.                                 stmt.close();
  90.                             }
  91.                         } catch (SQLException sqle1) {// log this
  92.                             try {
  93.                                 if (conn != null){
  94.                                     conn.close();
  95.                                 }
  96.                             } catch (SQLException sqle2)  {// log this
  97.                                 System.out.println(sqle2);
  98.                             }
  99.                         }
  100.                     }
  101.              }
  102.              
  103.              return 0;
  104.          }
  105.  
  106.          
  107.          private void listar(){
  108.              
  109.              Connection conn = null;
  110.              Statement stmt = null;
  111.              ResultSet rs = null;
  112.          
  113.                try {                    
  114.                    conn = obterConexao();                      
  115.                    stmt = conn.createStatement();
  116.                    String sql = "SELECT * FROM apex_spo_sao.stg_rp_carga_tg";
  117.                    rs = stmt.executeQuery(sql);                      
  118.                    
  119.                    while (rs.next()) {
  120.                        int idCarga = rs.getInt("ID_CARGA");
  121.                        String cdRL = rs.getString("CD_RL");                          
  122.                        String nmRL = rs.getString("NM_RL");
  123.                        System.out.println("idCarga = " + "\t" + idCarga +
  124.                                           "\t" + "cdRL = " + "\t" + cdRL +
  125.                                           "\t" + "nmRL = " + "\t" + nmRL);
  126.                    }
  127.                    
  128.                } catch (Exception e) {
  129.                    e.printStackTrace();
  130.                } finally {
  131.                     try {
  132.                             if (rs != null){
  133.                                 System.out.println("Sem registros");
  134.                                 rs.close();
  135.                             }    
  136.                     } catch (SQLException sqle) {// log this
  137.                         try {
  138.                                 if (stmt != null){
  139.                                     stmt.close();
  140.                                 }
  141.                         } catch (SQLException sqle1) {// log this
  142.                                 try {
  143.                                         if (conn != null){
  144.                                             conn.close();
  145.                                         }
  146.                                 } catch (SQLException sqle2)  {// log this
  147.                                     System.out.println(sqle2);
  148.                                 }
  149.                         }
  150.                     }
  151.                }             
  152.              
  153.              
  154.          }
  155.          
  156.          private static Connection obterConexao() {
  157.                    Connection conexao = null;
  158.                    try {
  159.                         Class.forName("oracle.jdbc.driver.OracleDriver");
  160.                         conexao = DriverManager.getConnection(
  161.                                 "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=db1cluster.pgr.mpf.mp.br)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=adm.pgr.mpf.mp.br)))", "APEX_SPO_SAO_CNS", "pacee474");
  162.                                 System.out.println("Conectado");
  163.                    } catch (ClassNotFoundException e) {
  164.                         System.out.println ("Classe JDBC Driver nao encontrada.\n\n");
  165.                         return null;
  166.                         //e.printStackTrace();
  167.                    } catch (SQLException e) {
  168.                        System.out.println ("Erro ao se conectar com o banco de dados.\n\n");
  169.                        return null;                    
  170.                      //e.printStackTrace();
  171.                    }
  172.                    return conexao;
  173.          }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement