在“私有类删除实现ActionListener”中获取异常“java.lang.ClassNotFoundException:com.mysql.jdbc.driver”

时间:2011-08-09 05:45:29

标签: java mysql

import java.awt.Panel;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.sql.DriverManager;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class first {

    JButton b1= new JButton("save"),b2=new JButton("delete");
    JFrame frame1 = new JFrame("Ghar Hisab");
    Panel panel = new Panel();
    JTextField text1 = new JTextField(20);
    JTextField text2 = new JTextField(20);

    JLabel label1= new JLabel("FIRST NAME");
    JLabel label2= new JLabel("LAST NAME");
    int people;
    add l = new add();
    delete d = new delete();
            public void frame(){
                panel.add(label1);
                panel.add(text1);
                panel.add(label2); panel.add(text2); panel.add(b1); panel.add(b2);

                frame1.getContentPane().add(panel);
                frame1.setSize(800,800);
                frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame1.setVisible(true);
            //  people=Integer.parseInt(JOptionPane.showInputDialog("Enter the number of people")); 
                }

            public void name(){

                    b1.addActionListener(l);
                    b2.addActionListener(d);



            }   


        private class add implements ActionListener {



        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            try{
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/people","root","desire");
            Statement stat= (Statement) con.createStatement();
            String s1=text1.getText();
            String s2=text2.getText();
            stat.executeUpdate("insert into name(first,last)values('"+s1+"','"+s2+"')");
            text1.setText(null);
            text2.setText(null);


            }

            catch(Exception e1){
                System.out.println(e1);
            }
        }




        }

        private class delete implements ActionListener{

            @Override
            public void actionPerformed(ActionEvent event) {
                // TODO Auto-generated method stub
                try{
                    Class.forName("com.mysql.jdbc.driver");
                    Connection con1 = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/people","root","desire");
                    Statement stat= (Statement) con1.createStatement();
                    String s1=text1.getText();
                    String s2=text2.getText();
                    stat.executeUpdate("DELETE FROM 'people'.'name' WHERE 'name'.'first' ='"+s1+"' AND 'name'.'last'='"+s2+"'LIMIT=1");
                    text1.setText(null);
                    text2.setText(null);



                }

                catch(Exception e2){ 
                    System.out.println("this is the xception "+ e2);
                }


            }

        }

}

2 个答案:

答案 0 :(得分:1)

您需要在项目中包含jdbc jar并将其添加到路径中。

答案 1 :(得分:1)

确保jdbc .jar文件位于类路径中。