简体   繁体   English

无法通过JDBC连接到Oracle 11g

[英]Can't connect to Oracle 11g via JDBC

I am trying to connect my JSF application to my Oracle 11g database. 我正在尝试将JSF应用程序连接到我的Oracle 11g数据库。 A while back I had a JSF application connecting a similar way with a Derby database. 前一段时间,我有一个JSF应用程序以类似的方式将其与Derby数据库连接。 Around the same time I was able to connect to an Oracle 11g database via a Java program I wrote. 大约在同一时间,我能够通过编写的Java程序连接到Oracle 11g数据库。

I've tried to port the code into this recent project, and while everything looks correct, my connection in the code is returning null. 我试图将代码移植到这个最近的项目中,尽管一切看起来正确,但我在代码中的连接返回null。

To further this issue, NetBeans seems to lock up when I try to debug. 为了进一步解决此问题,当我尝试调试时,NetBeans似乎锁定了。 I'm assuming it goes to use the port that is running GlassFish 4, but somehow can't tell its occupied. 我假设它将使用运行GlassFish 4的端口,但不知何故无法得知其已被占用。

Any help is appreciated; 任何帮助表示赞赏; please let me know if I can provide further information that I somehow overlooked. 请让我知道是否可以提供我以某种方式被忽略的更多信息。

Code snippets as follows: 代码片段如下:

@ManagedBean(name="OracleBean")
@RequestScoped
public class OracleBean {

    private String driver = "oracle.jdbc.driver.OracleDriver";
    private String url = "jdbc:oracle:thin:@localhost:8081:xe";
    private String dbName = "test";
    private String dbUsername = "Username";
    private String dbPassword = "password";
    private Connection connect = null;

    private OracleMethods Method;

    /**
     * Creates a new instance of DataBean
     */
    public OracleBean() {
        driver = "oracle.jdbc.driver.OracleDriver";
        url = "jdbc:oracle:thin:@localhost:8081:xe";
        dbName = "Test";
        dbUsername = "Username";
        dbPassword = "password";
        connect = null;

        Method = new OracleMethods();
    }

    public String getColorData(String rowID, int col) {
        connect = Method.getConnection(driver, url, dbName, dbUsername, dbPassword);

        if (connect == null) {
            return "SQL Error";
        }

//end Bean code //结束Bean代码

public class OracleMethods extends JPanel {

    private Connection connect = null;

    public OracleMethods() {}

    public Connection getConnection(String driver, String url, String dbName, String dbUsername, String dbPassword) {
        try {
            Class.forName(driver).newInstance();
            connect = DriverManager.getConnection((url + dbName), dbUsername, dbPassword);
        } catch (Exception lex) {
            lex.printStackTrace();
        }
        return connect;
    }

As mentioned above - are you sure the db is running on port 8081 and not 1521 as standard. 如上所述-您确定数据库在标准端口8081而不是1521上运行。 Also is the dbname Test or XE as you have in the url. 也是url中的dbname Test或XE。 I suspect it's xe if you have the standard setup. 如果您具有标准设置,我怀疑是xe。

The following should get you a connection assuming you have the oracle driver jar as an external library 假设您已将oracle驱动程序jar作为外部库,则以下内容应为您建立连接

connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "your_user", "your_password");

I think that you your error is that you are concatenating url and dbname, and it should be just the url as you are connectiong to instance xe and not xeTest instead of 我认为您的错误是您将url和dbname串联在一起,并且应该仅是url,因为您是连接到实例xe而不是xeTest而不是

connect = DriverManager.getConnection((url+dbName), dbUsername, dbPassword);

use 采用

connect = DriverManager.getConnection((url), dbUsername, dbPassword);

After several tests and force deploying the application a few times (though it would show coding changes without this), the following syntax ended up working: 经过多次测试并强行部署了几次应用程序(尽管如果没有此操作,它将显示代码更改),以下语法最终起作用:

public OracleBean()
{
    driver = "oracle.jdbc.driver.OracleDriver";
    url = "jdbc:oracle:thin:@localhost:1521:xe";
    dbName = "Test";
    dbUsername = "username";
    dbPassword = "password";
    connect = null;

    Method = new OracleMethods();
}

public String getColorData(String rowID, int col)
{
    connect = Method.openConnection(driver, url, dbName, dbUsername, dbPassword);

//end bean code //结束bean代码

public Connection openConnection(String driver, String url, String dbName, String dbUsername, String dbPassword)
{
    try
    {
        Class.forName(driver);
    }
    catch (ClassNotFoundException e)
    {
            System.out.println("Could not load the driver");
    }

    try
    {
        connect = DriverManager.getConnection((url), dbUsername, dbPassword);
    }
    catch(Exception lex)
    {
        lex.printStackTrace();
    }

    return connect;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM