Advertisement
Guest User

Untitled

a guest
Nov 8th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package aula.facul;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.SQLException;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. import javax.swing.JOptionPane;
  14.  
  15.  
  16. public class Conexao {
  17. private final String URL = "jdbc:mysql://localhost:3306/sapataria";
  18. private final String Driver = "org.gjt.mm.mysql.Driver";
  19. private final String Usuario = "root";
  20. private final String Senha = "";
  21. private static Connection Con;
  22.  
  23. public Conexao(){
  24. try {
  25. Con = DriverManager.getConnection(URL, Usuario, Senha);
  26. Con.setAutoCommit(false);
  27. //JOptionPane.showMessageDialog(null, "Conectado com Sucesso", "Conectado", 1);
  28. } catch (Exception e) {
  29. JOptionPane.showMessageDialog(null, "Erro ao Conectar com o Banco", "Erro", 0);
  30. }
  31. }
  32.  
  33. public static Connection getConnection(){
  34. if (Con == null){
  35. new Conexao();
  36. }
  37. return Con;
  38. }
  39.  
  40. public static void fecharConexao(){
  41.  
  42. try {
  43. if (!Con.isClosed()){
  44. Con.close();
  45. }
  46. } catch (SQLException ex) {
  47. Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
  48. }
  49. }
  50.  
  51.  
  52. public static void main(String args[]){
  53. Conexao.getConnection();
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement