Advertisement
Guest User

Untitled

a guest
Jan 28th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 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.ConexaoBanco;
  7.  
  8. /**
  9. *
  10. * @author hotsystems
  11. */
  12. import java.sql.Connection;
  13.  
  14. import java.sql.DriverManager;
  15.  
  16. import java.sql.SQLException;
  17.  
  18. public class ConexaoMySQL {
  19.  
  20. public static String status = "Não conectou...";
  21.  
  22. public ConexaoMySQL() {
  23.  
  24. }
  25.  
  26. public static java.sql.Connection getConexaoMySQL() {
  27.  
  28. Connection connection = null; //atributo do tipo Connection
  29.  
  30. try {
  31.  
  32. String driverName = "com.mysql.jdbc.Driver";
  33.  
  34. Class.forName(driverName);
  35.  
  36. String serverName = "";
  37.  
  38. String mydatabase ="";
  39.  
  40. String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
  41.  
  42. String username = "";
  43.  
  44. String password = "";
  45.  
  46. connection = DriverManager.getConnection(url, username, password);
  47.  
  48. //Testa sua conexão//
  49. if (connection != null) {
  50.  
  51. status = ("STATUS--->Conectado com sucesso!");
  52.  
  53. } else {
  54.  
  55. status = ("STATUS--->Não foi possivel realizar conexão");
  56.  
  57. }
  58.  
  59. return connection;
  60.  
  61. } catch (ClassNotFoundException e) {
  62.  
  63. System.out.println("O driver do banco de dados nao foi encontrado.");
  64.  
  65. return null;
  66.  
  67. } catch (SQLException e) {
  68.  
  69. System.out.println(e);
  70. System.out.println("Nao foi possivel conectar ao Banco de Dados.");
  71.  
  72. return null;
  73.  
  74. }
  75.  
  76. }
  77.  
  78. public static String statusConection() {
  79.  
  80. return status;
  81.  
  82. }
  83.  
  84. public static boolean FecharConexao() {
  85.  
  86. try {
  87.  
  88. ConexaoMySQL.getConexaoMySQL().close();
  89.  
  90. return true;
  91.  
  92. } catch (SQLException e) {
  93.  
  94. return false;
  95.  
  96. }
  97.  
  98. }
  99.  
  100. public static java.sql.Connection ReiniciarConexao() {
  101.  
  102. FecharConexao();
  103.  
  104. return ConexaoMySQL.getConexaoMySQL();
  105.  
  106. }
  107.  
  108. }
  109.  
  110. /*
  111. * To change this license header, choose License Headers in Project Properties.
  112. * To change this template file, choose Tools | Templates
  113. * and open the template in the editor.
  114. */
  115. package controlesincronismo;
  116.  
  117. import br.com.ConexaoBanco.ConexaoMySQL;
  118.  
  119. /**
  120. *
  121. * @author hotsystems
  122. */
  123. public class ControleSincronismo {
  124.  
  125. /**
  126. * @param args the command line arguments
  127. */
  128. public static void main(String[] args) {
  129. ConexaoMySQL con = new ConexaoMySQL();
  130. con.getConexaoMySQL();
  131. con.statusConection();
  132. }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement