Advertisement
Guest User

Untitled

a guest
Apr 9th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. package edu.orangecoastcollege.cs272.mnguyen168.ic09;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.ArrayList;
  11. import java.util.Scanner;
  12.  
  13. public class QuizCreator {
  14. static int nextId;
  15.  
  16. public static void populateFromDB() {
  17.  
  18. try {
  19. Class.forName("org.sqlite.JDBC");
  20. }
  21. catch (ClassNotFoundException e) {
  22. e.printStackTrace();
  23. }
  24.  
  25. Connection connection = null;
  26.  
  27. try {
  28. connection = DriverManager.getConnection("jdbc:sqlite:cs272.db");
  29. Statement stmt = connection.createStatement();
  30. stmt.setQueryTimeout(30);
  31. stmt.executeUpdate("DROP TABLE IF EXISTS question");
  32. stmt.executeUpdate("CREATE TABLE IF NOT EXISTS question (id INTEGER PRIMARY KEY, question Text)");
  33. ResultSet rs = stmt.executeQuery("SELECT * FROM question");
  34.  
  35. while (rs.next()) {
  36. int id = rs.getInt("id");
  37. if (id + 1 > nextId)
  38. nextId = id + 1;
  39. String question = rs.getString("question");
  40. String choice_a = rs.getString("choice_a");
  41. String choice_b = rs.getString("choice_b");
  42. String choice_c = rs.getString("choice_c");
  43. String choice_d = rs.getString("choice_d");
  44. String answer = rs.getString("answer");
  45. String blank = rs.getString("blank");
  46.  
  47. System.out.println(id);
  48. System.out.println(question);
  49. System.out.println(choice_a);
  50. System.out.println(choice_b);
  51. System.out.println(choice_c);
  52. System.out.println(choice_d);
  53. System.out.println(answer);
  54. System.out.println(blank);
  55. }
  56. }
  57. catch (SQLException e) {
  58. e.printStackTrace();
  59. }
  60. }
  61.  
  62. public static void addToDB(int id, String question, String choice_a, String choice_b, String choice_c,
  63. String choice_d, String answer, String blank) {
  64.  
  65. try {
  66. Class.forName("org.sqlite.JDBC");
  67. }
  68. catch (ClassNotFoundException e) {
  69. e.printStackTrace();
  70. }
  71.  
  72. Connection connection = null;
  73.  
  74. try {
  75. connection = DriverManager.getConnection("jdbc:sqlite:cs272.db");
  76. Statement stmt = connection.createStatement();
  77. stmt.setQueryTimeout(30);
  78. stmt.executeUpdate("INSERT INTO question VALUES(" + id + ", '" + question + ", '" +
  79. choice_a + ", '" + choice_b + ", '" + choice_c + ", '" + choice_d + ", '" +
  80. answer + ", '" + blank + "')");
  81.  
  82. connection.close();
  83. }
  84. catch (SQLException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88.  
  89. public static void main(String[] args) {
  90.  
  91. populateFromDB();
  92. try {
  93. Scanner fileScanner = new Scanner(new File("Quiz.txt"));
  94. while (fileScanner.hasNextLine()) {
  95. String idString = fileScanner.nextLine();
  96. idString = idString.substring(0, idString.indexOf('.'));
  97. int id = Integer.parseInt(idString);
  98.  
  99. String question = fileScanner.nextLine();
  100. String choice_a = fileScanner.nextLine();
  101. String choice_b = fileScanner.nextLine();
  102. String choice_c = fileScanner.nextLine();
  103. String choice_d = fileScanner.nextLine();
  104. String answer = fileScanner.nextLine();
  105. String blank = fileScanner.nextLine();
  106.  
  107. addToDB(id, question, choice_a, choice_b, choice_c, choice_d, answer, blank);
  108. }
  109. fileScanner.close();
  110. }
  111. catch (IOException e) {
  112. System.out.println("File does not exist.");
  113. }
  114. }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement