Guest User

Untitled

a guest
Aug 2nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. static final String JDBC_DRIVER="com.mysql.jdbc.Driver";
  2. static final String DB_URL="jdbc:mysql://localhost/aishwarya";
  3. static final String USER="Aishwarya";
  4. static final String PASS="Aishwaryakadam06";
  5.  
  6.  
  7. public static void main(String[] args) throws IOException, SQLException {
  8.  
  9. Scanner scan = new Scanner(System.in);
  10. Connection con=null;
  11. Statement st=null;
  12. try {
  13. Class.forName("com.mysql.jdbc.Driver");
  14. System.out.println("Connection to database...");
  15.  
  16.  
  17. con= (Connection) DriverManager.getConnection(DB_URL, USER, PASS);
  18. System.out.println("Creating Statement...");
  19. st= (Statement) con.createStatement();
  20. String sql;
  21. System.out.println("Press enter to start the test: ");
  22. String Question_id1 = scan.nextLine();
  23. sql = "SELECT * FROM quiz1 limit 2";
  24. ResultSet rs=st.executeQuery(sql);
  25. while(rs.next())
  26. {
  27. int Question_id=rs.getInt("Question_id");
  28. System.out.print(""+Question_id);
  29. String Question_text=rs.getString("Question_text");
  30. System.out.println(" "+Question_text);
  31. String A=rs.getString("A");
  32. System.out.println("A: "+A);
  33. String B=rs.getString("B");
  34. System.out.println("B: "+B);
  35. String C=rs.getString("C");
  36. System.out.println("C: "+C);
  37. String D=rs.getString("D");
  38. System.out.println("D: "+D);
  39.  
  40. System.out.println("Submit Answer ");
  41. String CorrectAnswer1 = scan.nextLine();
  42.  
  43. int count= 0;
  44. String CorrectAnswer = rs.getString("CorrectAnswer") ;
  45. for (int i=0; i< 1; i++) {
  46. if (CorrectAnswer.equals(CorrectAnswer1))
  47. {
  48. System.out.println("Correct ");
  49. count++;
  50. }
  51. else
  52. {
  53. System.out.println("Wrong!!" );
  54. count--;
  55. }
  56. System.out.println("Marks: " +count);
  57. System.out.println(" ");
  58.  
  59. } //for
  60. } //while
  61. System.out.println(" **Correct Answers** ");
  62. System.out.println(" ");
  63. ResultSet rs1=st.executeQuery(sql);
  64.  
  65. while(rs1.next())
  66. {
  67.  
  68. for(int i=0;i<1;i++)
  69. {
  70. int Question_id=rs1.getInt("Question_id");
  71. System.out.print(" " +Question_id );
  72. } //for 2
  73. sql = "SELECT * FROM quiz1";
  74. String CorrectAnswer2=rs1.getString("CorrectAnswer");
  75.  
  76. System.out.println(" Correct Answer is: " +CorrectAnswer2);
  77.  
  78. System.out.println(" ");
  79. } //while2
  80. rs1.close();
  81. rs.close();
  82. st.close();
  83. con.close();
  84. } //try
  85.  
  86. }
  87.  
  88. }
  89. catch(SQLException se2)
  90. {
  91. se2.printStackTrace();
  92. }
  93. try //connection
  94. {
  95. if(con!=null)
  96. con.close();
  97.  
  98. }
  99. catch(SQLException se)
  100. {
  101. se.printStackTrace();
  102. }
  103.  
  104. } //psvm
  105.  
  106.  
  107.  
  108. } //main class
  109. `
Add Comment
Please, Sign In to add comment