String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=product";
    String username = "sa";
    String password = "";

    //定义驱动程序
String DBDRIVER="com.microsoft.jdbc.sqlserver.SQLServerDriver";

try
{
Class.forName(DBDRIVER);

}
catch(ClassNotFoundException ex)
{
ex.printStackTrace(System.err);
}
//获得连接
Connection conn=DriverManager.getConnection(url, username, password);
我的程序如上,可是最后一句,在eclipse中提示出错,说unhandle the type exception
请问是怎么回事?
谢谢

解决方案 »

  1.   

    获得Connection的语句也需要放在一个try..catch块里面,它会抛出一个受控异常SQLException
    改下:
    String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=product";
        String username = "sa";
        String password = "";

        //定义驱动程序
    String DBDRIVER="com.microsoft.jdbc.sqlserver.SQLServerDriver";

    try
    {
    Class.forName(DBDRIVER);
    Connection conn=DriverManager.getConnection(url, username, password);
    }
    catch(ClassNotFoundException ex)
    {
    ex.printStackTrace(System.err);
    }
      

  2.   

    多加一个
    catch(SQLException e){
      e.printStackTrace();
    }