Reputation: 153
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;
}
Upvotes: 1
Views: 3339
Reputation: 153
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;
}
Upvotes: 0
Reputation: 995
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");
Upvotes: 1
Reputation: 1
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);
Upvotes: 0