Programme QCM java

Fermé
onebag - 22 juin 2012 à 10:35
je voudrais réaliser une application d'évaluation de qcm et j'ai un rpobléme pour recupere une question avec sa liste de réponse correspondant dans une base de données ( wamp server )
une question doit s'affcher puis une liste de réponse a cocher


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* QCM2.java
*
* Created on 2 juin 2012, 02:41:40
*/
package IHM;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import modele.*;
import controleur.*;
import java.sql.*;
import java.util.Timer;
import java.util.TimerTask;

/**
*
* @author Administrateur
*/
public class QCM2 extends javax.swing.JFrame {

private Timer timer2;
private PreparedStatement st = null;
private TimerTask time2 = new TimerTask() {

@Override
/**
* Tache a faire toute les seconde
*/
public void run() {
//a faire
if (temps.getText().equals("120")) {
JOptionPane.showMessageDialog(rootPane, "Temps imparti ecoulé le programme va se fermer!");
dispose();
}
temps.setText("" + Chrono.getNbSec());

}
};
private Connection con = null;

/**
* Creates new form QCM2
*/
public QCM2(Utilisateur ut, String theme) {
setVisible(true);
initComponents();
user.setText(ut.getPrenom() + " " + ut.getNom());
th.setText(theme);

try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/qcm", "root", "");
} catch (ClassNotFoundException ex) {
System.out.println(ex.getMessage());
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
//modification ajouter Récuperer les questios
try {
st = con.prepareStatement("select * from question");
ResultSet rs = null;
rs = st.executeQuery();
while (rs.next()) {
question.setText(rs.getString("libelle"));
}
} catch (SQLException e) {
e.getStackTrace();
}
//fin récupération

Chrono c = new Chrono();
c.reset();
c.start();

timer2 = new Timer();
timer2.schedule(time2, 0, 1000);
}

/**
* 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

div = new javax.swing.JLabel();
th = new javax.swing.JLabel();
REP2 = new javax.swing.JCheckBox();
question = new javax.swing.JLabel();
REP1 = new javax.swing.JCheckBox();
REP3 = new javax.swing.JCheckBox();
REP4 = new javax.swing.JCheckBox();
user = new javax.swing.JLabel();
BtnValider = new javax.swing.JButton();
BtnSuivant = new javax.swing.JButton();
res = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
temps = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
formFocusGained(evt);
}
});

div.setFont(new java.awt.Font("Tahoma", 3, 18)); // NOI18N
div.setText("1");

th.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
th.setText("THEME");

REP2.setText("REPONSE 2");

question.setText("LIBELLE DE LA QUESTION NUMERO ");

REP1.setText("REPONSE 1");
REP1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
REP1ActionPerformed(evt);
}
});

REP3.setText("REPONSE 3");

REP4.setText("REPONSE 4");

user.setBackground(new java.awt.Color(255, 153, 255));
user.setFont(new java.awt.Font("Tahoma", 3, 12)); // NOI18N
user.setForeground(new java.awt.Color(0, 102, 255));
user.setText("USER NAME");

BtnValider.setText("Valider");
BtnValider.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BtnValiderActionPerformed(evt);
}
});

BtnSuivant.setText("Suivant");
BtnSuivant.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BtnSuivantActionPerformed(evt);
}
});

res.setFont(new java.awt.Font("Tahoma", 3, 18)); // NOI18N
res.setForeground(new java.awt.Color(0, 153, 0));
res.setText("0");

jLabel6.setFont(new java.awt.Font("Tahoma", 3, 18)); // NOI18N
jLabel6.setText("/");

jLabel1.setFont(new java.awt.Font("Tahoma", 3, 24)); // NOI18N
jLabel1.setText("Q.C.M");

temps.setFont(new java.awt.Font("Tahoma", 3, 24)); // NOI18N
temps.setText("0");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(39, 39, 39)
.addComponent(BtnValider)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(BtnSuivant)
.addGap(116, 116, 116))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(REP3, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(REP4, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(REP2, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(REP1, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(th, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(user, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(question, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(res, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(1, 1, 1)
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(div, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(3, 3, 3)
.addComponent(temps, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(user, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(res, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(div, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(th, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(23, 23, 23)
.addComponent(question))
.addComponent(temps, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(REP1)
.addGap(5, 5, 5)
.addComponent(REP2)
.addGap(5, 5, 5)
.addComponent(REP3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(REP4)
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(BtnValider)
.addComponent(BtnSuivant))
.addGap(25, 25, 25))
);

pack();
}// </editor-fold>

private void BtnValiderActionPerformed(java.awt.event.ActionEvent evt) {

if (REP1.getSelectedObjects() == null && REP3.getSelectedObjects() == null && REP4.getSelectedObjects() == null && REP2.getSelectedObjects() != null) {

JOptionPane.showMessageDialog(rootPane, "Bonne Reponse, Bravo!");
res.setText("" + (Integer.parseInt(res.getText()) + 1));
} else {
JOptionPane.showMessageDialog(rootPane, "Mauvaise Reponse!");


}
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/qcm", "root", " ");
} catch (ClassNotFoundException ex) {
System.out.println(ex.getMessage());
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
//mdofication ajouter
try {
st = con.prepareStatement("SELECT * FROM 'reponse'");
ResultSet rs = null;
rs = st.executeQuery();
while (rs.next()) {
question.setText(rs.getString("libelle"));
REP1.setSelected(rs.getBoolean("est_correcte"));
// REP2.setSelected(false);
// REP3.setSelected(false);
// REP4.setSelected(false);
// BTNvalider.setVisible(false);
}
} catch (SQLException e) {
e.getStackTrace();
}


// REP1.setSelected(false);
// REP2.setSelected(false);
// REP3.setSelected(false);
// REP4.setSelected(false);
// BTNvalider.setVisible(false);
}

private void BtnSuivantActionPerformed(java.awt.event.ActionEvent evt) {

String c = div.getText();
int i = Integer.parseInt(c);
i++;
if (i > 5) {
JOptionPane.showMessageDialog(rootPane, "Fin du TesT");
dispose();
}
question.setText("LIBELLE DE LA QUESTION " + i);
REP1.setText("REPONSE N° " + (i + 4));
REP2.setText("REPONSE N° " + (i + 5));
REP3.setText("REPONSE N° " + (i + 6));
REP4.setText("REPONSE N° " + (i + 7));

div.setText("" + i);
BtnValider.setVisible(true);
// TODO add your handling code here:
}

private void REP1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void formFocusGained(java.awt.event.FocusEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify
private javax.swing.JButton BtnSuivant;
private javax.swing.JButton BtnValider;
private javax.swing.JCheckBox REP1;
private javax.swing.JCheckBox REP2;
private javax.swing.JCheckBox REP3;
private javax.swing.JCheckBox REP4;
private javax.swing.JLabel div;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel question;
private javax.swing.JLabel res;
private javax.swing.JLabel temps;
private javax.swing.JLabel th;
private javax.swing.JLabel user;
// End of variables declaration
}