Advertisement
techsharif

java

Jan 25th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 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. package practise;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import javax.swing.*;
  10. import java.sql.*;
  11.  
  12. public class Test extends JFrame implements ActionListener{
  13.     JFrame jf;
  14.     JPanel jp;
  15.     JTextField jt;
  16.     JButton[] jb;
  17.     JLabel[] jl;
  18.  
  19.     public Test() {
  20.         jf=new JFrame();
  21.         jp=new JPanel();
  22.         jt= new JTextField(40);
  23.         jb =new JButton[10];
  24.         jl=new JLabel[10];
  25.         jb[0]=new JButton("Submit");
  26.         jf.add(jp);
  27.         jp.add(jt);
  28.         jp.add(jb[0]);
  29.         jb[0].addActionListener(this);
  30.         jf.setSize(1000, 500);
  31.         jf.setVisible(true);
  32.                
  33.     }
  34.  
  35.     @Override
  36.     public void actionPerformed(ActionEvent ae) {
  37.         try{
  38.         Connection  c=DriverManager.getConnection("jdbc:mysql://localhost/empinfo","root","root");
  39.         PreparedStatement ps = c.prepareStatement("insert into experiment value(?,?)");
  40.         ps.setString(1, null);
  41.         ps.setString(2, jt.getText());
  42.         ps.executeUpdate();
  43.         display();
  44.         JOptionPane.showConfirmDialog(rootPane, "inserted");
  45.         }catch(SQLException e){
  46.             JOptionPane.showMessageDialog(rootPane, e);
  47.         }
  48.     }
  49.    
  50.     public void display(){
  51.         try{
  52.             Connection  c=DriverManager.getConnection("jdbc:mysql://localhost/empinfo","root","root");
  53.             Statement st = c.createStatement();
  54.             ResultSet rs=st.executeQuery("select * from experiment");
  55.             int i=1;
  56.             while(rs.next()){
  57.                 jl[i]=new JLabel();
  58.                 jl[i].setText(rs.getString(2));
  59.                 jp.add(jl[i]);
  60.                 jb[i]=new JButton("edit");
  61.                 jb[i+1]=new JButton("delete");
  62.                 jp.add(jb[i]);
  63.                 jp.add(jb[i+1]);
  64.                 i++;
  65.             }
  66.         }catch(SQLException e){
  67.             JOptionPane.showMessageDialog(rootPane, e);
  68.         }
  69.     }
  70.    
  71.     public static void main(String[] args){
  72.         Test t = new Test();
  73.     }
  74.    
  75.    
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement