So I was just trying out a simple JDBC beginners code that retrieves table rows. It worked well when I ran it on IntelliJ IDEA. but it won't run when I try to run it using the command line.
I have gone through many similar questions but none of them answered my problem. I have tried the following things:
- looked for any typo while compiling and running java file ( it was correct )
- included the ojdbc8 driver in classpath but that didn't work as well.
- I have verified that I have my JDK set in an environmental variable.
and my files are saved as "oracle.java" and my class name is "oracle" if that helps.
import java.sql.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.Class;
class oracle
{
public static void main(String args[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getInt(3));
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:
C:\Users\mypc\Desktop>java -cp ojdbc8.jar -Xdiag oracle
Error: Could not find or load main class oracle
Caused by: java.lang.ClassNotFoundException: oracle
java.lang.ClassNotFoundException: oracle
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Unknown Source)
at java.base/sun.launcher.LauncherHelper.loadMainClass(Unknown Source)
at java.base/sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)