Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. package survey;
  2.  
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11.  
  12.  
  13. public class DatabaseHandler {
  14.  
  15. static java.sql.Connection connection;
  16.  
  17. public DatabaseHandler()
  18. {
  19. if (checkDriver("com.mysql.jdbc.Driver"))
  20. System.out.println(" ... OK");
  21. else
  22. System.exit(1);
  23. connection = connectToDatabase("jdbc:mysql://", "localhost:3306", "ankiety", "root", "");
  24. if (connection != null)
  25. System.out.print(" polaczenie OK\n");
  26. }
  27.  
  28.  
  29. public static boolean checkDriver(String driver) {
  30. System.out.print("Sprawdzanie sterownika:");
  31. try {
  32. Class.forName(driver).newInstance();
  33. return true;
  34. } catch (Exception e) {
  35. System.out.println("Error when loading driver!");
  36. return false;
  37. }
  38. }
  39.  
  40. public static Connection connectToDatabase(String kindOfDatabase, String adress,
  41. String dataBaseName, String userName, String password) {
  42. String baza = kindOfDatabase + adress + "/" + dataBaseName;
  43. java.sql.Connection connection = null;
  44. try {
  45. connection = DriverManager.getConnection(baza, userName, password);
  46. } catch (SQLException e) {
  47. System.out.println("Error connecting to database!");
  48. System.exit(1);
  49. }
  50. return connection;
  51. }
  52.  
  53. private static Statement createStatement(Connection connection) {
  54. try {
  55. return connection.createStatement();
  56. } catch (SQLException e) {
  57. System.out.println("Error - > createStatement! " + e.getMessage() + ": " + e.getErrorCode());
  58. System.exit(3);
  59. }
  60. return null;
  61. }
  62.  
  63. private static void closeConnection(Connection connection, Statement s) {
  64. try {
  65. s.close();
  66. connection.close();
  67. } catch (SQLException e) {
  68. System.out
  69. .println("Error closing database! " + e.getMessage() + ": " + e.getErrorCode());;
  70. System.exit(4);
  71. }
  72. //System.out.print(" zamknięcie OK");
  73. }
  74.  
  75. private static ResultSet executeQuery(Statement s, String sql) {
  76. try {
  77. return s.executeQuery(sql);
  78. } catch (SQLException e) {
  79. System.out.println("Query couldn't be executed! " + e.getMessage() + ": " + e.getErrorCode());
  80. }
  81. return null;
  82. }
  83. private static int executeUpdate(Statement s, String sql) {
  84. try {
  85. return s.executeUpdate(sql);
  86. } catch (SQLException e) {
  87. System.out.println("Statement couldn't be executed! " + e.getMessage() + ": " + e.getErrorCode());
  88. }
  89. return -1;
  90. }
  91.  
  92. public HashMap<Integer, String> getQuestions()
  93. {
  94. System.out.println("Getting questions from database");
  95. HashMap<Integer, String> questionsMap = new HashMap<>();
  96. Statement sqlStatement = createStatement(connection);
  97. ResultSet result = executeQuery(sqlStatement, "Select * from Questions");
  98.  
  99. try {
  100. while(result.next())
  101. {
  102. Object number = result.getObject(1);
  103. Object question = result.getObject(2);
  104. questionsMap.put(Integer.parseInt(number.toString()), question.toString());
  105. }
  106. }
  107. catch (SQLException e) {
  108. System.out.println("Error getting questions from database! " + e.getMessage() + ": " + e.getErrorCode());
  109. }
  110.  
  111. return questionsMap;
  112. }
  113.  
  114. public HashMap<Integer, ArrayList<String>> getAnswers()
  115. {
  116. System.out.println("Getting answers from database");
  117.  
  118. HashMap<Integer, ArrayList<String>> answerMap = new HashMap<>();
  119.  
  120. Statement sqlStatement = createStatement(connection);
  121. ResultSet result = executeQuery(sqlStatement, "Select * from Answers");
  122.  
  123. try {
  124. while(result.next())
  125. {
  126. ArrayList<String> answers = new ArrayList<>();
  127. Object number = result.getObject(1);
  128. for(int i = 2; i < 7; ++i){
  129. Object question = result.getObject(i);
  130. answers.add(question.toString());
  131.  
  132. }
  133. answerMap.put(Integer.parseInt(number.toString()), answers);
  134.  
  135. }
  136. }
  137. catch (SQLException e) {
  138. System.out.println("Error getting answers from database! " + e.getMessage() + ": " + e.getErrorCode());
  139. }
  140.  
  141. return answerMap;
  142. }
  143.  
  144. public static void sendAnswerToDatabase(String clientID,int questionNumber, String result ){
  145. Statement s = createStatement(connection);
  146. executeUpdate(s, "INSERT INTO Results (Client_ID, Question_Number, Answer)"
  147. + "VALUES ('" + clientID + "'," + questionNumber + ",'" + result + "');");
  148.  
  149. }
  150.  
  151. public void getSummary()
  152. {
  153.  
  154. Statement s = createStatement(connection);
  155. executeQuery(s, "SELECT updateResults();");
  156. closeConnection(connection, s);
  157. }
  158.  
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement