Guest User

Untitled

a guest
Mar 10th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. package server.util;
  2. import java.sql.*;
  3. import java.security.MessageDigest;
  4. import server.model.players.Client;
  5. import server.model.players.PlayerHandler;
  6. import server.Server;
  7.  
  8. public class SQL {
  9. public static Connection con = null;
  10. public static Statement stmt;
  11. private static long lastUsed = System.currentTimeMillis();
  12.  
  13. public static void createConnection() {
  14. try {
  15. Class.forName("com.mysql.jdbc.Driver").newInstance();
  16. con = DriverManager.getConnection(
  17. "jdbc:mysql://213.229.123.70/craftedp_Forums", "craftep_Brad", "lol123");
  18. stmt = con.createStatement();
  19. Misc.println("Connected");
  20. } catch (Exception e) {
  21. Misc.println("Connection Problem");
  22. }
  23. }
  24.  
  25. public static ResultSet query(String s) throws SQLException {
  26. try {
  27. if (s.toLowerCase().startsWith("select")) {
  28. ResultSet rs = stmt.executeQuery(s);
  29. return rs;
  30. } else {
  31. stmt.executeUpdate(s);
  32. }
  33. return null;
  34. } catch (Exception e) {
  35. destroyConnection();
  36. createConnection();
  37. }
  38. return null;
  39. }
  40.  
  41. public static void destroyConnection() {
  42. try {
  43. stmt.close();
  44. con.close();
  45. } catch (Exception e) {
  46. }
  47. }
  48.  
  49. public static Connection getConnection() throws Exception {
  50. if (con == null) {
  51. throw new Exception("connection is null");
  52. }
  53. if (System.currentTimeMillis() - lastUsed > 300000) {
  54. try {
  55. lastUsed = System.currentTimeMillis();
  56. con.close();
  57. createConnection();
  58. } catch (Exception e) {
  59. throw new Exception("error refreshing database connection");
  60. }
  61. }
  62. return con;
  63. }
  64.  
  65. public static boolean checkStatus(Client c) {
  66. try {
  67. Statement s = getConnection().createStatement();
  68. ResultSet results = s.executeQuery("SELECT * FROM `status` WHERE `username` = '" + c.playerName + "' AND `given` = '0' LIMIT 10;");
  69. while(results.next()) {
  70. c.getItems().addItem(results.getInt("item"), results.getInt("quantity"));
  71. Statement st = getConnection().createStatement();
  72. st.executeUpdate("UPDATE `status` SET `given` = '10' WHERE `id`='" + results.getInt("id") + "';");
  73. st.close();
  74. }
  75. s.close();
  76. } catch(Exception e) {
  77. e.printStackTrace();
  78. }
  79. return true;
  80. }
  81.  
  82. }
Add Comment
Please, Sign In to add comment