943,917 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4328
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 15th, 2009
0

Connect java to oracle

Expand Post »
Hi guys
Iwant to connect java to oracle ,but Iam not familiarneither jdbc nor in setting classpath
however depending on my little information,I used the following code


java Syntax (Toggle Plain Text)
  1. import java.sql.*;
  2. public class OraThin {
  3.  
  4. public static void main(String[] args)
  5. throws ClassNotFoundException, SQLException
  6. {
  7. Class.forName("oracle.jdbc.driver.OracleDriver");
  8. //
  9. // or
  10. // DriverManager.registerDriver
  11. // (new oracle.jdbc.driver.OracleDriver());
  12.  
  13. String url = "jdbc:oracle:thin:@computer-d219bd:1521:orcl";
  14. Connection conn =
  15. DriverManager.getConnection(url,"system", "overmars11");
  16.  
  17. conn.setAutoCommit(false);
  18. Statement stmt = conn.createStatement();
  19. ResultSet rset =
  20. stmt.executeQuery("select BANNER from SYS.V_$VERSION");
  21. while (rset.next()) {
  22. System.out.println (rset.getString(1));
  23. }
  24. stmt.close();
  25. System.out.println ("Ok.");
  26. }
  27. }
then I run the followin commands into dos command
Java Syntax (Toggle Plain Text)
  1. C:\j2sdk1.4.1\bin>javac -classpath d:\oracle\product\10.2.0\db1\jdbc\lib c:\OraThin.java
  2.  
  3. C:\j2sdk1.4.1\bin>java -classpath d:\oracle\product\10.2.0\db1\jdbc\lib c:\OraThin
but I get the error:-
Exception in thread "main" java.lang.NoClassDefFoundError: c:\OraThin
Iwould be glad if someone help me.
Last edited by vanpersie; Jun 15th, 2009 at 9:52 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
vanpersie is offline Offline
32 posts
since Jun 2009
Jun 15th, 2009
-1

Re: Connect java to oracle

Your have to set environment variable PATH with "C:\j2sdk1.4.1\bin>".

However you may run this program.
Java Syntax (Toggle Plain Text)
  1. C:\j2sdk1.4.1\bin>java -classpath d:\oracle\product\10.2.0\db1\jdbc\lib;c:\; OraThin
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jun 15th, 2009
0

Re: Connect java to oracle

you may also have to include the necessary jar file.
If it is oracle 10g you can include ojdbc5.jar or ojdbc14.jar
Reputation Points: 10
Solved Threads: 0
Newbie Poster
juser11 is offline Offline
5 posts
since Jun 2009
Jun 16th, 2009
0

Re: Connect java to oracle

The problem is not of PATH variables or the Oracle driver Jar not found in classpath, It is quite simply you are not invoking the program correctly.

When you are running any Java program, you give the "java" interpreter only the name of the main class (and quite obviously there is no main class by the name of C:\OraThin) .

Also in your case the complication rises cause you are specifying the value for your "classpath", here along with your other JARs you would also need to include the directory which contains the class file of your OraThin class ( which is what adatapost has done by including "c:" in the classpath as shown here d:\oracle\product\10.2.0\db1\jdbc\lib;c:\ ).

You should try what adatapost has suggested, I ended up giving him a bad cookie cause I failed to observe you are running your programs from inside the bin directory of your JDK installation. Hopefully someone will equalize for my blunder there.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Jun 16th, 2009
1

Re: Connect java to oracle

Welcome stephen84s.

>but I get the error:-
Exception in thread "main" java.lang.NoClassDefFoundError: c:\OraThin
Iwould be glad if someone help me.

Have you seen this?

java launcher cannot load the class when it is qualified with path or drive letter.

Java Syntax (Toggle Plain Text)
  1. C:\j2sdk1.4.1\bin>java -classpath d:\oracle\product\10.2.0\db1\jdbc\lib c:\OraThin
Last edited by adatapost; Jun 16th, 2009 at 6:38 am.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jun 16th, 2009
1

Re: Connect java to oracle

For your kind consideration try this code:

Create p1.java at c:\ and compile it
Java Syntax (Toggle Plain Text)
  1. public class p1{
  2. public static void main(String []args) {
  3. System.out.println("hello");
  4. }
  5. }

Now, change your current directory: Assume that c:\javaprg> is your current directory

What are the options you have to run this program?
Java Syntax (Toggle Plain Text)
  1. C:\javaprg>java -cp c:\; p1

Isn't it?
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jun 16th, 2009
0

Re: Connect java to oracle

Click to Expand / Collapse  Quote originally posted by adatapost ...
Welcome stephen84s.

>but I get the error:-
Exception in thread "main" java.lang.NoClassDefFoundError: c:\OraThin
Iwould be glad if someone help me.

Have you seen this?

java launcher cannot load the class when it is qualified with path or drive letter.

Java Syntax (Toggle Plain Text)
  1. C:\j2sdk1.4.1\bin>java -classpath d:\oracle\product\10.2.0\db1\jdbc\lib c:\OraThin
Yes, you're right. Java launcher can't parse a class given with path or drive. Qualification of a Java class is allowed only with package/sub-package name(s) and these packages/sub-packages, as you would certainly be aware of, are represented by the directories/sub-directories with the corresponding names.

CLASSPATH is required to be setup (or to be mentioned on the command line), as you have shown with an example, so that the class-loader knows which all root directories (and/or JARs), it will look for finding (and subsequently loading) all the qualified/non-qualified Java classes referenced by the program.

The reason for 'java.lang.NoClassDefFoundError' is that the launcher can't find a Java class with the name 'c:\OraThin' as it would not treat 'c:\' as the path qualifier of the .class file named 'OraThin.class' instead it would look for a .class file with the name represented by the entire string (including drive letter, colon, and back-slash).

Please let me know if I misunderstood your point.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
GeekExplains is offline Offline
5 posts
since Jun 2009
Jun 16th, 2009
0

Re: Connect java to oracle

Thank you all for your helpfull replies.
Click to Expand / Collapse  Quote originally posted by adatapost ...
Your have to set environment variable PATH with "C:\j2sdk1.4.1\bin>".
ok I went to Start -> Control Panel -> System -> Advanced -> Environment Variables

variable name is java_home of course.

but what the variable value?
is it
%SystemRoot%\system32;%SystemRoot%;c:\jdk1.4.2\bin\;
or
c:\jdk1.4.2\bin\;
Reputation Points: 10
Solved Threads: 0
Light Poster
vanpersie is offline Offline
32 posts
since Jun 2009
Jun 16th, 2009
0

Re: Connect java to oracle

Click to Expand / Collapse  Quote originally posted by vanpersie ...
Thank you all for your helpfull replies.

ok I went to Start -> Control Panel -> System -> Advanced -> Environment Variables

variable name is java_home of course.

but what the variable value?
is it
%SystemRoot%\system32;%SystemRoot%;c:\jdk1.4.2\bin\;
or
c:\jdk1.4.2\bin\;
Your JAVA_HOME would probably be 'c:\jdk1.4.2' as it's normally the root of your JDK installation. BTW, you don't really need to set this environment variable. It's normally used when you have more than one JDK installation and you don't want to make your CLASSPATH and PATH settings being dependent upon the root directories of those installations.

You would just change the JAVA_HOME and everything else would remain unchanged (considering that the directory structure is the same in all the installations). In case you have only one JDK installation, you would probably not mind giving the full path wherever required.

Steps to update your PATH variable with the full path of your JDK bin:-

1. Go to the 'Environment Variables' section (the same way as you have mentioned)
2. Select 'PATH'
3. Edit
4. append 'c:\jdk1.4.2\bin' to the existing value. Separator to be used would be ';' (for Windows). For example: if the existing value of PATH is 'c:\abc;d:\a1;c:\def\ghi' then the updated value should be 'c:\abc;d:\a1;c:\def\ghi;c:\jdk1.4.2\bin'.

Setting the JDK's bin in PATH would ensure that you can invoke the tools like javac (Java Compiler), java (Java Interpreter), and other tools available in the JDK bin directory from any working directory on the system.

You will need to setup PATH even if you have set up JAVA_HOME. It's just that the PATH entry will get little trimmed as you can then setup your PATH by appending ';%JAVA_HOME%\bin' (a variable is required to be enclosed within a pair of '%'s for its value to be picked) to the existing value of the PATH. Hope this helps.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
GeekExplains is offline Offline
5 posts
since Jun 2009
Jun 16th, 2009
0

Re: Connect java to oracle

Click to Expand / Collapse  Quote originally posted by adatapost ...
However you may run this program.
Java Syntax (Toggle Plain Text)
  1. C:\j2sdk1.4.1\bin>java -classpath d:\oracle\product\10.2.0\db1\jdbc\lib;c:\; OraThin
these my trials all of them not successed
C:\j2sdk1.4.1\bin>java -classpath d:\oracle\product\10.2.0\db1\jdbc\lib;c\; jdbc\lib;c;\; OraThin

Exception in thread "main" java.lang.NoClassDefFoundError: jdbc\lib;c;\;

C:\j2sdk1.4.1\bin>set classpath=d:\oracle\product\10.2.0\db1\jdbc\lib;c\; jdbc\lib;c;\; OraThin


C:\j2sdk1.4.1\bin>java -classpath d:\oracle\product\10.2.0\db1\jdbc\lib;c\; jdbc\lib;c;\; OraThin

Exception in thread "main" java.lang.NoClassDefFoundError: jdbc\lib;c;\;

C:\j2sdk1.4.1\bin>java -classpath d:\oracle\product\10.2.0\db1\jdbc\lib;c\; OraThin

Exception in thread "main" java.lang.ClassNotFoundException: oracle.jdbc.driver.
OracleDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:198)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:140)
at OraThin.main(OraThin.java:8)
Do you have suggestion
thank you.
Reputation Points: 10
Solved Threads: 0
Light Poster
vanpersie is offline Offline
32 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Hmm, UI manager and static JProgressBar?
Next Thread in Java Forum Timeline: Problem in Big number in java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC