Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package practise;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- import java.sql.*;
- public class Test extends JFrame implements ActionListener{
- JFrame jf;
- JPanel jp;
- JTextField jt;
- JButton[] jb;
- JLabel[] jl;
- public Test() {
- jf=new JFrame();
- jp=new JPanel();
- jt= new JTextField(40);
- jb =new JButton[10];
- jl=new JLabel[10];
- jb[0]=new JButton("Submit");
- jf.add(jp);
- jp.add(jt);
- jp.add(jb[0]);
- jb[0].addActionListener(this);
- jf.setSize(1000, 500);
- jf.setVisible(true);
- }
- @Override
- public void actionPerformed(ActionEvent ae) {
- try{
- Connection c=DriverManager.getConnection("jdbc:mysql://localhost/empinfo","root","root");
- PreparedStatement ps = c.prepareStatement("insert into experiment value(?,?)");
- ps.setString(1, null);
- ps.setString(2, jt.getText());
- ps.executeUpdate();
- display();
- JOptionPane.showConfirmDialog(rootPane, "inserted");
- }catch(SQLException e){
- JOptionPane.showMessageDialog(rootPane, e);
- }
- }
- public void display(){
- try{
- Connection c=DriverManager.getConnection("jdbc:mysql://localhost/empinfo","root","root");
- Statement st = c.createStatement();
- ResultSet rs=st.executeQuery("select * from experiment");
- int i=1;
- while(rs.next()){
- jl[i]=new JLabel();
- jl[i].setText(rs.getString(2));
- jp.add(jl[i]);
- jb[i]=new JButton("edit");
- jb[i+1]=new JButton("delete");
- jp.add(jb[i]);
- jp.add(jb[i+1]);
- i++;
- }
- }catch(SQLException e){
- JOptionPane.showMessageDialog(rootPane, e);
- }
- }
- public static void main(String[] args){
- Test t = new Test();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement