无法通过JDBC连接到Oracle 11g

时间:2015-01-14 17:14:54

标签: java oracle jdbc oracle11g

我正在尝试将我的JSF应用程序连接到Oracle 11g数据库。前段时间我有一个JSF应用程序连接类似的方式与Derby数据库。大约在同一时间,我能够通过我编写的Java程序连接到Oracle 11g数据库。

我试图将代码移植到这个最近的项目中,虽然一切看起来都正确,但我在代码中的连接返回null。

为了解决这个问题,当我尝试调试时,NetBeans似乎锁定了。我假设它使用运行GlassFish 4的端口,但不知何故无法告诉它被占用。

感谢任何帮助;如果我能提供一些我忽略的信息,请告诉我。

代码段如下:

@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";
        }

//结束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;
    }

3 个答案:

答案 0 :(得分:1)

如上所述 - 您确定数据库是在端口8081而不是1521上运行的。也就是你在url中的dbname Test或XE。如果您有标准设置,我怀疑它是xe。

假设您将oracle驱动程序jar作为外部库

,以下内容应该为您提供连接
connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "your_user", "your_password");

答案 1 :(得分:0)

我认为你的错误是你连接url和dbname,它应该只是url,因为你连接到实例xe而不是xeTest 而不是

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

使用

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

答案 2 :(得分:0)

经过多次测试并强制部署应用程序几次(虽然它会显示编码更改但没有这个),以下语法最终起作用:

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);

//结束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;
}