Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未报告的异常类NotFoundException;必须是catch或抛出的class.forName(sun.jdbc.odbc.JdbcOdbcDriver)_Java - Fatal编程技术网

Java 未报告的异常类NotFoundException;必须是catch或抛出的class.forName(sun.jdbc.odbc.JdbcOdbcDriver)

Java 未报告的异常类NotFoundException;必须是catch或抛出的class.forName(sun.jdbc.odbc.JdbcOdbcDriver),java,Java,如果Class.forNameString无法加载传递给它的名称的类(例如,如果类路径中缺少一个jar),它将抛出ClassNotFoundException。这是一个已检查的异常,必须向上抛出,例如,将抛出ClassNotFoundException添加到方法的声明中,或者捕获: import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; class Delet

如果Class.forNameString无法加载传递给它的名称的类(例如,如果类路径中缺少一个jar),它将抛出ClassNotFoundException。这是一个已检查的异常,必须向上抛出,例如,将抛出ClassNotFoundException添加到方法的声明中,或者捕获:

 import   java.sql.*; 
import   java.awt.*; 
import   java.awt.event.*;
import   javax.swing.*;
 class DeleteDemo extends Frame implements
     ActionListener {
                        Label l1;
                       Button b1;
                       TextField t1;
                       DeleteDemo()
                        {   
                         setVisible(true);
                         setSize(400,400);
                         l1=new  Label("Enter Id");
                         b1=new Button("Delete");
                         t1=new TextField(20);
                         setLayout(new FlowLayout());
                         add(l1);add(t1);
                         add(b1);
                         b1.addActionListener(this);
                        } 


         public void actionPerformed(ActionEvent  e)
         {
                           if(e.getSource()==b1)
                          {      
                              try
                              {         
                                      System.out.println("Data try to delete");
                                      String id=t1.getText(); 
                                      l1.setText("hi");
                                      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        Connection con=DriverManager.getConnection("Jdbc:Odbc:xyz","","");
                Statement st=con.createStatement();
                st.executeUpdate("delete from tblEmp where id ='"+id+"' ");
                System.out.println("Data has been deleted");
            }catch(SQLException eb){} 
         }
     }     
        public static void main(String args[ ])       {
            DeleteDemo dtl=new DeleteDemo();        } 
      }

对于Java 7,您可以简单地省略Class.forName语句,因为它实际上不是必需的?如果问题是关于程序没有编译,那么您需要做的就是为ClassNotFoundException再添加一个catch块编译错误说明问题所在。您的问题是什么?在控制台上运行时,显示的消息无法加载JDBC驱动程序我想从数据库tble加载一行,但它不起作用。运行程序控制台窗口后,显示messege无法加载JDBC驱动程序
try {
    System.out.println("Data try to delete");
    String id=t1.getText(); 
    l1.setText("hi");
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con=DriverManager.getConnection("Jdbc:Odbc:xyz","","");
    Statement st=con.createStatement();
    st.executeUpdate("delete from tblEmp where id ='"+id+"' ");
    System.out.println("Data has been deleted");
} catch(ClassNotFoundException cne) {
    System.err.println("Could not load JDBC driver");
} catch(SQLException eb) {
    System.err.println("Could not delete data");
}