Harshil Shah
Harshil Shah

Reputation: 359

Unable to connect to the database

public class Db_try1 {


public static void main(String[] args)
{
String driver="oracle.jdbc.OracleDriver";
    String url="jdbc:oracle:thin:@localhost:1521:xe";
    String user="admin";
    String pass="admin";
    String sql="";
    Connection conn=null;
    try 
    {
        Class.forName(driver);
        System.out.println("Connecting to database Student.....");
        conn=DriverManager.getConnection(url,user,pass);

        Statement stmt = conn.createStatement();
        sql="select * from student";
        ResultSet rs= stmt.executeQuery(sql);
        System.out.println("Reading the data and printing...");
        while(rs.next())
        {
            System.out.println(rs.getInt("id"));
            System.out.println(rs.getString(1));
            System.out.println(rs.getString("mobile"));
        }
         rs.close();          stmt.close();          conn.close();
    }
    catch (Exception e)
    {
        // TODO Auto-generated catch block
                System.out.println("Error found");
        e.printStackTrace();

    }


}

}

i m using oracle 10g i can connect to the database when i click on test connectin in netbeans. but when i run this code i am getting exception classnotfoundexception..

Upvotes: 0

Views: 91

Answers (1)

Jigar Joshi
Jigar Joshi

Reputation: 240860

You need to add oracle.jdbc.OracleDriver to your class path, Add appropriate version's jar to your application's classpath

Upvotes: 2

Related Questions