Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!!");
}
}
share|improve this question
3  
How are you running this code? You need to add the drivers to the classpath. –  Boris the Spider Jun 15 at 10:14
    
Ok but i don't know how to add it to the terminal.I am not using any IDE –  Paku Jun 15 at 10:15
    
@Paku: You have a -classpath argument for the interpreter to add some directory/zip/jar to the class path. –  Abrixas2 Jun 15 at 10:17
    
Yes but which jar file or zip file? –  Paku Jun 15 at 10:20
    
The jar file(s) containing the class oracle.jdbc.driver.OracleDriver and its dependencies (which you may need to download first). –  Abrixas2 Jun 15 at 10:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.