Advertisement
Guest User

Untitled

a guest
May 16th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 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 br.com.minerthal.Conexao;
  7.  
  8. /**
  9. *
  10. * @author jpsa
  11. */
  12. import java.sql.Connection;
  13.  
  14. import java.sql.DriverManager;
  15.  
  16. import java.sql.SQLException;
  17.  
  18. //Início da classe de conexão//
  19. public class Conexao {
  20.  
  21. public static String status = "Não conectou...";
  22.  
  23. //Método Construtor da Classe//
  24. public Conexao() {
  25.  
  26. }
  27.  
  28. //Método de Conexão//
  29. public static java.sql.Connection abrir() {
  30.  
  31. Connection connection = null; //atributo do tipo Connection
  32.  
  33. try {
  34.  
  35. // Carregando o JDBC Driver padrão
  36. String driverName = "com.mysql.jdbc.Driver";
  37.  
  38. Class.forName(driverName);
  39.  
  40. // Configurando a nossa conexão com um banco de dados//
  41. String serverName = "localhost"; //caminho do servidor do BD
  42.  
  43. String mydatabase = "miner"; //nome do seu banco de dados
  44.  
  45. String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
  46.  
  47. String username = "root"; //nome de um usuário de seu BD
  48.  
  49. String password = "123456"; //sua senha de acesso
  50.  
  51. connection = DriverManager.getConnection(url, username, password);
  52.  
  53. //Testa sua conexão//
  54. if (connection != null) {
  55. status = ("STATUS--->Conectado com sucesso!");
  56. } else {
  57. status = ("STATUS--->Não foi possivel realizar conexão");
  58. }
  59.  
  60. return connection;
  61.  
  62. } catch (ClassNotFoundException e) {
  63. //Driver não encontrado
  64. System.out.println("O driver expecificado nao foi encontrado.");
  65. return null;
  66.  
  67. } catch (SQLException e) {
  68. //Não conseguindo se conectar ao banco
  69. System.out.println("Nao foi possivel conectar ao Banco de Dados.");
  70. return null;
  71.  
  72. }
  73.  
  74. }
  75.  
  76. //Método que retorna o status da sua conexão//
  77. public static String statusConection() {
  78.  
  79. return status;
  80.  
  81. }
  82.  
  83. //Método que fecha sua conexão//
  84. public static boolean FecharConexao() {
  85.  
  86. try {
  87.  
  88. Conexao.abrir().close();
  89.  
  90. return true;
  91.  
  92. } catch (SQLException e) {
  93.  
  94. return false;
  95.  
  96. }
  97.  
  98. }
  99.  
  100. //Método que reinicia sua conexão//
  101. public static java.sql.Connection ReiniciarConexao() {
  102.  
  103. FecharConexao();
  104.  
  105. return Conexao.abrir();
  106.  
  107. }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement