Advertisement
Guest User

Untitled

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