Connect java to oracle

Reply

Join Date: Jun 2009
Posts: 9
Reputation: vanpersie is an unknown quantity at this point 
Solved Threads: 0
vanpersie vanpersie is offline Offline
Newbie Poster

Connect java to oracle

 
0
  #1
Jun 15th, 2009
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


  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
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 464
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Connect java to oracle

 
-1
  #2
Jun 15th, 2009
Your have to set environment variable PATH with "C:\j2sdk1.4.1\bin>".

However you may run this program.
  1. C:\j2sdk1.4.1\bin>java -classpath d:\oracle\product\10.2.0\db1\jdbc\lib;c:\; OraThin
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: juser11 is an unknown quantity at this point 
Solved Threads: 0
juser11 juser11 is offline Offline
Newbie Poster

Re: Connect java to oracle

 
0
  #3
Jun 15th, 2009
you may also have to include the necessary jar file.
If it is oracle 10g you can include ojdbc5.jar or ojdbc14.jar
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Connect java to oracle

 
0
  #4
Jun 16th, 2009
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.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 464
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Connect java to oracle

 
1
  #5
Jun 16th, 2009
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.

  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.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 464
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Connect java to oracle

 
0
  #6
Jun 16th, 2009
For your kind consideration try this code:

Create p1.java at c:\ and compile it
  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?
  1. C:\javaprg>java -cp c:\; p1

Isn't it?
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: GeekExplains is an unknown quantity at this point 
Solved Threads: 0
GeekExplains GeekExplains is offline Offline
Newbie Poster

Re: Connect java to oracle

 
0
  #7
Jun 16th, 2009
Originally Posted by adatapost View Post
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.

  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.
Cheers, Geek.
Geek Explains
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 9
Reputation: vanpersie is an unknown quantity at this point 
Solved Threads: 0
vanpersie vanpersie is offline Offline
Newbie Poster

Re: Connect java to oracle

 
0
  #8
Jun 16th, 2009
Thank you all for your helpfull replies.
Originally Posted by adatapost View Post
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\;
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: GeekExplains is an unknown quantity at this point 
Solved Threads: 0
GeekExplains GeekExplains is offline Offline
Newbie Poster

Re: Connect java to oracle

 
0
  #9
Jun 16th, 2009
Originally Posted by vanpersie View Post
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.
Cheers, Geek.
Geek Explains
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 9
Reputation: vanpersie is an unknown quantity at this point 
Solved Threads: 0
vanpersie vanpersie is offline Offline
Newbie Poster

Re: Connect java to oracle

 
0
  #10
Jun 16th, 2009
Originally Posted by adatapost View Post
However you may run this program.
  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC