Gestion des stagiaire avec ms-sql server base

Description

Gestion des stgaiaire avec une database mssql server liée avec le programme on mode bridge odbc jdbc. il contiens 2 class, class stagiaire et class pour la connection et la jfrom c'est le démarrage de prog.

Source / Exemple :


//Class > MyBase.java / connection avec la base. @facebook user : Mohamed Analyzer@
/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
  • /
package gesstg; import java.sql.*; import javax.swing.JOptionPane; /** *
  • @author Mohamed Lartab
  • /
public class MyBase { public static stagiaire stg; public static Connection cn; // fonction de la connection ^^ public static boolean connect(){ boolean x; try { // con = DriverManager.getConnection("jdbc:microsoft:sqlserver://MOHAMED-PC:1433/.;databaseName=stagiaire;selectMethod=cursor", "Mohamed", "v@") Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); cn = DriverManager.getConnection("jdbc:odbc:stagiaire"); x = true; } catch (Exception e) { x=false; JOptionPane.showMessageDialog(null, "ODBC ERROR Montez Database and Try", "X--ERROR", JOptionPane.ERROR_MESSAGE);} return x; } //fonction de disconnection public static boolean disconnect(){ boolean eta; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); cn.close(); eta = true; } catch (Exception e) { eta = false; JOptionPane.showMessageDialog(null, "Connexion f9r", "X--ERROR", JOptionPane.ERROR_MESSAGE); } return eta; } // renvoi de la connection public static Connection getConn() { return cn; } } // class stagiaire /*
  • To change this template, choose Tools | Templates
  • and open the template in the editor.
  • /
package gesstg; import java.sql.*; import javax.swing.table.*; import javax.swing.JOptionPane; /** *
  • @author Mohamed Lartab
  • /
public class stagiaire { public Statement stx; public String query; public ResultSet rset; public Connection cn; JOptionPane jop1, jop2, jop3; public stagiaire(){ MyBase.connect(); cn = MyBase.getConn(); } public void ajouter(String nom,String prenom,double moy,String date_nais){ try { //Execution de la requete query="INSERT INTO stg VALUES ('"+nom+"', '"+prenom+"', "+moy+", '"+date_nais+"')"; stx = cn.createStatement(); int rs = stx.executeUpdate(query); cn.commit(); } catch (SQLException ex) { } }// public void modifier(int mat,String nom,String prenom,double moy,String date_nais){ try { //Execution de la requete query="UPDATE stg SET nom='"+nom+"',prenom='"+prenom+"',moyenne="+moy+",daten='"+date_nais+"' WHERE mat="+mat+""; stx = cn.createStatement(); int rs = stx.executeUpdate(query); cn.commit(); } catch (SQLException ex) { } }// public void supprimer(int mat){ try { //Execution de la requete query="DELETE FROM stg WHERE mat = "+mat+""; stx = cn.createStatement(); int rs = stx.executeUpdate(query); cn.commit(); } catch (SQLException ex) { } }// @SuppressWarnings("static-access") public ResultSet recherchenom(String nom){ //Select * From stg WHERE nom='' try { //Execution de la requete query="SELECT * FROM stg WHERE nom = '"+nom+"'"; stx = cn.createStatement(); rset = stx.executeQuery(query); cn.commit(); rset.next(); } catch (SQLException ex) { } return rset; }// public ResultSet recherchemat(int mat){ //Select * From stg WHERE nom='' try { //Execution de la requete query="SELECT * FROM stg WHERE mat = "+mat+""; stx = cn.createStatement(); rset = stx.executeQuery(query); cn.commit(); rset.next(); } catch (SQLException ex) { } return rset; } public DefaultTableModel getl3aybat(){ DefaultTableModel dt = new DefaultTableModel(); dt.addColumn("Mat"); dt.addColumn("Nom"); dt.addColumn("Prenom"); dt.addColumn("Moyenne"); dt.addColumn("Date nais"); try { query="SELECT * FROM stg "; stx = cn.createStatement(); rset = stx.executeQuery(query); cn.commit(); while(rset.next()){ Object [] st = {rset.getInt(1),rset.getString(2),rset.getString(3),rset.getString(4),rset.getString(5)}; dt.addRow(st); } } catch (SQLException ex) { } return dt; } } // code de la jform /*
  • To change this template, choose Tools | Templates
  • and open the template in the editor.
  • /
/*
  • start.java
*
  • Created on Jun 5, 2012, 12:57:28 PM
  • /
package gesstg; import java.sql.*; import javax.swing.JOptionPane; /** *
  • @author Mohamed Lartab
  • /
public class start extends javax.swing.JFrame { int s; stagiaire stg = new stagiaire(); String d; int mat; double m; /** Creates new form start */ public start() { initComponents(); grp.add(rdb1); grp.add(rdb2); } public void afficher(){ dtgrid.setModel(stg.getl3aybat()); } public void khwiboxat(){ txtbox1.setText(""); txtbox2.setText(""); txtbox3.setText(""); txtbox4.setText(""); txtbox5.setText(""); } /** 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() { grp = new javax.swing.ButtonGroup(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton(); rdb1 = new javax.swing.JRadioButton(); rdb2 = new javax.swing.JRadioButton(); searchbox = new javax.swing.JTextField(); jButton5 = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); txtbox1 = new javax.swing.JTextField(); txtbox2 = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); txtbox3 = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); txtbox4 = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); txtbox5 = new javax.swing.JTextField(); jLabel5 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); dtgrid = new javax.swing.JTable(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosed(java.awt.event.WindowEvent evt) { formWindowClosed(evt); } public void windowOpened(java.awt.event.WindowEvent evt) { formWindowOpened(evt); } }); jButton1.setText("Ajouter"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("Modifier"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setText("Supprimer"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jButton4.setText("Rechercher"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } }); rdb1.setText("Par Nom"); rdb2.setText("Par Mat"); jButton5.setText("Afficher"); jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton5ActionPerformed(evt); } }); jLabel1.setText("Matricule : "); jLabel2.setText("Nom :"); jLabel3.setText("Prenom:"); jLabel4.setText("Moyenne:"); jLabel5.setText("Date nais:"); jLabel6.setForeground(new java.awt.Color(255, 102, 102)); jLabel6.setText("Gestion des Stagiaires"); dtgrid.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { {}, {}, {}, {} }, new String [] { } )); dtgrid.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { dtgridMouseClicked(evt); } }); jScrollPane1.setViewportView(dtgrid); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel6) .addContainerGap(659, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addGap(20, 20, 20) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel5) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(txtbox3, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(txtbox2, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addComponent(jLabel1) .addGap(28, 28, 28) .addComponent(txtbox1, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(jLabel4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(txtbox5, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txtbox4, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 48, Short.MAX_VALUE) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 492, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(22, 22, 22)) .addGroup(layout.createSequentialGroup() .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(searchbox, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(rdb1) .addComponent(rdb2)) .addGap(14, 14, 14)))) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel6) .addGap(35, 35, 35) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(txtbox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(txtbox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(txtbox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4) .addComponent(txtbox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5) .addComponent(txtbox5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createSequentialGroup() .addGap(32, 32, 32) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(28, 28, 28) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(searchbox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton2) .addComponent(jButton1) .addComponent(jButton3) .addComponent(jButton4) .addComponent(jButton5)) .addGap(9, 9, 9)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(rdb1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(rdb2))) .addGap(40, 40, 40)) ); pack(); }// </editor-fold> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { m = Double.valueOf(txtbox4.getText()); d= txtbox5.getText(); stg.ajouter(txtbox2.getText(),txtbox3.getText(),m,d); afficher(); } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { m = Double.valueOf(txtbox4.getText()); mat = Integer.valueOf(txtbox1.getText()); stg.modifier(mat,txtbox2.getText(),txtbox3.getText(),m,txtbox5.getText()); afficher(); } private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { mat = Integer.valueOf(txtbox1.getText()); stg.supprimer(mat); afficher(); } private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { khwiboxat(); try { if (rdb1.isSelected()){ stg.rset = stg.recherchenom(searchbox.getText()); }else if (rdb2.isSelected()){ mat = Integer.valueOf(searchbox.getText()); stg.rset = stg.recherchemat(mat); } if (stg.rset.getRow()<=0){ JOptionPane.showMessageDialog(null, " Stagiaire n'exist pas ", "X--ERROR", JOptionPane.ERROR_MESSAGE); }else{ txtbox1.setText(stg.rset.getString(1)); txtbox2.setText(stg.rset.getString(2)); txtbox3.setText(stg.rset.getString(3)); txtbox4.setText(stg.rset.getString(4).substring(0, 3)); txtbox5.setText(stg.rset.getString(5)); } } catch (SQLException ex) { } } private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) { afficher(); } private void formWindowOpened(java.awt.event.WindowEvent evt) { afficher(); } private void dtgridMouseClicked(java.awt.event.MouseEvent evt) { try { s = dtgrid.getSelectedRow(); }catch(Exception e){ } } private void formWindowClosed(java.awt.event.WindowEvent evt) { MyBase.disconnect(); } /**
  • @param args the command line arguments
  • /
public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 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(start.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(start.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(start.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(start.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { new start().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JTable dtgrid; private javax.swing.ButtonGroup grp; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JButton jButton5; private javax.swing.JLabel jLabel1; 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.JScrollPane jScrollPane1; private javax.swing.JRadioButton rdb1; private javax.swing.JRadioButton rdb2; private javax.swing.JTextField searchbox; private javax.swing.JTextField txtbox1; private javax.swing.JTextField txtbox2; private javax.swing.JTextField txtbox3; private javax.swing.JTextField txtbox4; private javax.swing.JTextField txtbox5; // End of variables declaration }

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.