Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JDBC Database Connectivity Error

Status
Not open for further replies.

JDOne

Programmer
Apr 9, 2003
40
US
Greetings,

I wrote a small Java program to connect to an existing datasource using the jdbc-odbc bridge. The compile is successful. However, when launching the class file, I get the following error message:

ORA-12154:TNS:could not resolve service name

Here's a snippet of my code describing the proposed connection:

try
{
//Load Sun's jdbc-odbc driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException cnfe) // Driver not found
{
System.err.println("Unable to load Database Driver.");
System.err.println("Details: "+cnfe);
System.exit(0);
}

//Create the database connection
Connection db_connection = DriverManager.getConnection("jdbc:eek:dbc:datasourcename","userid","password");

If I manually log on to SQLPlus, I have no problem connecting to my data source.

Thanks for your help.

JDOne
 
It seems to have a problem finding your database.

From the connection string your passed, your db name is "datasourcename", your username is "userid" and your password is "password". Is that correct, or should they represent different values?
 
Hi,

Actually, they represent different values - dvt30,cl40,cl40dve respectively.

It seems strange that it should be having difficulty finding the database when I can easily connect manually from SQLPlus using those exact same parameters.

Thanks.

JDOne
 
This has nothing to do with Java. ORA-12154 means oracle cannot locate your service name in tnsnames.ora. A quick check can be done by connecting through MS Access or other tools beside PL/SQL.

~za~
You can't bring back a dead thread!
 
You say it has nothing to do with Java. However it's Java that's returning that error when I try to launch this Java program.

Exception in thread "main" java.sql.SQLException: [Microsoft][ODBC driver for Oracle][Oracle]ORA-12154: TNS:could not resolve service name

I've never touched the file tsnames.ora. Is this service name somehow supposed to be referenced in my Java connection code? I've never seen that used as a paramater. Just wondering.

Thanks.

JDOne

 

Cause:
This error indicates that the service (or TNS alias) specified when trying to connect does not exist.

‘TNS aliases’ or ‘service names’ are defined locally to each workstation. Thus different workstations could have a completely different alias to refer to the same database.

Remedy:
Check what aliases are defined on the workstation, either use one that is defined or add an appropriate one.

The following methods can be used to view the TNS names:

Look in the file ‘ORANT\ Network\ Admin\ Tnsnames.ora’ (for versions of Oracle prior to 8.1) or ‘Oracle\ Ora81\ Network\ Admin\ Tnsnames.ora’ (for versions of Oracle from 8.1 onwards). In this file each defined name will normally have ‘.world’ appended to it.

For Oracle 8, run ‘Oracle Net8 Easy Config’. This will list the available service names.

For Oracle 7, run ‘SQL Net Easy Configuration’. This provides the option to list the available service names.

For Oracle 8.1 onwards: Try specifying the full service name, i.e. as it appears in the tnsnames file.

If a connection alias is not being explicity provided then try to identify what alias Oracle thinks you are using. This will typically be contained in the ‘sqlnet.log’ file that the Oracle client will produce. Possible places where Oracle could be getting this (default) alias from:
The environment variable ‘LOCAL’.
 
>>>> Exception in thread "main" java.sql.SQLException: [Microsoft][ODBC driver for Oracle][


Why are you using an ODBC driver (and thus a JDBC-ODBC bridge) to connect to Oracle ?

Surely this is madness ?! Why not use Oracle's JDBC drivers that come with Oracle (in the classes12.zip package) ?? Your connection will no doubt resolve correctly, plus will be more stable and quicker ... there's lots of JDBC examples in Oracle's docs ...
 
>I've never touched the file tsnames.ora.
I understand you never touch it. However, using the
directory structure provided by jwenting, you may
want to verify the tnsnames.ora against another
"working" tnsnames.ora. Make sure the entry for your
schema is correct.

Sedj is right. You want to use Oracle jdbc driver(that is
a native driver). The performance will be multiplied once
you solve the above 'ORA-12154' problem.

>Exception in thread "main" java.sql.SQLException: [Microsoft][ODBC driver for Oracle][Oracle]ORA-12154
You will get this error regardless of tools you use, i.e
MS Access. That's why I said, that is an Oracle problem,
not java.


~za~
You can't bring back a dead thread!
 
>I've never touched the file tsnames.ora.
I understand you never touch it. However, using the
directory structure provided by JVZ, you may
want to verify the tnsnames.ora against another
"working" tnsnames.ora. Make sure the entry for your
schema is correct.

Sedj is right. You want to use Oracle jdbc driver(that is
a native driver). The performance will be multiplied once
you solve the above 'ORA-12154' problem.

>Exception in thread "main" java.sql.SQLException: [Microsoft][ODBC driver for Oracle][Oracle]ORA-12154
You will get this error regardless of tools you use, i.e
MS Access. That's why I said, that is an Oracle problem,
not java.


~za~
You can't bring back a dead thread!
 
Thanks all, for your responses.

Regarding the Oracle JDBC driver that's in the classes12.zip package - how would I point to that driver in my connection code? Or is it set up via the control panel in windows like odbc? I've only used the jdbc-odbc bridge. This is something new to me.

Appreciate all of your help.

Thanks.

JDOne
 
if you're writing a java app, then add the ref to your CLASSPATH environment variable.

If it's a web app, then it should be in \Tomcat41\common\lib
directory.

YOu definitely wants to use the jdbc type 4 driver that is included in classes12.zip(provided by oracle).

~za~
You can't bring back a dead thread!
 
OK, but I still have one additional question. in the Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") statement in which I am currently referencing the jdbc-odbc bridge, I would imagine this would have to be modified, right? What would I replace that code with?

Thanks.

JDOne
 
You wull connect something like this ::

Code:
Class.forName("oracle.jdbc.driver.DriverManager");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@serverName:1521:myDB", "user", passwd");
 
on top of all these, make sure to fix the error "ORA-12154
" first. That is oracle specific error that you have to fix.
Your dba(if you have one) should be able to assist you.

~za~
You can't bring back a dead thread!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top