Advertisement
Guest User

Untitled

a guest
Feb 4th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. package rocky.myhub.SQL;
  2.  
  3.  
  4.  
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10.  
  11.  
  12. public class MySQL {
  13.  
  14. public static String host = "127.0.0.1";
  15. public static String port = "3306";
  16. public static String database = "minecraft";
  17. public static String username = "root";
  18. public static String password = "";
  19.  
  20. public static Connection con;
  21.  
  22. public static boolean isConnected(){
  23. return con != null;
  24. }
  25.  
  26. public static void connect(){
  27. if(!isConnected()){
  28. try {
  29. con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", username, password);
  30. } catch (SQLException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. }
  35.  
  36. public static void disconnect(){
  37. try {
  38. con.close();
  39. } catch (SQLException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43.  
  44. public static PreparedStatement getStatement(String sql){
  45. if(isConnected()){
  46. PreparedStatement ps;
  47. try {
  48. ps = con.prepareStatement(sql);
  49. return ps;
  50. } catch (SQLException e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. return null;
  55. }
  56.  
  57. public static ResultSet getResult(String sql){
  58. if(isConnected()){
  59. PreparedStatement ps;
  60. ResultSet rs;
  61. try {
  62. ps = getStatement(sql);
  63. rs = ps.executeQuery();
  64. return rs;
  65. } catch (SQLException e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. return null;
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement