2014-10-06 138 views
-5
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();  } 
     } 
+1

对于Java 7,你可以简单地忽略的Class.forName()语句,因为它是不真的需要。 – Petro 2014-10-06 05:34:35

+1

你在这里有什么问题?如果问题是关于程序不编译,那么你所需要做的就是为** ClassNotFoundException添加一个catch块** – parth6 2014-10-06 05:37:28

+0

编译错误说明了问题所在。你的问题是什么? – 2014-10-06 05:59:52

回答

1

Class.forName(String)抛出一个ClassNotFoundException,如果它不能加载谁的名字传递给它(例如类,如果你在你的类路径中缺少一个jar)。这是一个检查的异常,并且必须被向上抛出(例如添加throws ClassNotFoundException到您的方法的声明),或抓住:

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"); 
} 
+0

我想要从数据库tble行,但它是没有工作。在运行程序控制台窗口后显示messege“无法加载JDBC驱动程序“ – 2014-10-06 07:57:37

相关问题