我正在使用JPA和Swing创建应用程序,它工作正常,但现在我想测试JPA是否与我的数据库连接,例如,如果我在JDBC_URL中添加了错误的IP,则无法在数据库中连接我想知道如何在异常发生时获得异常。有没有办法做到这一点?
答案 0 :(得分:1)
试试吧..
public class JdbcTest {
private final static String URL = "jdbc:oracle:...";
private final static String USER = "sa";
private final static String PASSWORd = "";
public static TestConnection() {
Connection c = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Driver found");
System.out.println("Connecting...");
c = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (Exception e) {
System.out.println(Cannot connect the database);
} finally {
if (c != null) {
c.close();
}
}
}
}