java错误(找不到类)

时间:2011-02-27 13:17:47

标签: java exception

我的代码是:

    String baglantiURL="jdbc:postgresql://localhost:5432/Test";
    String surucu="org.postgresql.Driver";
    try{

        Class.forName(surucu);
        Connection baglanti=DriverManager.getConnection(baglantiURL);
        Statement ifade=baglanti.createStatement();
        String sorgu="select * from tablo";
        ResultSet sonucKumesi=ifade.executeQuery(sorgu);
        while (sonucKumesi.next()) {System.out.println(sonucKumesi.getString(1));


        System.out.println(sonucKumesi.getString(2));
        System.out.println(sonucKumesi.getString(3));

        }
    }
    catch (ClassNotFoundException e) {
        System.out.println("Class not found");
    }
    catch (SQLException e) {
        System.out.println("SQL error");
    }
    catch (Exception e) {
                 System.out.println("hata");
    }
}

输出是:

  

SQL错误

有什么不对?

1 个答案:

答案 0 :(得分:3)

您的部分问题在以下代码段中:

catch (ClassNotFoundException e) {
    System.out.println("Class not found");
}
catch (SQLException e) {
    System.out.println("SQL error");
}
catch (Exception e) {
    System.out.println("hata");
}

您的代码丢弃了大部分可以告诉您应用程序问题的信息。在每次println次调用之后,添加一行以打印出堆栈跟踪; e.g。

    e.printStackTrace(System.out);