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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
package client;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JLabel;
import javax.swing.JTextField;
import message.Message;
import mise_en_forme.JTextLimited;
import connexion_bd.ConectDB;
import javax.swing.ImageIcon;
import java.awt.Toolkit;
import javax.swing.SwingConstants;
import javax.swing.JComboBox;
public class Ajouter_Cilent extends JDialog {
private static final long serialVersionUID = 1L;
private final JPanel contentPanel = new JPanel();
private JTextField text_nom;
private JTextField text_prenom;
private JTextField text_tel;
ConectDB con=null;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
Ajouter_Cilent dialog = new Ajouter_Cilent();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
* @throws SQLException
*/
public Ajouter_Cilent() throws SQLException {
setIconImage(Toolkit.getDefaultToolkit().getImage(Ajouter_Cilent.class.getResource("/images/logo_sipa.png")));
setTitle("Enr\u00E9gistrement d'un client");
setModal(true);
setBounds(100, 100, 455, 304);
getContentPane().setLayout(new BorderLayout());
setLocationRelativeTo(getParent());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
setLocationRelativeTo(this.getParent());
con=new ConectDB();
JLabel lblEnrgistrerUnNouveau = new JLabel("Enr\u00E9gistrer un nouveau client");
lblEnrgistrerUnNouveau.setBounds(65, 11, 300, 33);
Font f=new Font("Serif", Font.PLAIN, 25);
lblEnrgistrerUnNouveau.setFont(f);
contentPanel.add(lblEnrgistrerUnNouveau);
{
JLabel lblNom = new JLabel("Nom:");
lblNom.setHorizontalAlignment(SwingConstants.RIGHT);
lblNom.setBounds(12, 97, 100, 30);
contentPanel.add(lblNom);
}
{
JLabel lblPrnom = new JLabel("Pr\u00E9nom:");
lblPrnom.setHorizontalAlignment(SwingConstants.RIGHT);
lblPrnom.setBounds(12, 137, 100, 30);
contentPanel.add(lblPrnom);
}
{
JLabel lblTlphone = new JLabel("T\u00E9l\u00E9phone:");
lblTlphone.setHorizontalAlignment(SwingConstants.RIGHT);
lblTlphone.setBounds(12, 177, 100, 30);
contentPanel.add(lblTlphone);
}
text_nom = new JTextField();
text_nom.setFont(new Font("Dialog", Font.PLAIN, 12));
text_nom.setBounds(122, 97, 300, 30);
contentPanel.add(text_nom);
text_nom.setColumns(10);
{
text_prenom = new JTextField();
text_prenom.setFont(new Font("Dialog", Font.PLAIN, 12));
text_prenom.setBounds(122, 137, 300, 30);
contentPanel.add(text_prenom);
text_prenom.setColumns(10);
}
{
text_tel = new JTextField();
text_tel.setFont(new Font("Dialog", Font.PLAIN, 12));
text_tel.setBounds(122, 177, 300, 30);
JTextLimited jt=new JTextLimited();
text_tel.setDocument(jt);
contentPanel.add(text_tel);
text_tel.setColumns(10);
}
JLabel lblAgence = new JLabel("Agence:");
lblAgence.setHorizontalAlignment(SwingConstants.RIGHT);
lblAgence.setBounds(12, 56, 100, 30);
contentPanel.add(lblAgence);
final JComboBox comboBox = new JComboBox();
comboBox.setBounds(122, 56, 300, 33);
comboBox.addItem("");
if(con.parm.getType().equals("Structure")){
PreparedStatement pre;
pre = ConectDB.con.prepareStatement("select agence.nom from agence,structure where agence.id_structure=structure.id and structure.code=?");
pre.setString(1, con.parm.getValeur());
ResultSet res2=pre.executeQuery();
while(res2.next()){
comboBox.addItem(res2.getString("nom"));
}
pre.close();
}else{
PreparedStatement pre;
pre = ConectDB.con.prepareStatement("select nom from agence where code=?");
pre.setString(1, con.parm.getValeur());
ResultSet res2=pre.executeQuery();
while(res2.next()){
comboBox.addItem(res2.getString("nom"));
}
pre.close();
}
contentPanel.add(comboBox);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("Enr\u00E9gistrer");
okButton.setIcon(new ImageIcon(Ajouter_Cilent.class.getResource("/icones/mod.gif")));
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(text_nom.getText().length()!=0){
if(text_prenom.getText().length()!=0){
if(text_tel.getText().length()!=0){
if(comboBox.getSelectedItem().toString().length()!=0){
PreparedStatement pre;
int id_client=0;
int id_agence=0;
try {
pre=ConectDB.con.prepareStatement("select max(client.id) as client from client,agence where client.id_agence=agence.id and agence.nom=?");
pre.setString(1, comboBox.getSelectedItem().toString());
ResultSet res=pre.executeQuery();
while(res.next()){
id_client=res.getInt("client");
}
pre.close();
pre=ConectDB.con.prepareStatement("select id from agence where nom=?");
pre.setString(1, comboBox.getSelectedItem().toString());
ResultSet res2=pre.executeQuery();
while(res2.next()){
id_agence=res2.getInt("id");
}
pre.close();
id_client++;
String[] prenoms=text_prenom.getText().split(" ");
String code="CL"+id_client;
String prenom="";
for(int i=0;i<prenoms.length;i++){
prenom=prenom+prenoms[i].substring(0,1).toUpperCase()+prenoms[i].substring(1,prenoms[i].length())+" ";
}
prenom=prenom.substring(0,prenom.length()-1);
pre = ConectDB.con.prepareStatement("insert into client(nom,prenom,tel,code,id_agence) values (?,?,?,?,?)");
pre.setString(1, text_nom.getText().toUpperCase());
pre.setString(2, prenom);
pre.setInt(3, Integer.parseInt(text_tel.getText()));
pre.setString(4, code);
pre.setInt(5, id_agence);
pre.executeUpdate();
pre.close();
new Message().dialog("Enrégistrement effectué avec succès");
text_nom.setText("");
text_prenom.setText("");
text_tel.setText("");
comboBox.setSelectedIndex(0);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
new Message().dialog(e.getMessage());
}
}else new Message().dialog("Le champ:agence ne doit pas être vide!");
}else new Message().dialog("Le champ: Téléphone ne doit pas être vide!");
}else new Message().dialog("Le champ: Prénom ne doit pas être vide!");
}else new Message().dialog("Le champ: Nom ne doit pas être vide!");
}
});
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.setIcon(new ImageIcon(Ajouter_Cilent.class.getResource("/icones/suppr.png")));
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
dispose();
}
});
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
}
} |
Partager