无法在 Java 桌面应用程序上通过 JDBC 连接到 Oracle DB

发布于 2025-01-07 12:45:39 字数 1535 浏览 0 评论 0原文

我试图创建一个非常简单的应用程序,在加载主窗口时程序连接到数据库。

如果我评论与数据库的连接部分,它工作正常。该应用程序没有显示任何错误,也没有抛出异常,所以我无法弄清楚出了什么问题。我发布了我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花落人断肠 2025-01-14 12:45:39

没有足够的调试打印或 catch 语句
尝试捕获一般异常和 Throwable,看看是否会添加数据
如果您没有调试器,请在每一行添加调试打印,以便您确切地知道自己在哪里
如果您有 ojdbc6.jar,请尝试将其添加到 DriverManager.getConnection 之前:

DriverManager.registerDriver(new oracle.jdbc.OracleDriver());

只是为了确保

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:

DriverManager.registerDriver(new oracle.jdbc.OracleDriver());

just to make sure

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文