Advertisement
Guest User

Untitled

a guest
May 18th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. package Engine;
  2.  
  3. import java.sql.*; // for standard JDBC programs
  4. import java.util.*;
  5.  
  6. public class DBConnection {
  7.  
  8. private static String url = "jdbc:mysql://185.13.227.167:3306/michiai147_domin";
  9. private static String user = "michiai147_domin";
  10. private static String pass = "groep1domin2016";
  11. private static Connection connection;
  12. private ResultSet resultset;
  13. private PreparedStatement prepStatemnt;
  14. private String[] arrayResults;
  15. private ArrayList<String[]> data = new ArrayList<>();
  16. private boolean control = false;
  17.  
  18. public DBConnection() throws Exception {
  19. Class.forName("com.mysql.jdbc.Driver");
  20. connectDB();
  21. }
  22.  
  23. private void connectDB() throws SQLException {
  24. connection = DriverManager.getConnection(url, user, pass);
  25. }
  26.  
  27. public void dataToArrayList(ResultSet result) throws SQLException {
  28. ResultSetMetaData metaData = resultset.getMetaData();
  29. int columns = metaData.getColumnCount()+1;
  30.  
  31. while (resultset.next()) {
  32. arrayResults = new String[columns-1];
  33. for (int i = 1; i < columns; i++) {
  34. arrayResults[i-1] = resultset.getString(i);
  35. if (i == metaData.getColumnCount()) {
  36. data.add(arrayResults);
  37. }
  38. }
  39. }
  40. }
  41.  
  42. private void setSelectQuery(String table, String columns, String where) throws SQLException {
  43. prepStatemnt = connection.prepareStatement("SELECT " + columns + " "
  44. + "FROM " + table + " "
  45. + (!table.equals("Cards") ? "" : "JOIN CardSets ON Cards.SetID = CardSets.SetID ")
  46. + "JOIN CardTypes ON Cards.TypeID = CardTypes.TypeID "
  47. + (!columns.contains("Effect") ? "" : "JOIN CardEffects ON Cards.CardID = CardEffects.Card JOIN Effects ON CardEffects.Effects = Effects.IDEffect ")
  48. + (where.equals("") ? "" : "WHERE " + where));
  49.  
  50. System.out.println(prepStatemnt);
  51. dataToArrayList(this.resultset = prepStatemnt.executeQuery());
  52. close();
  53. }
  54.  
  55. public ArrayList<String[]> getSelectQuery(String table, String columns, String where) throws SQLException {
  56. if (data.isEmpty()) {
  57. connectDB();
  58. setSelectQuery(table, columns, where);
  59. close();
  60. } else {
  61. data.clear();
  62. getSelectQuery(table, columns, where);
  63. }
  64. return data;
  65. }
  66.  
  67. private void setUpdateQuery(String table, String set, String where) throws SQLException {
  68. prepStatemnt = connection.prepareStatement("UPDATE " + table + " "
  69. + "SET " + set + " "
  70. + (where.equals("") ? "" : "WHERE " + where));
  71. int check = prepStatemnt.executeUpdate();
  72. if (check > 0) {
  73. this.control = true;
  74. }
  75. close();
  76. }
  77.  
  78. public boolean executeUpdateQuery(String table, String set, String where) throws SQLException {
  79. connectDB();
  80. setUpdateQuery(table, set, where);
  81. close();
  82. return control;
  83. }
  84.  
  85. private void setInsertQuery(String table, String columns, String values) throws SQLException {
  86. prepStatemnt = connection.prepareStatement("INSERT INTO " + table + " (" + columns + ") VALUES(" + values + ")");
  87. int check = prepStatemnt.executeUpdate();
  88. if (check > 0) {
  89. control = true;
  90. }
  91. close();
  92. }
  93.  
  94. public boolean executeInsertQuery(String table, String columns, String values) throws SQLException {
  95. setInsertQuery(table, columns, values);
  96. return control;
  97. }
  98.  
  99. public void close() {
  100. try {
  101. if (resultset != null) {
  102. resultset.close();
  103. }
  104.  
  105. if (prepStatemnt != null) {
  106. prepStatemnt.close();
  107. }
  108.  
  109. if (connection != null) {
  110. connection.close();
  111. }
  112. } catch (Exception e) {
  113.  
  114. }
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement