Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class AcessaBanco {
  4.  
  5. private String sql;
  6. private Connection conexao;
  7. private String usuario; //nome usuario banco
  8. private String senha; //senha do usuario no banco
  9. private String banco; //Local e Servidor do banco
  10.  
  11. public AcessaBanco(){
  12. this.usuario = "usuario3";
  13. this.senha = "SO3NzJLAGiY";
  14. this.banco = "//localhost/wifi_livre_sp";
  15. //Construtor de Carregar o drive MySql de Acesso ao Banco
  16. this.carregarDriver();
  17. }
  18.  
  19. public AcessaBanco(String usuarioBanco,String senhaBanco,String caminhoBanco){
  20. this.usuario = usuarioBanco;
  21. this.senha = senhaBanco;
  22. this.banco = caminhoBanco;
  23. //Construtor de Carregar o drive MySql de Acesso ao Banco
  24. this.carregarDriver();
  25. }
  26.  
  27. public void setSql(String _sql){
  28. this.sql = _sql;
  29. }
  30.  
  31. public String getSql(){
  32. return(sql);
  33. }
  34. public void carregarDriver(){
  35. try{
  36. Class.forName("com.mysql.jdbc.Driver").newInstance();
  37. //JOptionPane.showMessageDialog(null, "Driver carregado com sucesso!");
  38. System.out.println("Driver carregado com sucesso!");
  39. }catch(Exception e){
  40. //JOptionPane.showMessageDialog(null,"Driver não carregado, verifique importação do driver\n"+e);
  41. System.out.println("Driver não carregado, verifique importação do driver\n"+e);
  42. }
  43. }
  44.  
  45. public void abrirConexao(){
  46. try{
  47. conexao = DriverManager.getConnection("jdbc:mysql:"+ banco,usuario,senha);
  48. }
  49. catch(Exception e)
  50. {
  51. e.printStackTrace();
  52. System.out.println("Conexão não inicializada :(");
  53. }
  54. }
  55.  
  56. public void fecharConexao(){
  57. try {
  58. conexao.close();
  59. } catch (SQLException e) {
  60. e.printStackTrace();
  61. System.out.println("Não foi possivel fechar conexão ... continua aberta :(");
  62. }
  63. }
  64.  
  65. public ResultSet consulta(){
  66. try {
  67. PreparedStatement psmt = conexao.prepareStatement(sql);
  68. ResultSet consulta = psmt.executeQuery();
  69. return consulta;
  70. } catch (SQLException e) {
  71. e.printStackTrace();
  72. System.out.println("Não realiza a consulta :(");
  73. return null;
  74. }
  75. }
  76.  
  77. public void executa(){
  78. try {
  79. PreparedStatement psmt = conexao.prepareStatement(sql);
  80. psmt.execute();
  81. } catch (SQLException e) {
  82. e.printStackTrace();
  83. System.out.println("Erro de execução Sql");
  84. }
  85. }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement