1

Hi guys i trying to connect SQL server with netbeans

public class SQLconnection {

    /**
     * @param args the command line arguments
     * @throws java.lang.ClassNotFoundException
     * @throws java.sql.SQLException
     */
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        String connectionURL = "jdbc:derby://localhost:1527;databaseName=Ornek;user=sa;password=123";
        Connection con  = DriverManager.getConnection(connectionURL);

        System.out.println("Connect");
    }

}

But İ have errors in Netbeans and i dont know how to pass

Exception in thread "main" java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) at sqlconnection.SQLconnection.main(SQLconnection.java:23)

driconmax
  • 956
  • 1
  • 18
  • 32
  • 4
    you need to add the Jdbc connector jar as an external file to the classpath – Yahya Apr 26 '17 at 20:13
  • Why are you trying to load the JDBC-ODBC driver (which no longer exists in Java 8), then attempting to connect to a Derby database (which doesn't even use that JDBC-ODBC driver), while your question mentions Microsoft SQL Server? If you want to connect to Microsoft SQL Server, you need to use the Microsoft SQL Server JDBC driver, and then use [the right connection string](https://learn.microsoft.com/en-us/sql/connect/jdbc/connecting-to-sql-server-with-the-jdbc-driver). – Mark Rotteveel Apr 27 '17 at 07:54

2 Answers2

1

You need to find your driver .jar and add it to your classpath.

How to setup classpath in Netbeans?

It seems like the JDBC-ODBC bridge was removed in Java 8. The best thing I could find for you is a hack solution found in answer two of this link: Removal of JDBC ODBC bridge in java 8

As Oracle has stated here:

http://docs.oracle.com/javase/7/docs/technotes/guides/jdbc/bridge.html

"Oracle recommends that you use JDBC drivers provided by the vendor of your database instead of the JDBC-ODBC Bridge."

Community
  • 1
  • 1
4dc0
  • 453
  • 3
  • 11
-2

A java.lang.classnotfoundexception sun.jdbc.odbc.jdbcodbcdriver error occurs when you try to connect to a database from Java using JDBC and the JDBC ODBC bridge driver is not available in classpath.

You need to provide the driver jar in classpath.

David L
  • 32,885
  • 8
  • 62
  • 93