Advertisement
Guest User

Untitled

a guest
Aug 6th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. package com.herocraftonline.rightlegred.heroesleaderboard.persistance;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.logging.Level;
  9.  
  10. import com.herocraftonline.rightlegred.heroesleaderboard.HeroesLeaderboard;
  11.  
  12. public class MySQL {
  13.  
  14. // Create a String to hold the Database location.
  15. Connection con = null;
  16. String url = "testdb.db";
  17. String user = "testuser";
  18. String password = "test623";
  19. private final HeroesLeaderboard plugin;
  20.  
  21. public MySQL(HeroesLeaderboard plugin) {
  22. this.plugin = plugin;
  23. }
  24.  
  25. /**
  26. * Grab a new Database Connection
  27. * @return
  28. */
  29. public Connection getConnection() {
  30. Connection connection = null;
  31. try {
  32. con = DriverManager.getConnection(url, user, password);
  33. Statement st = con.createStatement();
  34. ResultSet result = st.executeQuery("SELECT VERSION()");
  35.  
  36. if (result.next()) {
  37. System.out.println(result.getString(1));
  38. }
  39.  
  40. con.close();
  41.  
  42. } catch (SQLException ex) {
  43. System.out.println(ex.getMessage());
  44. }
  45. return connection;
  46. }
  47.  
  48. /**
  49. * This function performs Inserts & Updates.
  50. * @param sqlString
  51. */
  52. public void tryUpdate(String sqlString) {
  53. try {
  54. plugin.log(Level.INFO, sqlString);
  55.  
  56. Connection conn = getConnection();
  57. Statement st = conn.createStatement();
  58. st.executeUpdate(sqlString);
  59. st.close();
  60. conn.close();
  61. } catch (Exception e) {
  62. plugin.log(Level.WARNING, "The following statement failed: " + sqlString);
  63. plugin.log(Level.WARNING, "Statement failed: " + e.toString());
  64. }
  65.  
  66. }
  67.  
  68. /**
  69. * This function performs Selects and returns a ResultSet.
  70. * @param sqlString
  71. * @return
  72. */
  73. public ResultSet trySelect(String sqlString) {
  74. try {
  75. plugin.log(Level.INFO, sqlString);
  76.  
  77. Connection conn = getConnection();
  78. Statement st = conn.createStatement();
  79. return st.executeQuery(sqlString);
  80. } catch (Exception e) {
  81. plugin.log(Level.WARNING, "Statement failed: " + e.toString());
  82. }
  83. return null;
  84. }
  85.  
  86. /**
  87. * Return the Row Count of the Query.
  88. * @param query
  89. * @return
  90. */
  91. public int rowCount(String query){
  92. int count = 0;
  93. try {
  94. ResultSet r = trySelect(query);
  95. while(r.next()) {
  96. count++;
  97. }
  98. r.close();
  99. } catch (SQLException e) {
  100. e.printStackTrace();
  101. }
  102. return count;
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement