954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java connection with Oracle 10g express

Hi,

I have an problem with connecting Oracle 10g express edition with java.

I am accessing my Oracle 10g with the following URL:
localhost:8080/apex

I am using the following code:

try
{
  Class.forName("oracle.jdbc.driver.OracleDriver");

  Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:8080:xe","hr","hr");
  System.out.println("Connected Successfully To Oracle");
  con.close();
}
catch(Exception ex)
{
  ex.printStackTrace();
}


Is the code I wrote correct????

If not please suggest me the correct code.

Thanks in advance.....

Rahul Das
Newbie Poster
5 posts since Mar 2011
Reputation Points: 26
Solved Threads: 0
 

I have got the solution and now its working fine......

Rahul Das
Newbie Poster
5 posts since Mar 2011
Reputation Points: 26
Solved Threads: 0
 

Would you mind to share it? You may possibly help somebody in the future with same/similar issue....

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 
Would you mind to share it? You may possibly help somebody in the future with same/similar issue....


Why Not!!
Here is my code which is working fine.

I am accessing tomcat with: localhost/8084
and oracle 10g with:localhost:8080/apex

Following is the java code:

try
        {
            // Load the JDBC driver
            String driverName = "oracle.jdbc.OracleDriver";
            Class.forName(driverName);

            // Create a connection to the database
            String serverName = "127.0.0.1";
            String portNumber = "1521";
            String sid = "XE";
            String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
            System.out.println(url);
            String username = "xxxxxxx";
            String password = "xxxxxxx";
            try{
            connection = DriverManager.getConnection(url, username, password);
            }
            catch(Exception e)
            {
                System.out.println("Connection Error: " + e);
            }
            String sql = "select * from test" ;
            Statement stmt = connection.createStatement();
            try
            {
                ResultSet rs = stmt.executeQuery(sql);

                while (rs.next())
                {
                    System.out.println("results: " + rs.getString("id"));
                    System.out.println("results: " + rs.getString("name"));
                    System.out.println("results: " + rs.getString("password"));
                }
            }
            catch(Exception e)
            {
                System.out.println("result set Error: " + e);
            }
            connection.close();
            //System.out.println("Error Class Found");
        }
        catch (ClassNotFoundException e)
        {
            System.out.println("Error Class Not Found"+e);
        }
       catch (SQLException e)
       {
            System.out.println("SQL Error"+e);
       }
Rahul Das
Newbie Poster
5 posts since Mar 2011
Reputation Points: 26
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: