Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 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 float[][] resultTable = new float[10][5];
  16. static int pomoc;
  17. static int test=0;
  18. static java.sql.Connection connection;
  19.  
  20. public DatabaseHandler()
  21. {
  22. if (checkDriver("com.mysql.jdbc.Driver"))
  23. System.out.println(" ... OK");
  24. else
  25. System.exit(1);
  26. connection = connectToDatabase("jdbc:mysql://", "localhost:3306", "sob", "root", "");
  27. if (connection != null)
  28. System.out.print(" polaczenie OK\n");
  29. }
  30.  
  31.  
  32. public static boolean checkDriver(String driver) {
  33. System.out.print("Sprawdzanie sterownika:");
  34. try {
  35. Class.forName(driver).newInstance();
  36. return true;
  37. } catch (Exception e) {
  38. System.out.println("Error when loading driver!");
  39. return false;
  40. }
  41. }
  42.  
  43. public static Connection connectToDatabase(String kindOfDatabase, String adress,
  44. String dataBaseName, String userName, String password) {
  45. String baza = kindOfDatabase + adress + "/" + dataBaseName;
  46. java.sql.Connection connection = null;
  47. try {
  48. connection = DriverManager.getConnection(baza, userName, password);
  49. } catch (SQLException e) {
  50. System.out.println("Error connecting to database!");
  51. System.exit(1);
  52. }
  53. return connection;
  54. }
  55.  
  56. private static Statement createStatement(Connection connection) {
  57. try {
  58. return connection.createStatement();
  59. } catch (SQLException e) {
  60. System.out.println("Error - > createStatement! " + e.getMessage() + ": " + e.getErrorCode());
  61. System.exit(3);
  62. }
  63. return null;
  64. }
  65.  
  66. private static void closeConnection(Connection connection, Statement s) {
  67. try {
  68. s.close();
  69. connection.close();
  70. } catch (SQLException e) {
  71. System.out
  72. .println("Error closing database! " + e.getMessage() + ": " + e.getErrorCode());;
  73. System.exit(4);
  74. }
  75.  
  76. }
  77.  
  78. private static ResultSet executeQuery(Statement s, String sql) {
  79. try {
  80. return s.executeQuery(sql);
  81. } catch (SQLException e) {
  82. System.out.println("Query couldn't be executed! " + e.getMessage() + ": " + e.getErrorCode());
  83. }
  84. return null;
  85. }
  86. private static int executeUpdate(Statement s, String sql) {
  87. try {
  88. return s.executeUpdate(sql);
  89. } catch (SQLException e) {
  90. System.out.println("Statement couldn't be executed! " + e.getMessage() + ": " + e.getErrorCode());
  91. }
  92. return -1;
  93. }
  94.  
  95. public HashMap<Integer, String> getQuestions()
  96. {
  97. System.out.println("Getting questions from database");
  98. HashMap<Integer, String> questionsMap = new HashMap<>();
  99. Statement sqlStatement = createStatement(connection);
  100. ResultSet result = executeQuery(sqlStatement, "Select * from Questions");
  101.  
  102. try {
  103. while(result.next())
  104. {
  105. Object number = result.getObject(1);
  106. Object question = result.getObject(2);
  107. questionsMap.put(Integer.parseInt(number.toString()), question.toString());
  108. }
  109. }
  110. catch (SQLException e) {
  111. System.out.println("Error getting questions from database! " + e.getMessage() + ": " + e.getErrorCode());
  112. }
  113.  
  114. return questionsMap;
  115. }
  116.  
  117. public HashMap<Integer, ArrayList<String>> getAnswers()
  118. {
  119. System.out.println("Getting answers from database");
  120.  
  121. HashMap<Integer, ArrayList<String>> answerMap = new HashMap<>();
  122.  
  123. Statement sqlStatement = createStatement(connection);
  124. ResultSet result = executeQuery(sqlStatement, "Select * from Answers");
  125.  
  126. try {
  127. while(result.next())
  128. {
  129. ArrayList<String> answers = new ArrayList<>();
  130. Object number = result.getObject(1);
  131. for(int i = 2; i < 7; ++i){
  132. Object question = result.getObject(i);
  133. answers.add(question.toString());
  134.  
  135. }
  136. answerMap.put(Integer.parseInt(number.toString()), answers);
  137.  
  138. }
  139. }
  140. catch (SQLException e) {
  141. System.out.println("Error getting answers from database! " + e.getMessage() + ": " + e.getErrorCode());
  142. }
  143.  
  144. return answerMap;
  145. }
  146.  
  147. public static void sendAnswerToDatabase(String clientID,int questionNumber, String result ){
  148. Statement s = createStatement(connection);
  149. executeUpdate(s, "INSERT INTO Results (Client_ID, Question_Number, Answer)"
  150. + "VALUES ('" + clientID + "'," + questionNumber + ",'" + result + "');");
  151.  
  152. }
  153.  
  154.  
  155. public void getSummary() throws SQLException
  156. {
  157. Statement s = createStatement(connection);
  158. ResultSet result1 = executeQuery(s, "Select count(Client_ID)/10 as zlicz from results");
  159. if(result1.next())
  160. this.pomoc = result1.getInt("zlicz");
  161.  
  162. System.out.println(this.pomoc);
  163. for(int i=1; i<=10; i++) {
  164. for( int j=1; j<=5; j++) {
  165. ResultSet result = executeQuery(s, "Select count(*) as total from results where question_number=" + i + " && answer="+ j);
  166.  
  167. //System.out.println("aa");
  168. if(result.next())
  169. this.resultTable[i-1][j-1] = result.getInt("total");
  170.  
  171. }
  172. }
  173. for(int i=1; i<=10; i++) {
  174.  
  175. System.out.println("Na pytanie nr :" + i);
  176.  
  177. for( int j=1; j<=5; j++) {
  178.  
  179. System.out.println("Procentowy wybor odpowiedz " + j + " wynosi: ");
  180. System.out.println((this.resultTable[i-1][j-1]/this.pomoc)*100 + "%");
  181.  
  182.  
  183. }
  184. System.out.println();
  185. }
  186. closeConnection(connection, s);
  187. }
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement