我收到错误,然后出现异常错误

时间:2017-04-25 04:44:01

标签: java mysql

所以我是初学者编程,我正在使用java练习数据库实现。这是我用过的代码(道歉,如果它太长了)

       public DBTest(String title){
            super(title);
            setLayout(new GridLayout(6,2));
            lbName=new JLabel("Name");
            lbAddress=new JLabel("Address");
            lbRoll=new JLabel("Roll No.");
            txtName=new JTextField("30");
            txtAddress=new JTextField("30");
            txtRoll=new JTextField("30");
            btnInsert=new JButton("INSERT");
            btnUpdate=new JButton("UPDATE");
            btnDelete=new JButton("DELETE");
            btnSelect=new JButton("SELECT");
            btnCancel=new JButton("Cancel");
            try{
                Class.forName("com.mysql.jdbc.Driver");
                con=DriverManager.getConnection("jdbc:mysql//localhost:3306/std");
            }
            catch(Exception ex){
                System.out.println("Error: "+ex.getMessage());
            }
            EventHandler handler=new EventHandler();
            add(lbName);add(txtName);
            add(lbRoll);add(txtRoll);
            add(lbAddress);add(txtAddress);
            add(btnInsert);add(btnSelect);
            add(btnDelete);add(btnUpdate);
            add(btnCancel);
            btnInsert.addActionListener(handler);
            btnUpdate.addActionListener(handler);
            btnDelete.addActionListener(handler);
            btnSelect.addActionListener(handler);
            btnCancel.addActionListener(handler);
       }

       private class EventHandler implements ActionListener{
           public void actionPerformed(ActionEvent ae){
               if(ae.getSource()==btnSelect)
                   onSelect();
               else if(ae.getSource()==btnUpdate)
                    onUpdate();
               else if(ae.getSource()==btnDelete)
                    onDelete();
               else if(ae.getSource()==btnInsert)
                   onInsert();
               else
                       System.exit(0);
               try{
                    con.close( );
               }catch(SQLException ex){ }
           }
       }

       public void onSelect(){
        try{
           ps=con.prepareStatement("Select * From student Where Roll=? ");
           ps.setInt(1,Integer.parseInt(txtRoll.getText()));
           rs=ps.executeQuery();
           txtName.setText(rs.getString("Name"));
           txtAddress.setText(rs.getString("Address"));    
        }catch(SQLException ex){ 
            System.out.println("Error: "+ex.getMessage());}
       }

        public void onInsert(){
        try{
           ps=con.prepareStatement("Insert Into Student Values(?,?,?)");
           ps.setInt(1,Integer.parseInt(txtRoll.getText()));
           ps.setString(2,txtName.getText());
           ps.setString(3,txtAddress.getText());
           rs=ps.executeQuery();    
        }catch(SQLException ex){ 
            System.out.println("Error: "+ex.getMessage());
        }
       }

        public void onDelete(){
        try{
           ps=con.prepareStatement("Delete From Student Where Roll=?");
           ps.setInt(1,Integer.parseInt(txtRoll.getText()));
           rs=ps.executeQuery();    
        }catch(SQLException ex){ 
            System.out.println("Error: "+ex.getMessage());}
       }

         public void onUpdate(){
        try{
            if(txtName.getText()!=null){
                ps=con.prepareStatement("Update Stident Set Name=? Where Roll=?");
                ps.setString(1,txtName.getText());
                ps.setInt(2,Integer.parseInt(txtRoll.getText()));
                rs=ps.executeQuery(); 
            }   
            if(txtAddress.getText()!=null){
                 ps.setInt(2,Integer.parseInt(txtRoll.getText()));
                ps.setString(2,txtAddress.getText());
                rs=ps.executeQuery();  
            }   
        }catch(SQLException ex){ 
            System.out.println("Error: "+ex.getMessage());}
       }
    }

所以,我有两个问题:error:Error: No suitable driver found for jdbc:mysql//localhost:3306/std 但代码仍在运行。然后它显示了一个java.lang.NullPointerException以及其他一堆错误 在DBTest.DBTest.onSelect(DBTest.java:77) 在DBTest.DBTest $ EventHandler.actionPerformed(DBTest.java:60).... 一旦我在任何文本字段上输入了一个值,然后按下除取消之外的任何按钮。

是的,我已经在库中加载了MySqlDriver。

0 个答案:

没有答案