不好意思错误报告写错了,应该时:
jdataframe.java:123: unreported exception java.lang.ClassNotFoundException; must
 be caught or declared to be thrown
                                Class.forName(driver);
                                             ^
jdataframe.java:124: unreported exception java.sql.SQLException; must be caught
or declared to be thrown
                                Connection cn1=DriverManager.getConnection(url,"
sa","");
                                                                          ^
jdataframe.java:125: unreported exception java.sql.SQLException; must be caught
or declared to be thrown
                                Statement stmt1=cn1.createStatement();
                                                                   ^
jdataframe.java:128: unreported exception java.sql.SQLException; must be caught
or declared to be thrown
                                stmt1.executeUpdate(SQL1);
                                                   ^
jdataframe.java:129: unreported exception java.sql.SQLException; must be caught
or declared to be thrown
                                cn1.close();
                                         ^

解决方案 »

  1.   

    加一个try catch 要捕获异常
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;public class JDataFrame extends JFrame {
        
        /**
         * The constructor.
         */ 
         static String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver"; 
         static String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=northwind";
         JTextField txtComName=null;
         JTextField txtAdr=null;
         JTextField txtCity=null;
         String strId=null;     public JDataFrame() {
    JLabel labcomname=new JLabel();  
            labcomname.setSize(40,4);
            JLabel labadr=new JLabel();
            labadr.setSize(40,4);
            JLabel labcity=new JLabel();
            labcity.setSize(40,4);
            txtComName=new JTextField(20);
            txtComName.setSize(20,4);
            txtAdr=new JTextField(20);
            txtAdr.setSize(40,4);
            txtCity=new JTextField(20);
            txtCity.setSize(40,4);
            Container container=this.getContentPane();
            JPanel selPanel=new JPanel();
            selPanel.setLayout(new FlowLayout());
            selPanel.add(labcomname);
            selPanel.add(labadr);
            selPanel.add(labcity);
            selPanel.setSize(200,100);
            JPanel updatePanel=new JPanel();
            updatePanel.setLayout(new FlowLayout());
            updatePanel.add(txtComName);
            updatePanel.add(txtAdr);
            updatePanel.add(txtCity);
            updatePanel.setSize(200,100);
            JButton butOK=new JButton("OK");
            butOK.setSize(20,4);
            
            Container myFrm=this.getContentPane();
            myFrm.setLayout(new BorderLayout());
            myFrm.add(selPanel,BorderLayout.WEST);
            myFrm.add(updatePanel,BorderLayout.EAST);
            myFrm.add(butOK,BorderLayout.SOUTH);
            
            //read database
           
            try
            {
            Class.forName(driver);
        
            Connection cn=DriverManager.getConnection(url,"sa","");
            String SQL="Select CompanyName,Address,City From Customers";
            Statement stmt=cn.createStatement();
            ResultSet rs=stmt.executeQuery(SQL);
            if (rs.next())
            {
             labcomname.setText(rs.getString("CompanyName"));
             labadr.setText(rs.getString("Address"));
             labcity.setText(rs.getString("city"));
             strId=rs.getString("CustomerID");
            }
            rs.close();
            cn.close();
             }
             catch (Exception e)      
             {
               JOptionPane.showMessageDialog(null,e.getMessage());
             }
            this.setSize(400,240);
            this.setVisible(true);
           // container.setLayout(new BorderLayout());
            
         butOK.addActionListener(
         new ActionListener(){
         public void actionPerformed(ActionEvent event)
         {try{
        
         Class.forName(driver);
         Connection cn1=DriverManager.getConnection(url,"sa","");
         Statement stmt1=cn1.createStatement();
         String SQL1="Update Customers Set CompanyName='" + txtComName.getText() + "',Address='"
         + txtAdr.getText() + "',city='" + txtCity.getText() + "' Where CustomerID='" + strId + "'";
         stmt1.executeUpdate(SQL1);
         cn1.close();
         }
         catch(Exception e){}
         }
         }
         );    
        }}
      

  2.   

    请问tom2005(快乐着)为什么加一个try catch 要捕获异常就没问题了,难道用jdbc时必须加try catch 吗,谢谢
      

  3.   

    unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown如果再遇到,就try{ } catch{ } .... 
    Class  Not  Found  Exception  ....  Are You understand?
      

  4.   

    Must Catch Your Operation!
      

  5.   

    mport java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
    import javax.swing.*;public class SqlTest
    {
    public static void main(String[] args)
    {
    JFrame frame = new JDataFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.show();
    }
    }
    class JDataFrame extends JFrame 
    {
        
        /**
         * The constructor.
         */ 
         static String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver"; 
         static String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=northwind";
         JTextField txtComName=null;
         JTextField txtAdr=null;
         JTextField txtCity=null;
         String strId=null;     public JDataFrame() 
         {
         
            JLabel labcomname=new JLabel();//customernamelabel  
            labcomname.setSize(40,4);
            JLabel labadr=new JLabel();//customeraddress
            labadr.setSize(40,4);
            JLabel labcity=new JLabel();//customercity
            labcity.setSize(40,4);
            txtComName=new JTextField(20);//companyname
            txtComName.setSize(20,4);
            txtAdr=new JTextField(20);
            txtAdr.setSize(40,4);
            txtCity=new JTextField(20);
            txtCity.setSize(40,4);
            Container container=this.getContentPane();
            JPanel selPanel=new JPanel();
            selPanel.setLayout(new GridLayout(3,1));
            selPanel.add(labcomname);
            selPanel.add(labadr);
            selPanel.add(labcity);
            selPanel.setSize(200,100);
            JPanel updatePanel=new JPanel();
            updatePanel.setLayout(new FlowLayout());
            updatePanel.add(txtComName);
            updatePanel.add(txtAdr);
            updatePanel.add(txtCity);
            updatePanel.setSize(200,100);
            JButton butOK=new JButton("OK");
            butOK.setSize(20,4);
            
            Container myFrm=this.getContentPane();
            myFrm.setLayout(new BorderLayout());
            myFrm.add(selPanel,BorderLayout.NORTH);
            myFrm.add(updatePanel,BorderLayout.CENTER);
            myFrm.add(butOK,BorderLayout.SOUTH);
            
            //read database
           
            try
            {
            Class.forName(driver);
        
            Connection cn=DriverManager.getConnection(url,"sa","king");
            String SQL="Select CompanyName,Address,City,CustomerID From Customers";
            Statement stmt=cn.createStatement();
            ResultSet rs=stmt.executeQuery(SQL);
            if (rs.next())
            {
             labcomname.setText("companyname:" + rs.getString("CompanyName") + "|");
             labadr.setText("companyaddress:" + rs.getString("Address") + "|");
             labcity.setText("companycity:"+ rs.getString("city"));
             strId=rs.getString("CustomerID");
            }
            rs.close();
            cn.close();
             }
             catch (Exception e)      
             {
               JOptionPane.showMessageDialog(null,e.getMessage());
             }
            this.setSize(400,240);
            this.setVisible(true);
           // container.setLayout(new BorderLayout());
            
         butOK.addActionListener(
         new ActionListener(){
         public void actionPerformed(ActionEvent event)
         {
         Connection cn1=null;
         try
         {
        
         Class.forName(driver);
         cn1=DriverManager.getConnection(url,"sa","king");
         Statement stmt1=cn1.createStatement();
         String SQL1="Update Customers Set CompanyName='" + txtComName.getText() + "',Address='"
         + txtAdr.getText() + "',city='" + txtCity.getText() + "' Where CustomerID='" + strId + "'";
         stmt1.executeUpdate(SQL1);
         cn1.close();
         }
         catch(SQLException se)
         {
         System.out.println("SQL occur exception");
         }
         catch(ClassNotFoundException snfe)
         {
         System.out.println("Class not found");
         }
         finally
         {
        
         }
         }
         }
         );    
        }
    //已经编译通过了,java对安全性要求特别高,对可能出现的异常一定要进行处理,否则,编译器会报错