Advertisement
Guest User

Untitled

a guest
Jan 27th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. package me.Rodrigo.SQL;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8. public class Mysql {
  9.  
  10. private final String bancodedados;
  11. private final String host;
  12. private final String porta;
  13. private final String senha;
  14. private final String usuario;
  15.  
  16. public Mysql(String db, String host, String port, String pw, String user) {
  17. this.bancodedados = db;
  18. this.host = host;
  19. this.porta = port;
  20. this.senha = pw;
  21. this.usuario = user;
  22. }
  23.  
  24. public synchronized Connection conectar() {
  25.  
  26. try {
  27. Class.forName("com.mysql.jdbc.Driver");
  28. return DriverManager.getConnection("jdbc:mysql://" + host + ":" + porta + "/" + bancodedados, usuario, senha);
  29. } catch (SQLException | ClassNotFoundException e) {
  30. e.printStackTrace();
  31. return null;
  32. }
  33.  
  34. }
  35.  
  36. public synchronized void execute(String query) {
  37. Connection con = conectar();
  38. try {
  39. con.prepareStatement(query).executeUpdate();
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. } finally {
  43. try {
  44. con.close();
  45. } catch (SQLException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49.  
  50. }
  51.  
  52. public synchronized ResultSet getQueryResult(String query) {
  53. Connection con = conectar();
  54. try {
  55. return con.prepareStatement(query).executeQuery();
  56. } catch (SQLException e) {
  57. e.printStackTrace();
  58. return null;
  59. }
  60.  
  61. }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement