Estou fazendo um quiz mas não consigo fazer o jframe mostrar o banco de dados.
código do JFRAME:
> package view;
import Project.ConnectionProvider; import java.awt.HeadlessException; import java.sql.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JOptionPane; import javax.swing.Timer;/*
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
/
/*@author tetep
*/
public class PainelQuiz extends javax.swing.JFrame {public String Id = “1”; public String answer = “1”; public int min = 0; public int sec = 0; public String marks = “0”; public String rollNo = “1”;public void answerCheck(){ String studentAnswer = “”; if(jRadioButton1.isSelected()){ studentAnswer = jRadioButton1.getText(); } else if(jRadioButton2.isSelected()){ studentAnswer = jRadioButton2.getText(); }else if(jRadioButton3.isSelected()){ studentAnswer = jRadioButton3.getText(); }if(studentAnswer.equals(answer)){ marks=marks+1; String marks1 = String.valueOf(marks); jLabel13.setText(marks1); } //numero da questao muda int Id1=Integer.parseInt(Id); Id1=Id1+1; Id = String.valueOf(Id1); // jRadioButton1.setSelected(false); jRadioButton2.setSelected(false); jRadioButton3.setSelected(false); // if(Id.equals("10")){ jButton3.setVisible(false); }}
public void question(){ try{ Connection con = ConnectionProvider.getCon(); Statement st = con.createStatement(); ResultSet rs1 = st.executeQuery(“select *from question where id=’”+Id+"’");while(rs1.next()){ jLabel4.setText(rs1.getString(1)); jLabel7.setText(rs1.getString(2)); jRadioButton1.setText(rs1.getString(3)); jRadioButton2.setText(rs1.getString(4)); jRadioButton3.setText(rs1.getString(5)); answer = rs1.getString(6); } } catch(SQLException e){ JOptionPane.showMessageDialog(null,e); }}
public void submit(){
answerCheck(); try { Connection con = ConnectionProvider.getCon(); Statement st = con.createStatement(); st.executeUpdate("update score set marks='"+marks+""); String marks1 = String.valueOf(marks); JOptionPane.showMessageDialog(null,marks1); } catch(HeadlessException | SQLException e){ JOptionPane.showMessageDialog(null,e); }}
/**
- Creates new form pAINELqUIZ
*/
public PainelQuiz() {
initComponents();}
Timer time;
/** * */ public final void PainelQuiz() { initComponents();try{ Connection con = ConnectionProvider.getCon(); Statement st = con.createStatement(); ResultSet rs1 = st.executeQuery("select *from quiz.question where id='"+Id+"'"); while(rs1.next()){ jLabel4.setText(rs1.getString(1)); jLabel7.setText(rs1.getString(2)); jRadioButton1.setText(rs1.getString(3)); jRadioButton2.setText(rs1.getString(4)); jRadioButton3.setText(rs1.getString(5)); answer = rs1.getString(6); } } catch(SQLException e){ JOptionPane.showMessageDialog(null,e); } //tempo setLocationRelativeTo(this); time = new Timer(1000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jLabel12.setText(String.valueOf(sec)); jLabel11.setText(String.valueOf(min)); if(sec == 60){ sec = 0; min++; if(min==10){ time.stop(); answerCheck(); submit(); } } sec++; } });
time.start();
}/**
This method is called from within the constructor to initialize the form.
WARNING: Do NOT modify this code. The content of this method is always
regenerated by the Form Editor. */ @SuppressWarnings(“unchecked”) // private void initComponents() {buttonGroup1 = new javax.swing.ButtonGroup(); buttonGroup2 = new javax.swing.ButtonGroup(); buttonGroup3 = new javax.swing.ButtonGroup(); buttonGroup4 = new javax.swing.ButtonGroup(); jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jSeparator1 = new javax.swing.JSeparator(); jLabel2 = new javax.swing.JLabel(); jProgressBar1 = new javax.swing.JProgressBar(); jProgressBar2 = new javax.swing.JProgressBar(); jRadioButton1 = new javax.swing.JRadioButton(); jRadioButton2 = new javax.swing.JRadioButton(); jRadioButton3 = new javax.swing.JRadioButton(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); jLabel8 = new javax.swing.JLabel(); jLabel9 = new javax.swing.JLabel(); jLabel10 = new javax.swing.JLabel(); jLabel11 = new javax.swing.JLabel(); jLabel12 = new javax.swing.JLabel(); jLabel13 = new javax.swing.JLabel(); jButton3 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); jMenu2 = new javax.swing.JMenu();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/resources/bulbasauro2.gif"))); // NOI18NjLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/resources/miau.gif"))); // NOI18N jLabel2.setMaximumSize(new java.awt.Dimension(536, 500)); jLabel2.setMinimumSize(new java.awt.Dimension(536, 500));jRadioButton1.setText(“jRadioButton1”); jRadioButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jRadioButton1ActionPerformed(evt); } });jRadioButton2.setText(“jRadioButton2”); jRadioButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jRadioButton2ActionPerformed(evt); } });jRadioButton3.setText(“jRadioButton3”); jRadioButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jRadioButton3ActionPerformed(evt); } });
jLabel3.setText(“Questao Número:”);
jLabel4.setLabelFor(jLabel4);
jLabel4.setText(“00”);
jLabel6.setText(“Questão”);
jLabel7.setText(“jLabel7”);
jLabel8.setText(“Roll Number”);
jLabel9.setText(“00”);
jLabel10.setText(“Tempo”);
jLabel11.setText(“00”);
jLabel12.setText(“00”);
jLabel13.setText(“00”);javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(jSeparator1)) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jProgressBar2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(18, 18, 18) .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel13) .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(0, 0, Short.MAX_VALUE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(39, 39, 39) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jRadioButton2) .addComponent(jRadioButton1) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel6) .addGap(498, 498, 498) .addComponent(jLabel5) .addGap(40, 40, 40) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel8) .addComponent(jLabel3))) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 549, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 95, Short.MAX_VALUE) .addComponent(jLabel10))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel4) .addComponent(jLabel9) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel11) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel12)))) .addComponent(jRadioButton3)) .addGap(0, 36, Short.MAX_VALUE))) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(191, 191, 191) .addComponent(jLabel1)) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(96, 96, 96) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jProgressBar2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(40, 40, 40) .addComponent(jLabel13) .addGap(18, 18, 18) .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(83, 83, 83) .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(jLabel4) .addComponent(jLabel6)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel5) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel8) .addComponent(jLabel9)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel10) .addComponent(jLabel11) .addComponent(jLabel12)))))) .addGap(18, 18, 18) .addComponent(jRadioButton1) .addGap(3, 3, 3) .addComponent(jRadioButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE) .addComponent(jRadioButton3)) );jButton3.setText(“Próximo”); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } });jButton4.setText(“encerrar”); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } });
jMenu1.setText(“File”);
jMenuBar1.add(jMenu1);
jMenu2.setText(“Edit”);
jMenuBar1.add(jMenu2);setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addGap(49, 49, 49) .addComponent(jButton3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton4) .addGap(63, 63, 63)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton3) .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(13, 13, 13)) );pack();
}//private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: if(jRadioButton1.isSelected()){ jRadioButton2.setSelected(false); jRadioButton3.setSelected(false);}}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: answerCheck(); question();}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here: int a = JOptionPane.showConfirmDialog(null, "Realmente quer sair?","Selecione",JOptionPane.YES_NO_OPTION); if(a==0){ answerCheck(); submit(); }}
private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: if(jRadioButton2.isSelected()){ jRadioButton1.setSelected(false); jRadioButton3.setSelected(false);}}
private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: if(jRadioButton3.isSelected()){ jRadioButton2.setSelected(false); jRadioButton1.setSelected(false);}}
/**
@param args the command line arguments
/
public static void main(String args[]) {
/ Set the Nimbus look and feel /
//
/ If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if (“Nimbus”.equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(PainelQuiz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(PainelQuiz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(PainelQuiz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(PainelQuiz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
///* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new PainelQuiz().setVisible(true); } }); }// Variables declaration - do not modify private javax.swing.ButtonGroup buttonGroup1; private javax.swing.ButtonGroup buttonGroup2; private javax.swing.ButtonGroup buttonGroup3; private javax.swing.ButtonGroup buttonGroup4; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel10; private javax.swing.JLabel jLabel11; private javax.swing.JLabel jLabel12; private javax.swing.JLabel jLabel13; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JMenu jMenu1; private javax.swing.JMenu jMenu2; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JPanel jPanel1; private javax.swing.JProgressBar jProgressBar1; private javax.swing.JProgressBar jProgressBar2; private javax.swing.JRadioButton jRadioButton1; private javax.swing.JRadioButton jRadioButton2; private javax.swing.JRadioButton jRadioButton3; private javax.swing.JSeparator jSeparator1; // End of variables declaration }
Código da conecção com o banco:
package Project;
import java.sql.*;/**
*
- @author tetep
*/
public class ConnectionProvider {
public static Connection getCon(){
try{
Class.forName(“com.mysql.jdbc.Driver”);
Connection con = DriverManager.getConnection(“jdbc:mysql://localhost:3306:/quiz”,“root”,"");
return con;
}
catch(ClassNotFoundException | SQLException e){
return null;
}
}}
código que chama o jframe após o botão ser acionado:
private ProjectButton createButtonToStart() { ProjectButton startButton = new ProjectButton(“COMEÇAR”); startButton.setLayoutX(350); startButton.setLayoutY(300);PainelQuiz painel = new PainelQuiz(); startButton.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent arg0) { gameStage.close(); painel.setVisible(true); painel.PainelQuiz(); } }); return startButton; //}