I am trying run a basic code for Jdbc on my linux terminal.I have sqlplus installed.The version is Oracle 11.2.0.2.Everytime i run the code i get the following errors:
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at jdbcdisplay.main(jdbcdisplay.java:17)
Now i have seen the other questions asked here and most of them are connecting to Jdbc drivers using Eclipse and downloading the the jar file seems enough.But if i had to run it in the terminal how would i do it?Would i have to download http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html or is other any other way i can do it.My code is as follows:
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Date;
import java.sql.SQLException;
class jdbcdisplay
{
public static void main(String[] args)
{
Connection conn=null;
try
{
String driver="oracle.jdbc.driver.OracleDriver";
Class.forName(driver);
System.out.println("Connecting to database...");
String jdbcUrl="jdbc:oracle:thin:@localhost:1521:orcl";
String user="Sourajyoti";
String password="Sourajyoti7";
conn=DriverManager.getConnection(jdbcUrl,user,password);
Statement stmt=conn.createStatement();
String sql="SELECT ENAME,SAL,HIREDATE FROM EMP";
ResultSet rs=stmt.executeQuery(sql);
while (rs.next())
{
String name=rs.getString("ENAME");
double salary=rs.getDouble(2);
Date date=rs.getDate("HIREDATE");
System.out.print("Name: "+name);
System.out.print("Salary: "+salary);
System.out.println("Hiredate: "+date);
}
rs.close();
stmt.close();
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (conn!=null)
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
}
System.out.println("GoodBye!!");
}
}
-classpath
argument for the interpreter to add some directory/zip/jar to the class path. – Abrixas2 Jun 15 at 10:17oracle.jdbc.driver.OracleDriver
and its dependencies (which you may need to download first). – Abrixas2 Jun 15 at 10:25