java.lang.Error: Unresolved compilation problems: 
Unhandled exception type java.lang.IllegalAccessException
Unhandled exception type java.lang.InstantiationException
Unhandled exception type java.lang.ClassNotFoundException
Unhandled exception type java.sql.SQLException
Unhandled exception type java.sql.SQLException
Unhandled exception type java.sql.SQLException
Unhandled exception type java.sql.SQLException
Unhandled exception type java.sql.SQLException
Unhandled exception type java.sql.SQLException
Unhandled exception type java.sql.SQLException at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:274)
at tst.tst.main(tst.java:23)
Exception in thread "main"

解决方案 »

  1.   

    为何用第1种方法出错而用第2种方法正常?(第2种方法只多了个异常捕获语句。) public void use1()
    {
    {
    Class.forName("org.gjt.mm.mysql.Driver").newInstance();
    Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=gb2312");
    Statement stmt=conn.createStatement();
    ResultSet rs=stmt.executeQuery("SELECT * from t1");
    while(rs.next())
    System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));
    rs.close();
    stmt.close();
    conn.close();
    }
    } public void use2()
    {
    try{
    Class.forName("org.gjt.mm.mysql.Driver").newInstance();
    Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=gb2312");//8859_1");
    Statement stmt=conn.createStatement();
    ResultSet rs=stmt.executeQuery("SELECT * from t1");
    while(rs.next())
    System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));
    rs.close();
    stmt.close();
    conn.close();
    }
    catch(Exception e)
    {
    System.out.println("error:"+e);
    }
    }
      

  2.   

    你那些好像是
    编译时的提示,
    对不对?javac -nowarn不过,不能算是个好方法。
      

  3.   

    “通常我们在写与数据库连接时一定要加上异常捕获,否则是不能通过编译的。”我以前用C++不是这样的,在JAVA中一定要进行异常捕获吗?