aspose file tools*
The moose likes JDBC and the fly likes Not able to connect to the database with my JDBC program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Not able to connect to the database with my JDBC program" Watch "Not able to connect to the database with my JDBC program" New topic
Author

Not able to connect to the database with my JDBC program

rohit jagannath
Greenhorn

Joined: Sep 17, 2011
Posts: 6
Hello friends....

I started working on JDBC. Im using the Oracle 10G software as my database and NetBeans as my IDE. Im making use of type-4 driver for my jdbc operations.

Here goes my project...

1) I created a table using oracle 10g.

2) Included the required library. (path : C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14_g.jar)

3) Typed a program like the one below to connect to the database where i have created the table.....


package jdbc;

import java.sql.DriverManager;
import java.sql.*;

public class db_connection_test{
public static void main(String[] args){
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbcracle:thin:@localhost:1521","hr","hr"); // Im using a Type-4 driver.
System.out.println("Connecton Established");
}
catch(Exception e){
System.out.println("Connection cannot be established"+e);
}
}

}

4) now, if i run the above program the connection is not getting established. Im getting an exception stating " Connection cannot be establishedjava.sql.SQLException: Io exception: The Network Adapter could not establish the connection "


So, friends i need any one of your help on these. Please do analize this problem and get me the solution for the same.....

Regards,
Rohit.J
Philip Thamaravelil
Ranch Hand

Joined: Feb 09, 2006
Posts: 99


Rohit,
Have you checked to ensure the service is running? Have you verified your port # is correct?

Can you provide the full stack trace?

Cheers,
Philip
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1717
    
  14

Assuming the JDBC driver has been loaded OK, then check that your Oracle TNS listener service and the Oracle XE database service are both running (look in Windows services).


No more Blub for me, thank you, Vicar.
rohit jagannath
Greenhorn

Joined: Sep 17, 2011
Posts: 6
Philip Thamaravelil wrote:

Rohit,
Have you checked to ensure the service is running? Have you verified your port # is correct?

Can you provide the full stack trace?

Cheers,
Philip


Sir, since i am a beginner and i dont have any idea about port # or stack trace. So, i request you to be more clear on this.
Rohit.J
rohit jagannath
Greenhorn

Joined: Sep 17, 2011
Posts: 6
chris webster wrote:Assuming the JDBC driver has been loaded OK, then check that your Oracle TNS listener service and the Oracle XE database service are both running (look in Windows services).

Yes sir. My Oracle TNS listener and Oracle database service are both working fine.

I executed this program on eclipse. but, here also im getting the same exception. "Network adapter could not establish connection"
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

The stack trace tells you what line of code threw the exception, and what that exception was, and what line of code called the line which threw the exception, and so on. In other words it points exactly at where and what the problem was. Unlike your code, which tells you almost nothing. So try it, you'll like it:



As for port number: well, you're using Oracle so you must be working for a company large enough to have somebody who manages databases or networks. Ask them for basic training which should include that concept. Just explaining that by itself would still leave you too much of a beginner to work with the product, so I'm not going to do that.
rohit jagannath
Greenhorn

Joined: Sep 17, 2011
Posts: 6
Paul Clapham wrote:The stack trace tells you what line of code threw the exception, and what that exception was, and what line of code called the line which threw the exception, and so on. In other words it points exactly at where and what the problem was. Unlike your code, which tells you almost nothing. So try it, you'll like it:



As for port number: well, you're using Oracle so you must be working for a company large enough to have somebody who manages databases or networks. Ask them for basic training which should include that concept. Just explaining that by itself would still leave you too much of a beginner to work with the product, so I'm not going to do that.


ya sir... i used the printStackTrace method. I am getting the below listed exceptions. Not able to rectify what these are???

java.sql.SQLException: Io exception: The Network Adapter could not establish the connection
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:145)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:190)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:363)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:401)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:441)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:839)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at database.java_database.main(java_database.java:10)
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1717
    
  14

Not sure what else might be going on in your Java, but it looks like you have forgotten to tell it which database name to use. If you're using Orqacle XE the default DB name is "XE" so your JDBC connection might be something like:



This assumes your TNS listener is listening on port 1521, and that "XE" has been set up as a TNS name, which should happen automatically if you've installed OracleXE properly.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

rohit jagannath wrote:i used the printStackTrace method. I am getting the below listed exceptions. Not able to rectify what these are???

java.sql.SQLException: Io exception: The Network Adapter could not establish the connection
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:145)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:190)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:363)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:401)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:441)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:839)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at database.java_database.main(java_database.java:10)


Okay. There's only one exception there, not "exceptions", for a start. The first line tells you what the exception was (java.sql.SQLException) and the reason for it (The Network Adapter could not establish the connection). The lines after that refer to lines of code in various classes. The usual way of working with those lines is to scan down until you see a reference to your code. That would be at the bottom, in this case:

at java.sql.DriverManager.getConnection(DriverManager.java:185)
at database.java_database.main(java_database.java:10)


Here it says that at line 10, in a method named "main" in a class named "database.java_database", you called method "getConnection" on an object of class "java.sql.DriverManager". You should now be able to point at the line of code in your program which caused the exception.
rohit jagannath
Greenhorn

Joined: Sep 17, 2011
Posts: 6
chris webster wrote:Not sure what else might be going on in your Java, but it looks like you have forgotten to tell it which database name to use. If you're using Orqacle XE the default DB name is "XE" so your JDBC connection might be something like:



This assumes your TNS listener is listening on port 1521, and that "XE" has been set up as a TNS name, which should happen automatically if you've installed OracleXE properly.


Thank you sir. I got the connection established. I had missed "XE" in my connection statement.... after putting it, my statement looked like this,

Connection con = DriverManager.getConnection("jdbcracle:thin:@localhost:1521:xe","system","oracle");

Thanks a lot.

Regards,
Rohit
rohit jagannath
Greenhorn

Joined: Sep 17, 2011
Posts: 6
Paul Clapham wrote:
rohit jagannath wrote:i used the printStackTrace method. I am getting the below listed exceptions. Not able to rectify what these are???

java.sql.SQLException: Io exception: The Network Adapter could not establish the connection
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:145)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:190)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:363)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:401)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:441)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:839)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at database.java_database.main(java_database.java:10)


Okay. There's only one exception there, not "exceptions", for a start. The first line tells you what the exception was (java.sql.SQLException) and the reason for it (The Network Adapter could not establish the connection). The lines after that refer to lines of code in various classes. The usual way of working with those lines is to scan down until you see a reference to your code. That would be at the bottom, in this case:

at java.sql.DriverManager.getConnection(DriverManager.java:185)
at database.java_database.main(java_database.java:10)


Here it says that at line 10, in a method named "main" in a class named "database.java_database", you called method "getConnection" on an object of class "java.sql.DriverManager". You should now be able to point at the line of code in your program which caused the exception.


ok.. i got it sir. I had missed to write "XE" in that line.

Thank you for your guidence...

Regards,
Rohit
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Not able to connect to the database with my JDBC program