Advertisement
ApocalypsjeNL

[JAVA] MySQL

Aug 20th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. package com.apocalypsjenl.mysql;
  2.  
  3. import java.sql.*;
  4.  
  5. //MySQL Class created by ApocalypsjeNL
  6. public class MySQL {
  7.  
  8. private Connection connection = null;
  9.  
  10. public void connect(String host, Integer port, String username, String password, String database) throws SQLException, ClassNotFoundException {
  11. Class.forName("com.mysql.jdbc.Driver");
  12. connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
  13. }
  14.  
  15. public void execute(String sql) throws SQLException {
  16. if(connection != null) {
  17. Statement statement = connection.createStatement();
  18. statement.execute(sql);
  19. }
  20. }
  21.  
  22. public ResultSet executeQuery(String sql) throws SQLException {
  23. if(connection != null) {
  24. Statement statement = connection.createStatement();
  25. return statement.executeQuery(sql);
  26. }
  27. return null;
  28. }
  29.  
  30. public void disconnect() throws SQLException {
  31. if(connection != null) {
  32. connection.close();
  33. System.out.println("Disconnected from database!");
  34. }
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement