Guest User

Untitled

a guest
Feb 9th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. package conexaoprojeto;
  2.  
  3. import java.sql.*;
  4. import javax.swing.JOptionPane;
  5.  
  6. public class ConexaoProjeto {
  7.  
  8. private final String Driver = "com.mysql.jdbc.Driver";
  9. private final String Url = "jdbc:mysql://localhost:3306/ConexaoProjeto";
  10. private final String User = "root";
  11. private final String Pass = "123456";
  12. public Connection conn;
  13. public Statement stmt;
  14. public ResultSet rs;
  15.  
  16.  
  17. public Statement Conectar() {
  18. try {
  19. System.setProperty("jdbc.drivers", Driver);
  20. conn = DriverManager.getConnection(Url, User, Pass);
  21. stmt = conn.createStatement();
  22. JOptionPane.showMessageDialog(null, "Conectado com Sucesso!");
  23. } catch (SQLException ex) {
  24. JOptionPane.showMessageDialog(null, "Ocorreu uma falha! Não pode ser conectado" + "n" + ex.getMessage());
  25. }
  26. return stmt;
  27. }
  28.  
  29. public void Desconectar() {
  30. try {
  31. conn.close();
  32. JOptionPane.showMessageDialog(null, "Conexão fechada com sucesso!");
  33. } catch (SQLException ex) {
  34. JOptionPane.showMessageDialog(null, "Conexão falhou ao ser encerrada!" + "n" + ex.getMessage());
  35. }
  36. }
  37. }
  38.  
  39. package conexaoprojeto;
  40.  
  41. import java.sql.Connection;
  42. import java.sql.DriverManager;
  43. import java.sql.SQLException;
  44. import java.sql.Statement;
  45.  
  46. public class Conecta {
  47.  
  48. private String DRIVER = "com.mysql.jdbc.Driver";
  49. private String BD = "ConexaoProjeto";
  50. private String URL = "jdbc:mysql://localhost:3306/" +BD;
  51. private String USERNAME = "root";
  52. private String PASSWORD = "123456";
  53. private Connection conexao;
  54. private Statement stm;
  55. private String msg;
  56.  
  57. public Conecta() {
  58. this.msg = this.iniciaConexao();
  59. }
  60.  
  61. public Conecta(String bd, String user, String senha) {
  62. this.BD = bd;
  63. this.USERNAME = user;
  64. this.PASSWORD = senha;
  65. this.msg = this.iniciaConexao();
  66. }
  67.  
  68. public String iniciaConexao() {
  69. try {
  70. Class.forName(this.DRIVER);
  71. this.conexao = DriverManager.getConnection(URL, USERNAME, PASSWORD);
  72. // Definimos o objeto responsável por executar os comandos
  73. this.stm = this.getConexao().createStatement();
  74. return "sucesso";
  75.  
  76. } catch (ClassNotFoundException e) {
  77. this.conexao = null;
  78. return "Não foi possivel encontrar o driver de banco: " + e.getMessage();
  79. } catch (SQLException e) {
  80. this.conexao = null;
  81. return "SQLException Erro!" + e.getMessage();
  82. }
  83. }
  84.  
  85. public String fechaConexao() {
  86. try {
  87. if (this.getConexao() != null) {
  88. this.getConexao().close();
  89. this.conexao = null;
  90. }
  91. if (this.getStm() != null) {
  92. this.stm = null;
  93. }
  94. return "Conexão Encerrada";
  95. } catch (SQLException ex) {
  96. return "Houve erro no fechamento da conexão! "+ex.getMessage();
  97. }
  98. }
  99.  
  100. public Connection getConexao() {
  101. return conexao;
  102. }
  103.  
  104. public Statement getStm() {
  105. return stm;
  106. }
  107.  
  108. public String getMsg() {
  109. return msg;
  110. }
  111.  
  112. }
Add Comment
Please, Sign In to add comment