Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.90 KB | None | 0 0
  1. package db;
  2.  
  3. import java.sql.*;
  4.  
  5. public class DbFrame extends javax.swing.JFrame {
  6.  
  7.    
  8.     private Connection con;
  9.     private Statement st;
  10.     private PreparedStatement pst;
  11.     private ResultSet rs;
  12.    
  13.    
  14.     public DbFrame() {
  15.         initComponents();
  16.        
  17.         try
  18.         {
  19.             Class.forName("com.mysql.jdbc.Driver");
  20.             con = DriverManager.getConnection("jdbc:mysql://localhost:3306/emp","root","");
  21.             st = con.createStatement();
  22.         }
  23.         catch(Exception e)
  24.         {
  25.             System.out.println(e);
  26.         }
  27.                
  28.        
  29.        
  30.     }
  31.  
  32.     @SuppressWarnings("unchecked")
  33.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  34.     private void initComponents() {
  35.  
  36.         jScrollPane1 = new javax.swing.JScrollPane();
  37.         jTextArea1 = new javax.swing.JTextArea();
  38.         jButton1 = new javax.swing.JButton();
  39.         jTextField1 = new javax.swing.JTextField();
  40.         jTextField2 = new javax.swing.JTextField();
  41.         jLabel1 = new javax.swing.JLabel();
  42.         jLabel2 = new javax.swing.JLabel();
  43.         jButton2 = new javax.swing.JButton();
  44.         jButton3 = new javax.swing.JButton();
  45.  
  46.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  47.  
  48.         jTextArea1.setEditable(false);
  49.         jTextArea1.setColumns(20);
  50.         jTextArea1.setRows(5);
  51.         jScrollPane1.setViewportView(jTextArea1);
  52.  
  53.         jButton1.setText("Display");
  54.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  55.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  56.                 jButton1ActionPerformed(evt);
  57.             }
  58.         });
  59.  
  60.         jLabel1.setText("Name");
  61.  
  62.         jLabel2.setText("Salary");
  63.  
  64.         jButton2.setText("Save");
  65.         jButton2.addActionListener(new java.awt.event.ActionListener() {
  66.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  67.                 jButton2ActionPerformed(evt);
  68.             }
  69.         });
  70.  
  71.         jButton3.setText("Clear");
  72.         jButton3.addActionListener(new java.awt.event.ActionListener() {
  73.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  74.                 jButton3ActionPerformed(evt);
  75.             }
  76.         });
  77.  
  78.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  79.         getContentPane().setLayout(layout);
  80.         layout.setHorizontalGroup(
  81.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  82.             .addGroup(layout.createSequentialGroup()
  83.                 .addGap(34, 34, 34)
  84.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 356, javax.swing.GroupLayout.PREFERRED_SIZE)
  85.                 .addGap(45, 45, 45)
  86.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  87.                     .addGroup(layout.createSequentialGroup()
  88.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  89.                             .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
  90.                             .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
  91.                         .addGap(18, 18, 18)
  92.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  93.                             .addComponent(jButton2)
  94.                             .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  95.                                 .addComponent(jTextField1)
  96.                                 .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE))))
  97.                     .addGroup(layout.createSequentialGroup()
  98.                         .addGap(20, 20, 20)
  99.                         .addComponent(jButton1)
  100.                         .addGap(64, 64, 64)
  101.                         .addComponent(jButton3)))
  102.                 .addContainerGap(38, Short.MAX_VALUE))
  103.         );
  104.         layout.setVerticalGroup(
  105.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  106.             .addGroup(layout.createSequentialGroup()
  107.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  108.                     .addGroup(layout.createSequentialGroup()
  109.                         .addGap(71, 71, 71)
  110.                         .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 337, javax.swing.GroupLayout.PREFERRED_SIZE))
  111.                     .addGroup(layout.createSequentialGroup()
  112.                         .addGap(81, 81, 81)
  113.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  114.                             .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
  115.                             .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
  116.                         .addGap(89, 89, 89)
  117.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  118.                             .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
  119.                             .addComponent(jLabel1))
  120.                         .addGap(41, 41, 41)
  121.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  122.                             .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
  123.                             .addComponent(jLabel2))
  124.                         .addGap(48, 48, 48)
  125.                         .addComponent(jButton2)))
  126.                 .addContainerGap(58, Short.MAX_VALUE))
  127.         );
  128.  
  129.         pack();
  130.     }// </editor-fold>                        
  131.  
  132.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  133.         // TODO add your handling code here:
  134.         this.getData();
  135.     }                                        
  136.  
  137.     private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  138.         // TODO add your handling code here:
  139.         jTextArea1.setText("");
  140.     }                                        
  141.  
  142.     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  143.         // TODO add your handling code here:
  144.         this.setData();
  145.     }                                        
  146.  
  147.     /**
  148.      * @param args the command line arguments
  149.      */
  150.     public static void main(String args[]) {
  151.         /* Set the Nimbus look and feel */
  152.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  153.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  154.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  155.          */
  156.         try {
  157.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  158.                 if ("Nimbus".equals(info.getName())) {
  159.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  160.                     break;
  161.                 }
  162.             }
  163.         } catch (ClassNotFoundException ex) {
  164.             java.util.logging.Logger.getLogger(DbFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  165.         } catch (InstantiationException ex) {
  166.             java.util.logging.Logger.getLogger(DbFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  167.         } catch (IllegalAccessException ex) {
  168.             java.util.logging.Logger.getLogger(DbFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  169.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  170.             java.util.logging.Logger.getLogger(DbFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  171.         }
  172.         //</editor-fold>
  173.  
  174.         /* Create and display the form */
  175.         java.awt.EventQueue.invokeLater(new Runnable() {
  176.             public void run() {
  177.                 new DbFrame().setVisible(true);
  178.             }
  179.         });
  180.     }
  181.    
  182.     public void getData()
  183.     {
  184.         try
  185.         {
  186.             String q1 = "select * from worker";
  187.             rs = st.executeQuery(q1);
  188.             while(rs.next())
  189.             {
  190.                 jTextArea1.setText(jTextArea1.getText() +"id :"+rs.getInt("id")+"\n"+"name :"+rs.getString("name")+"\n"+"salary :"+rs.getFloat("sal")+"\n\n");
  191.             }
  192.            
  193.         }
  194.         catch(Exception e)
  195.         {
  196.             System.out.println(e);
  197.         }
  198.     }
  199.    
  200.     public void setData()
  201.     {
  202.         try
  203.         {
  204.             String q2 = "insert into worker(name,sal) values(?,?)";
  205.             //float val = Float.parseFloat(jTextField2.getText());
  206.             pst = con.prepareStatement(q2);
  207.             pst.setString(1, jTextField1.getText());
  208.             pst.setString(2, jTextField2.getText());
  209.             pst.executeUpdate();
  210.         }
  211.         catch(Exception e)
  212.         {
  213.             System.out.println(e);
  214.         }
  215.     }
  216.  
  217.     // Variables declaration - do not modify                    
  218.     private javax.swing.JButton jButton1;
  219.     private javax.swing.JButton jButton2;
  220.     private javax.swing.JButton jButton3;
  221.     private javax.swing.JLabel jLabel1;
  222.     private javax.swing.JLabel jLabel2;
  223.     private javax.swing.JScrollPane jScrollPane1;
  224.     private javax.swing.JTextArea jTextArea1;
  225.     private javax.swing.JTextField jTextField1;
  226.     private javax.swing.JTextField jTextField2;
  227.     // End of variables declaration                  
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement