Advertisement
marcelpace

Quiz em Java

Sep 18th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. package quizzes;
  2.  
  3. import java.awt.GridLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11.  
  12. import javax.swing.ButtonGroup;
  13. import javax.swing.JButton;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JOptionPane;
  17. import javax.swing.JRadioButton;
  18. import javax.swing.JTextField;
  19.  
  20. public class Main extends JFrame implements ActionListener {
  21. String[][] perguntas = new String[3][6];
  22. JRadioButton[] jrPerguntas = new JRadioButton[4];
  23. JLabel jlPergunta = new JLabel("");
  24. JLabel jlPontuacao = new JLabel("Pontuacao: 0");
  25. int posAtual = 0;
  26. int pontos = 0;
  27. JButton jbResponder = new JButton("Responder");
  28. ButtonGroup bgOp = new ButtonGroup();
  29.  
  30. public Main() {
  31. super("Quizz");
  32. setLayout(new GridLayout(7, 1));
  33. carregaPerguntas();
  34.  
  35. for (int id = 0; id < 4; id++) {
  36. jrPerguntas[id] = new JRadioButton();
  37. bgOp.add(jrPerguntas[id]);
  38. }
  39.  
  40. montaTela();
  41. jlPontuacao.setHorizontalAlignment(JTextField.RIGHT);
  42. add(jlPergunta);
  43. add(jrPerguntas[0]);
  44. add(jrPerguntas[1]);
  45. add(jrPerguntas[2]);
  46. add(jrPerguntas[3]);
  47. add(jlPontuacao);
  48. add(jbResponder);
  49. jbResponder.addActionListener(this);
  50. pack();
  51. setLocationRelativeTo(null);
  52. setVisible(true);
  53. }
  54.  
  55. public void carregaPerguntas() {
  56.  
  57. Connection conn = null;
  58. PreparedStatement stmt = null;
  59. ResultSet rs = null;
  60.  
  61. try {
  62. String driver = "com.mysql.jdbc.Driver";
  63. String url = "jdbc:mysql://localhost/quizzes";
  64. String user = "root";
  65. String pass = "";
  66.  
  67. String sql1 = "SELECT COUNT(*) as qtd FROM Perguntas";
  68. String sql2 = "SELECT * FROM perguntas as p, respostas as r WHERE p.COD_Pergunta = r.COD_Pergunta";
  69.  
  70. Class.forName(driver);
  71. conn = DriverManager.getConnection(url, user, pass);
  72. stmt = conn.prepareStatement(sql1);
  73. rs = stmt.executeQuery();
  74.  
  75. rs.next();
  76. int qtdP = rs.getInt("qtd");
  77.  
  78. stmt = conn.prepareStatement(sql2);
  79. rs = stmt.executeQuery();
  80.  
  81. String pergunta = rs.getString("perguntas");
  82. System.out.println(pergunta);
  83.  
  84. String resposta = rs.getString("respostas");
  85. System.out.println(resposta);
  86.  
  87. int codPergunta = rs.getInt("COD_Pergunta");
  88. int codResposta = rs.getInt("COD_Resposta");
  89.  
  90. rs.next();
  91. for (int i = 1; i < qtdP; i++) {
  92. perguntas[i][0] = rs.getString("perguntas");
  93.  
  94. for (int j = 1; j < 4; j++) {
  95. if (rs.getString("CertoErrado") == "e") {
  96. perguntas[i][j] = rs.getString("respostas");
  97. } else {
  98. perguntas[i][4] = String.valueOf(j);
  99. }
  100. rs.next();
  101.  
  102. }
  103. }
  104. }
  105.  
  106. catch (ClassNotFoundException ex) {
  107. System.out.println("Driver não encontrado!");
  108. }
  109.  
  110. catch (SQLException ex) {
  111. System.out.println("Erro de banco: " + ex.getMessage());
  112. }
  113.  
  114. finally {
  115.  
  116. if (rs != null) {
  117. try {
  118. rs.close();
  119. } catch (SQLException ex) {
  120. }
  121. }
  122.  
  123. if (stmt != null) {
  124. try {
  125. stmt.close();
  126. } catch (SQLException ex) {
  127. }
  128. }
  129.  
  130. if (conn != null) {
  131. try {
  132. conn.close();
  133. } catch (SQLException ex) {
  134. }
  135. }
  136. }
  137. }
  138.  
  139. public void montaTela() {
  140. jlPergunta.setText(perguntas[posAtual][0]);
  141. for (int id = 0; id < 4; id++) {
  142. jrPerguntas[id].setText(perguntas[posAtual][id + 1]);
  143. }
  144. }
  145.  
  146. public static void main(String[] args) {
  147. new Main();
  148. }
  149.  
  150. @Override
  151. public void actionPerformed(ActionEvent e) {
  152. // TODO Auto-generated method stub
  153. int respostaCerta = Integer.parseInt(perguntas[posAtual][5]);
  154.  
  155. if (jrPerguntas[respostaCerta - 1].isSelected()) {
  156. pontos++;
  157. } else {
  158. pontos--;
  159. }
  160. jlPontuacao.setText("Pontuacao: " + pontos);
  161. if (posAtual < 2)
  162. posAtual++;
  163. else
  164. JOptionPane.showMessageDialog(this, "Game Over");
  165. montaTela();
  166.  
  167. }
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement