I'm new to java and trying to write a simple program using JDBC. It must be something obvious but I just don't see it. I've been defining the class path manually because the only two classes (at least that I think the program needs when it runs is sitting within the same directory as the program, that is, files for ojdbc_g.jar and JDBCexample.class).
The program compiles fine using:
javac -cp ./ojdbc_g.jar JDBCexample.java
As you can tell, Oracle's JDBC driver (ojdbc_g.jar) is located in the same directory as file: JDBCexample.java. After compiling, I now also have file JDBCexample.class in this same directory. When I try to run it using:
java -classpath "*" JDBCexample or,
java -classpath ./ojdbc6_g.jar:./JDBCexample.class JDBCexample or,
java -classpath "*":ojdbc6_g.jar JDBCexample
I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: JDBCexample
Caused by: java.lang.ClassNotFoundException: JDBCexample
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: JDBCexample. Program will exit.
Here's the file: JDBCexample.java:
import java.sql.*;
import oracle.jdbc.*;
import oracle.jdbc.pool.OracleDataSource;
class JDBCexample {
public static void main(String args[]) throws SQLException {
Connection conn;
Statement stmt;
String query;
String sqlString;
ResultSet rset;
// connect to database
OracleDataSource ds = new OracleDataSource();
ds.setURL("jdbc:oracle:thin:@host6.mydomain.com:1521:ORCL");
conn = ds.getConnection(HR,HR);
// create Oracle DatabaseMetaData object
DatabaseMetaData meta = conn.getMetaData();
// show JDBC driver version
System.out.println("JDBC driver version is " + meta.getDriverVersion());
}
java -cp .:./ojdbc6_g.jar JDBCexamplejava -classpath . JDBCexample.