对SQL SERVER执行数据操作我得到了异常java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver

时间:2014-08-26 17:00:47

标签: java jdbc

我在sql server中创建了表并为其分配了DNS。我使用窗口8和jdk 1.7.0。 我的程序编译成功但是当我尝试对SQL SERVER进行数据操作时,我得到了一个异常

java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver

这是我的java代码:

 public void actionPerformed(ActionEvent ae)
    {
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con=DriverManager.getConnection("jdbc:odbc:MyDNS","","");
            Object obj=ae.getSource();
            if(obj==bins)
            {
                PreparedStatement pstat=con.prepareStatement("insert toys values(?,?,?)");
                pstat.setString(1,tid.getText());
                pstat.setString(2,tname.getText());
                pstat.setDouble(3,Double.parseDouble(trate.getText()));
                pstat.executeUpdate();
                JOptionPane.showMessageDialog(this,"Record Inserted");
            }
            else if(obj==bdel)
            {
                PreparedStatement pstat=con.prepareStatement("delete toys where ctoyid=?");
                pstat.setString(1,tid.getText());
                pstat.executeUpdate();
                JOptionPane.showMessageDialog(this,"Record Deleted");
            }
            else if(obj==bupd)
            {
                PreparedStatement pstat=con.prepareStatement("update toys set vtoyname=?,mtoyrate=? where ctoyid=?");
                pstat.setString(1,tname.getText());
                pstat.setDouble(2,Double.parseDouble(trate.getText()));
                pstat.setString(3,tid.getText());
                pstat.executeUpdate();
                JOptionPane.showMessageDialog(this,"Record Updated");
            }
            else if(obj==bsearch)
            {
                PreparedStatement pstat=con.prepareStatement("select ctoyid,vtoyname,mtoyrate from toys where ctoyid=?");
                pstat.setString(1,tid.getText());
                ResultSet res=pstat.executeQuery();
                if(res.next()==true)
                {
                    tname.setText(res.getString(2));
                    trate.setText(res.getString(3));
                }
                else
                {
                    JOptionPane.showMessageDialog(this,"Invalid Toy ID");
                }
            }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }

帮我解决这个问题?为什么我有这个例外?

0 个答案:

没有答案