1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
class Fenetre extends JFrame implements ActionListener
{
private JButton bouton2;
private Map<String, JCheckBox>boxes = new HashMap<String, JCheckBox>();
private Iterator it;
public Fenetre()
{
setTitle("Questionnaire audit");
Container co = getContentPane();
co.setLayout(new FlowLayout());
setSize(500, 300);
setLocationRelativeTo(this.getParent());
setDefaultCloseOperation(3);
Connection c = null;
Statement stmnt = null;
//on essai d'etablir la connection
try
{
//on renseigne le driver
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
// on donne les parametre de la connection : quest étant ce que l'on a defini dans le panneau de controle
c = DriverManager.getConnection( "jdbc:odbc:quest", "", "" );
stmnt = c.createStatement();
//notre requete sql
String query = "select chapitre from [quess$] order by ide asc;";
//on met le resultat de notre requete dans un resultset
ResultSet rs = stmnt.executeQuery( query );
//on boucle sur notre resultset
while( rs.next() )
{
boxes.put(rs.getString( "chapitre" ),new JCheckBox());
}
}
catch( Exception e )
{
System.err.println( e );
}
finally
{
try
{
stmnt.close();
c.close();
}
catch( Exception e )
{
System.err.println( e );
}
}
JCheckBox box;
for(String name : boxes.keySet())
{
box = boxes.get(name);
box.setLabel(name);
}
it = boxes.keySet().iterator();
while (it.hasNext())
{
co.add(boxes.get(it.next()));
}
bouton2 = new JButton("Valider");
co.add(bouton2);
bouton2.addActionListener(this);
}
public void actionPerformed(ActionEvent a)
{
String tab[] = new String[boxes.size()];
int i= 0;
JCheckBox box;
for(String name : boxes.keySet())
{
box = boxes.get(name);
if(box.isSelected())
{
tab[i] = (String)box.getLabel();
i++;
}
}
//construction de la requete SQL
String query ="select question from [ques$],[quess$] where ide=chap AND chapitre IN (";
for(int j=0;j<i;j++)
{
if(j< i- 1)
{
tab[j]="'"+tab[j]+"'";
query += tab[j]+"," ;
}
else
{
tab[j]="'"+tab[j]+"'";
query += tab[i-1]+")order by id";
}
}
Connection c2 = null;
Statement stmnt2 = null;
try
{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
c2 = DriverManager.getConnection( "jdbc:odbc:quest", "", "" );
stmnt2 = c2.createStatement();
ResultSet rs2 = stmnt2.executeQuery( query );
while( rs2.next() )
{
System.out.println(rs2.getString( "question" ));
}
}
catch( Exception e )
{
System.err.println( e );
}
finally
{
try
{
stmnt2.close();
c2.close();
}
catch( Exception e )
{
System.err.println( e );
}
}
}
} |
Partager