0

I have just started getting familiar with JAVA JDBC. When I am trying to compile the below code in Eclipse,I am getting an error. I hope I will get some help here. Error is mentioned below the code.

import java.sql.*;
import sun.jdbc.odbc.*;

public class JdbcDriver {

    public static void main(String args[]) throws SQLException
    {
        JdbcOdbcDriver d = new JdbcOdbcDriver();
        System.out.println("Drivers are loaded");
        //register driver
     DriverManager.registerDriver(d);
        System.out.println("Driver is registered");
        //Establish connection with Oracle DB
        Connection con= DriverManager.getConnection("jdbc:odbc:oradsn","Scott", "Tiger");
    if(con!=null)
        System.out.println("Connection is Established");
    else
        System.out.println("Connection is Fail");
        //create statement object for sending query to DB

    Statement st= con.createStatement();
    System.out.println("Statement object is created");
    //use statement object to execute the query
    String nss= "INSERT INTO DEPT VALUES(90,"IT","KPHB")";
    int i=st.executeUpdate(nss);
    System.out.println(i+"rec are inserted");
    //close the connection
    con.close();
    st.close();
    }
    }

Errors in console:

**Exception in thread "main" java.lang.Error: Unresolved compilation problems: Access restriction: The type JdbcOdbcDriver is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar **Access restriction: The constructor JdbcOdbcDriver() is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar Access restriction: The type JdbcOdbcDriver is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar Syntax error on tokens, delete these tokens at JdbcDriver.main(JdbcDriver.java:8)****

Can anyone help me? Thank you in advance.

Jens
  • 67,715
  • 15
  • 98
  • 113
  • Do not use `sun` classes directly – Jens Apr 18 '17 at 13:21
  • Also the JDBC-ODBC Bridge is removed in Java 8. You should not use it anymore – Jens Apr 18 '17 at 13:22
  • Thanks a ton for your help. – Sudheer Apr 18 '17 at 13:25
  • In any case instead of `new`ing it, you should use `Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")` to load it. It registers itself, so there is also zero reason to call `DriverManager.registerDriver` (which should only called by JDBC drivers initializing themselves). And if you want to connect to an Oracle database, you should really use the Oracle JDBC driver instead. – Mark Rotteveel Apr 18 '17 at 14:04

0 Answers0