Advertisement
Guest User

Untitled

a guest
Dec 8th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. package me.bobakanoosh.fnabot;
  2.  
  3. import java.sql.*;
  4.  
  5. /**
  6. * Created by Jack Englund on 8/20/2018.
  7. */
  8. public class QueryManager {
  9.  
  10. private Connection connection;
  11. private Statement statement;
  12.  
  13. public QueryManager(String host, String username, String password) {
  14.  
  15. try {
  16.  
  17. StringBuilder builder = new StringBuilder();
  18. builder.append(host);
  19. builder.append("?user=").append(username);
  20. builder.append("&password=").append(password);
  21. builder.append("&autoreconnect=true");
  22. builder.append("&useSSL=false");
  23. connection = DriverManager.getConnection(builder.toString());
  24.  
  25. //connection = DriverManager.getConnection(host, username, password);
  26. statement = connection.createStatement();
  27.  
  28. System.out.println("Connected to DB");
  29.  
  30. } catch (SQLException e) { System.err.println("-" + e.getMessage());}
  31.  
  32. }
  33.  
  34. public ResultSet select(PreparedStatement statement) {
  35.  
  36. try {
  37. return statement.executeQuery();
  38. } catch (SQLException e) {
  39. e.printStackTrace();
  40. }
  41.  
  42. throw new NullPointerException();
  43. }
  44.  
  45. public boolean hasEntry(String id) {
  46. PreparedStatement statement = null;
  47. try {
  48.  
  49. statement = this.getConnection().prepareStatement("SELECT discordid FROM users WHERE discordid = ?");
  50. statement.setString(1, id);
  51.  
  52. ResultSet set = statement.executeQuery();
  53.  
  54. // See if the resultset has anything in it, and return whether or not it does.
  55. try {
  56. return set.next();
  57. } catch (SQLException e) {System.err.println(e.getMessage());}
  58.  
  59. return true;
  60.  
  61. } catch (SQLException e) {
  62. e.printStackTrace();
  63. } finally {
  64. try {
  65. if (statement != null)
  66. statement.close();
  67. }
  68. catch (Exception e) {
  69. e.printStackTrace();
  70. }
  71. }
  72. return false;
  73. }
  74.  
  75. public int delete(String sql) {
  76.  
  77. System.out.println(sql);
  78.  
  79. try {
  80.  
  81. return statement.executeUpdate(sql);
  82.  
  83. } catch (SQLException e) {
  84.  
  85. System.err.println(e.getMessage());
  86. return 0;
  87.  
  88. }
  89. }
  90.  
  91. public boolean insert(PreparedStatement statement) {
  92.  
  93. try {
  94. statement.executeUpdate();
  95. } catch (SQLException e) {
  96. e.printStackTrace();
  97. return false;
  98. } finally {
  99. try {
  100. if (statement != null) {
  101. statement.close();
  102. }
  103. }
  104. catch (Exception e) {
  105. // log this error
  106. e.printStackTrace();
  107. }
  108. }
  109.  
  110. return true;
  111. }
  112.  
  113. public Connection getConnection() {
  114. return connection;
  115. }
  116.  
  117. public Statement getStatement() {
  118. return statement;
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement