Guest User

Untitled

a guest
Jan 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1. package BD;
  2. import excecoes.Excecao;
  3. import java.sql.*;
  4. import javax.swing.JOptionPane;
  5.        
  6. public class Conexao
  7. {
  8.    
  9.     private  static Connection con;
  10.     private  static Statement stm;
  11.     private  static ResultSet rs=null;
  12.     private  static DadosConexao dados;    
  13.  
  14.     public Conexao(DadosConexao dados){
  15.         this.dados=dados;
  16.         con=null;
  17.         stm=null;
  18.     }
  19.  
  20.     public static boolean Abrir()
  21.     {
  22.         try
  23.         {
  24.             String driver="", url="";
  25.             if (dados.mostrarSGBD().equals(DadosConexao.MYSQL)){
  26.                 url = "jdbc:mysql://"+dados.mostrarServidor()+"/"+dados.mostrarBanco();
  27.                 driver = "com.mysql.jdbc.Driver";
  28.                     Class.forName(driver);
  29.             //con=DriverManager.getConnection(url,dados.mostrarUsuario(),dados.mostrarSenha());
  30.             con=DriverManager.getConnection("jdbc:mysql://"+dados.mostrarServidor()+"/"+dados.mostrarBanco()+"",dados.mostrarUsuario(),dados.mostrarSenha());
  31.             }
  32.            
  33.            
  34.             stm=con.createStatement();
  35.             return true;
  36.         }
  37.         catch (Exception erro){return false;}
  38.     }
  39.  
  40.  
  41.     public static boolean Fechar()
  42.     {
  43.         try
  44.         {
  45.             con.close();
  46.             return true;
  47.         }
  48.         catch(SQLException sqle)
  49.         {
  50.             return false;
  51.         }
  52.     }
  53.    
  54.     public  boolean Busca(String sql) {
  55.         boolean resultado=false;
  56.         rs=null;
  57.         if (con==null){
  58.             Abrir();          
  59.         }
  60.         try{
  61.             rs=stm.executeQuery(sql);
  62.             resultado = true;
  63.         }
  64.         catch (Exception e){
  65.         }
  66.         //JOptionPane.showInputDialog("",sql);
  67.        
  68.        
  69.         return resultado;
  70.     }
  71.    
  72.     public static boolean Executa(String sql){
  73.         boolean resultado=false;
  74.         if (con==null){
  75.             Abrir();
  76.         }
  77.         try{
  78.             resultado=stm.execute(sql);
  79.             //resultado=true;
  80.         }
  81.         catch (Exception e){
  82.         }
  83.         return resultado;
  84.     }
  85.    
  86.     public String MostrarCampo(String campo){
  87.         String resultado="";
  88.         try{
  89.             resultado=rs.getString(campo);
  90.         }
  91.         catch(Exception e){
  92.         }
  93.         return resultado;
  94.     }
  95.    
  96.     public boolean MoverProximo(){
  97.         try{
  98.             return rs.next();
  99.         }
  100.         catch(Exception e){
  101.             return false;
  102.         }
  103.     }
  104.    
  105.     public boolean MoverInicio(){
  106.         try{
  107.             return rs.first();
  108.         }
  109.         catch(Exception e){
  110.             return false;
  111.         }
  112.     }
  113.  
  114.     public boolean MoverFim(){
  115.         try{
  116.             return rs.last();
  117.         }
  118.         catch(Exception e){
  119.             return false;
  120.         }
  121.     }
  122.  
  123.     public boolean MoverVoltar(){
  124.         try{
  125.             return rs.previous();
  126.         }
  127.         catch(Exception e){
  128.             return false;
  129.         }
  130.     }
  131.  
  132. }
Add Comment
Please, Sign In to add comment