Advertisement
Jvsierra

Conexão Java

May 19th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 KB | None | 0 0
  1. package utilitarios;
  2. import java.sql.*;
  3. import javax.swing.*;
  4.  
  5. public class conexao{
  6.  
  7.        
  8.    
  9. final String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
  10. final String url = "jdbc:odbc:Estoque";
  11. final private String usuario = "";
  12. final private String senha = "";
  13. private Connection conexao;
  14. public Statement statement;
  15. public ResultSet resultset;
  16. public boolean conecta()
  17. {
  18.     boolean result = true;
  19.     try{
  20.     Class.forName(driver);
  21.     conexao = DriverManager.getConnection(url);
  22.     JOptionPane.showMessageDialog(null, "Conectou com sucesso.");
  23.  
  24.     }catch(ClassNotFoundException ex){
  25.     JOptionPane.showMessageDialog(null, "Erro no driver: "+ex.getMessage());
  26.     result =  false;
  27.     }
  28.     catch(SQLException ex2){
  29.     JOptionPane.showMessageDialog(null, "Erro com o db. Erro: "+ex2);
  30.     }
  31. return result;
  32. }
  33. public void execute_sql(String sql){
  34. try{
  35.     statement = conexao.createStatement();
  36.     resultset = statement.executeQuery(sql);
  37. }
  38. catch(SQLException error){
  39. JOptionPane.showMessageDialog(null, "A conexão falhou. Erro: "+error+". O comando passado foi: "+sql);
  40. }
  41. }
  42. public void desconecta(){
  43. boolean result = true;
  44. try{
  45. conexao.close();
  46. JOptionPane.showMessageDialog(null, "O Banco de Dados foi fechado com sucesso.");
  47. }catch(SQLException naoconectou){
  48. JOptionPane.showMessageDialog(null, "Não foi possível desconectar. Erro:"+naoconectou);
  49. result = false;
  50. }
  51.  
  52. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement