所有需要异常处理(public "returntype" methodName() throws XXXXException)的方法在调用时需要你进行异常处理。简单说:把getConnection()和createStatement()等等这些东西放到一个try{}catch(){}块中

解决方案 »

  1.   

    没有写错误扑获语句
    这是我以前的代码import java.sql.*;class db
    {
      String str;
      Connection Conn;
      Statement Stmt;
      ResultSet RS ;
      
      db(String sss)
      {
         try 
         {
           Class.forName("org.gjt.mm.mysql.Driver").newInstance(); 
           System.out.println("Load driver.");
           Conn = DriverManager.getConnection("jdbc:mysql://132.208.5.2/dbase?user=starnet");
           System.out.println("connect to");
                 
         }
         catch (Exception R) 
         {
           System.err.println("Unable to load driver.");
           R.printStackTrace();
         }
       }
       
       public ResultSet txt(String sql)
       {   
         try 
         {    
           Stmt = Conn.createStatement();
           RS = Stmt.executeQuery(sql);
           System.out.println(sql);
           return RS;
      
         }
         catch (SQLException E) 
         {
           System.out.println("SQLException: " + E.getMessage());
           System.out.println("SQLState:     " + E.getSQLState());
           System.out.println("VendorError:  " + E.getErrorCode());
           return RS;
         }
        
       }
       
       public void dbclose()
       {
         try
         {
           RS.close();
           Stmt.close();
           Conn.close();
         }
         catch (SQLException E) 
         {
           System.out.println("SQLException: " + E.getMessage());
           System.out.println("SQLState:     " + E.getSQLState());
           System.out.println("VendorError:  " + E.getErrorCode());
          
         }  
       }
      
    }
    还有
    try
         {
           String ssst = new String("select tmport.p_id from tmport,office where office.o_idin=tmport.jh and office.o_idin="+idin);
           ResultSet rs = ddd.txt(ssst);
           while (rs.next()) 
           {
          coport.addItem(rs.getString(1));
          System.out.println("port: "+rs.getString(1));
        }
        rs.close();
      }  
         catch (SQLException E) 
         {
             System.out.println("SQLException: " + E.getMessage());
             System.out.println("SQLState:     " + E.getSQLState());
             System.out.println("VendorError:  " + E.getErrorCode());
         }