无法在 Java 桌面应用程序上通过 JDBC 连接到 Oracle DB
我试图创建一个非常简单的应用程序,在加载主窗口时程序连接到数据库。
如果我评论与数据库的连接部分,它工作正常。该应用程序没有显示任何错误,也没有抛出异常,所以我无法弄清楚出了什么问题。我发布了我的代码:
public final class Database {
private final String dbURL = "jdbc:oracle:thin:@" + dbHost + ":" + dbPort + ":" + dbService;
private final String dbDriver = "oracle.jdbc.driver.OracleDriver";
private Connection connection = null;
public Database() {
try {
System.out.println("Aqui 1");
Class.forName(dbDriver);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void connect() {
System.out.println("Dentro");
try {
connection = DriverManager.getConnection(dbURL, dbUser, dbPass);
} catch (SQLException ex) {
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void disconnect() {
try {
connection.close();
} catch (SQLException ex) {
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void numeroErrores(String dependencia){
System.out.println("Hola");
connect();
disconnect();
System.out.print("Adios");
}
}
dbUser、dbPass、dbHost、dbPort、dbService 设置为正确的值,我已经检查并重新检查了它们,而且我确信我已经添加了 oracle-jdbc 的正确 jar 文件。当调用 connect()
方法时,它有点挂起(我说是因为没有错误,除了我放在那里的 System.out.println 之外什么也没有显示)。
有什么想法吗?
I was trying to create a very simple app where at loading the main window the program connects to the DB.
If I comment the part of the connection to the DB it works fine. The app shows no error, throws no exception, so I cannot figure out what's wrong. I post my code:
public final class Database {
private final String dbURL = "jdbc:oracle:thin:@" + dbHost + ":" + dbPort + ":" + dbService;
private final String dbDriver = "oracle.jdbc.driver.OracleDriver";
private Connection connection = null;
public Database() {
try {
System.out.println("Aqui 1");
Class.forName(dbDriver);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void connect() {
System.out.println("Dentro");
try {
connection = DriverManager.getConnection(dbURL, dbUser, dbPass);
} catch (SQLException ex) {
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void disconnect() {
try {
connection.close();
} catch (SQLException ex) {
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void numeroErrores(String dependencia){
System.out.println("Hola");
connect();
disconnect();
System.out.print("Adios");
}
}
the dbUser, dbPass, dbHost, dbPort, dbService are set to their correct value, I've checked and rechecked them, also I am sure I've added the correct jar file of the oracle-jdbc. It kind of hangs (I say kind of 'cause no error no nothing is shown but the System.out.println I put there) when calling the connect()
method.
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有足够的调试打印或 catch 语句
尝试捕获一般异常和 Throwable,看看是否会添加数据
如果您没有调试器,请在每一行添加调试打印,以便您确切地知道自己在哪里
如果您有 ojdbc6.jar,请尝试将其添加到 DriverManager.getConnection 之前:
只是为了确保
There are not enough debug prints or catch statements
Try catching both general Exception and Throwable, see if that adds data
If you dont have a debugger, add debug prints on every line so you know exactly where you are
If you have the ojdbc6.jar, try adding this right before DriverManager.getConnection:
just to make sure