Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.65 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package tp3.bdd.ex2a5;
  8.  
  9. import java.sql.Connection;
  10. import java.sql.DatabaseMetaData;
  11. import java.sql.DriverManager;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.sql.Statement;
  15. import java.util.logging.Level;
  16. import java.util.logging.Logger;
  17. import javax.swing.table.DefaultTableModel;
  18.  
  19. /**
  20.  *
  21.  * @author kasprzykn
  22.  */
  23. public class NewJFrame extends javax.swing.JFrame {
  24.  
  25.     /**
  26.      * Creates new form NewJFrame
  27.      */
  28.     public NewJFrame() {
  29.         initComponents();
  30.     }
  31.     private Connection conn;
  32.     private Statement stmt;
  33.     private DatabaseMetaData meta;
  34.     private String pass;
  35.     /**
  36.      * This method is called from within the constructor to initialize the form.
  37.      * WARNING: Do NOT modify this code. The content of this method is always
  38.      * regenerated by the Form Editor.
  39.      */
  40.     @SuppressWarnings("unchecked")
  41.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  42.     private void initComponents() {
  43.  
  44.         jLabel1 = new javax.swing.JLabel();
  45.         jLabel2 = new javax.swing.JLabel();
  46.         login = new javax.swing.JTextField();
  47.         jButton1 = new javax.swing.JButton();
  48.         liste = new javax.swing.JComboBox();
  49.         jScrollPane1 = new javax.swing.JScrollPane();
  50.         table = new javax.swing.JTable();
  51.         pwd = new javax.swing.JPasswordField();
  52.  
  53.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  54.  
  55.         jLabel1.setText("Login");
  56.  
  57.         jLabel2.setText("Password");
  58.  
  59.         login.setText("IQ_BD_");
  60.         login.setName("login"); // NOI18N
  61.  
  62.         jButton1.setText("OK");
  63.         jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
  64.             public void mouseClicked(java.awt.event.MouseEvent evt) {
  65.                 jButton1MouseClicked(evt);
  66.             }
  67.         });
  68.  
  69.         liste.setEnabled(false);
  70.         liste.addMouseListener(new java.awt.event.MouseAdapter() {
  71.             public void mouseClicked(java.awt.event.MouseEvent evt) {
  72.                 listeMouseClicked(evt);
  73.             }
  74.         });
  75.         liste.addItemListener(new java.awt.event.ItemListener() {
  76.             public void itemStateChanged(java.awt.event.ItemEvent evt) {
  77.                 listeItemStateChanged(evt);
  78.             }
  79.         });
  80.  
  81.         table.setModel(new javax.swing.table.DefaultTableModel(
  82.             new Object [][] {
  83.  
  84.             },
  85.             new String [] {
  86.                 "Colonne", "Type"
  87.             }
  88.         ) {
  89.             boolean[] canEdit = new boolean [] {
  90.                 false, false
  91.             };
  92.  
  93.             public boolean isCellEditable(int rowIndex, int columnIndex) {
  94.                 return canEdit [columnIndex];
  95.             }
  96.         });
  97.         jScrollPane1.setViewportView(table);
  98.  
  99.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  100.         getContentPane().setLayout(layout);
  101.         layout.setHorizontalGroup(
  102.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  103.             .addGroup(layout.createSequentialGroup()
  104.                 .addGap(35, 35, 35)
  105.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  106.                     .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING)
  107.                     .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
  108.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  109.                             .addComponent(jLabel1)
  110.                             .addComponent(jLabel2))
  111.                         .addGap(26, 26, 26)
  112.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  113.                             .addComponent(login, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)
  114.                             .addComponent(pwd))))
  115.                 .addGap(33, 33, 33)
  116.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  117.                     .addGroup(layout.createSequentialGroup()
  118.                         .addGap(235, 235, 235)
  119.                         .addComponent(liste, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  120.                     .addGroup(layout.createSequentialGroup()
  121.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  122.                         .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 324, javax.swing.GroupLayout.PREFERRED_SIZE)))
  123.                 .addContainerGap())
  124.         );
  125.         layout.setVerticalGroup(
  126.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  127.             .addGroup(layout.createSequentialGroup()
  128.                 .addGap(83, 83, 83)
  129.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  130.                     .addComponent(jLabel1)
  131.                     .addComponent(login, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  132.                     .addComponent(liste, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  133.                 .addGap(31, 31, 31)
  134.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  135.                     .addGroup(layout.createSequentialGroup()
  136.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  137.                             .addComponent(jLabel2)
  138.                             .addComponent(pwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  139.                         .addGap(18, 18, 18)
  140.                         .addComponent(jButton1))
  141.                     .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE))
  142.                 .addContainerGap(30, Short.MAX_VALUE))
  143.         );
  144.  
  145.         pack();
  146.     }// </editor-fold>//GEN-END:initComponents
  147.  
  148.     private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
  149.         try{
  150.             DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  151.             pass=new String(pwd.getPassword());
  152.             conn = DriverManager.getConnection("jdbc:oracle:thin:@srv-iq-bdd:1521:iqbd", login.getText(), pass);
  153.             stmt = conn.createStatement();
  154.             String query = "SELECT table_name FROM user_tables";
  155.            
  156.            
  157.             ResultSet rset = stmt.executeQuery (query);
  158.             String str;
  159.             liste.setEnabled(true);
  160.             while (rset.next())
  161.             {
  162.                 str = rset.getString(1);
  163.                 liste.addItem(str);
  164.             }
  165.            
  166.             rset.close();
  167.            
  168.         }
  169.         catch(SQLException e){
  170.             System.out.println("ERR : "+e.getErrorCode()+":"+e.getMessage());
  171.         }
  172.     }//GEN-LAST:event_jButton1MouseClicked
  173.  
  174.     private void listeItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_listeItemStateChanged
  175.         try {
  176.            
  177.             meta = conn.getMetaData();
  178.             ResultSet rsColonnes=meta.getColumns(null, null, liste.getSelectedItem()+"", null);
  179.             DefaultTableModel model=(DefaultTableModel)table.getModel();
  180.            
  181.             if (model.getRowCount()>0){
  182.                 for (int i=0;i<model.getRowCount();i++){
  183.                     model.removeRow(i);
  184.                 }
  185.             }
  186.             table.setModel(model);
  187.             while (rsColonnes.next()) {
  188.                
  189.                 model.addRow(new Object[]{rsColonnes.getString("COLUMN_NAME"),rsColonnes.getString("TYPE_NAME")});
  190.             }
  191.             rsColonnes.close();
  192.         } catch (SQLException e) {
  193.             System.out.println("ERR : "+e.getErrorCode()+":"+e.getMessage());
  194.         }
  195.     }//GEN-LAST:event_listeItemStateChanged
  196.  
  197.     private void listeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listeMouseClicked
  198.        
  199.        
  200.     }//GEN-LAST:event_listeMouseClicked
  201.  
  202.     /**
  203.      * @param args the command line arguments
  204.      */
  205.     public static void main(String args[]) {
  206.         /* Set the Nimbus look and feel */
  207.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  208.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  209.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  210.          */
  211.         try {
  212.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  213.                 if ("Nimbus".equals(info.getName())) {
  214.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  215.                     break;
  216.                 }
  217.             }
  218.         } catch (ClassNotFoundException ex) {
  219.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  220.         } catch (InstantiationException ex) {
  221.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  222.         } catch (IllegalAccessException ex) {
  223.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  224.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  225.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  226.         }
  227.         //</editor-fold>
  228.  
  229.         /* Create and display the form */
  230.         java.awt.EventQueue.invokeLater(new Runnable() {
  231.             public void run() {
  232.                 new NewJFrame().setVisible(true);
  233.             }
  234.         });
  235.        
  236.     }
  237.  
  238.     // Variables declaration - do not modify//GEN-BEGIN:variables
  239.     private javax.swing.JButton jButton1;
  240.     private javax.swing.JLabel jLabel1;
  241.     private javax.swing.JLabel jLabel2;
  242.     private javax.swing.JScrollPane jScrollPane1;
  243.     private javax.swing.JComboBox liste;
  244.     private javax.swing.JTextField login;
  245.     private javax.swing.JPasswordField pwd;
  246.     private javax.swing.JTable table;
  247.     // End of variables declaration//GEN-END:variables
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement