1

I am trying to connect my JSF application to my Oracle 11g database. A while back I had a JSF application connecting a similar way with a Derby database. Around the same time I was able to connect to an Oracle 11g database via a Java program I wrote.

I've tried to port the code into this recent project, and while everything looks correct, my connection in the code is returning null.

To further this issue, NetBeans seems to lock up when I try to debug. I'm assuming it goes to use the port that is running GlassFish 4, but somehow can't tell its occupied.

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

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;
    }
11
  • Do you get an error? Commented Jan 14, 2015 at 17:16
  • Please post tnsnames.ora Commented Jan 14, 2015 at 17:16
  • Don't initialize your instance variables in two places. Either in the declarations or in the OracleBean constructor, not both. Also there are two OracleDriver classes in the jar, one in oracle.jdbc and one in oracle.jdbc.driver - I'm pretty sure you want the first one instead of the one you're using. Commented Jan 14, 2015 at 17:28
  • @A.L I can't seem to debug without glassfish locking up, so I'm not sure where to look for the error. As you can see in the code snippet, if the connect object is null, then it returns "SQL Error" to the webpage, that's as much as an indication that I have at the moment. Commented Jan 14, 2015 at 18:03
  • @RomanC I'm not sure what you mean. Commented Jan 14, 2015 at 18:04

3 Answers 3

1

As mentioned above - are you sure the db is running on port 8081 and not 1521 as standard. Also is the dbname Test or XE as you have in the url. I suspect it's xe if you have the standard setup.

The following should get you a connection assuming you have the oracle driver jar as an external library

connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "your_user", "your_password");
Sign up to request clarification or add additional context in comments.

1 Comment

I forgot about that, when I got the stand alone to connect to the db, it was on port 1521, but I'm still having trouble getting the JSF to connect with the same syntax.
0

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

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

use

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

2 Comments

so in the end it was this and the connection port?
It was the .jar file being properly referenced, the port, the url and using Netbeans / glassfish properly to deploy the project.
0

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.