0

I am trying to connect my sql server database to my code in java.

To get started I want to just make sure I can connect to the database via DSN but I am getting the error:

java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver

Here is my code:

package javaapplication1;

import java.sql.*;

public class JavaApplication1 {

    public static void main(String[] args) 
    {
        Connection con;
        Statement stmt;
        ResultSet rs;

        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("jdbc:odbc:SQLACCESS");
            System.out.print("CONNECTION SUCCESSFUL");
        }catch(Exception e)
        {
            System.err.println(e);
        }  
    }
}

The DSN named "SQLACCESS" does not require a username or password. How should I go about connecting the two?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
SQLUser
  • 97
  • 11

1 Answers1

0

Everything starting with sun. is specific to Sun JDK. So it will not work on other instances.

The method you describe doesn't work in Java 8. They have removed the class. There is an article on how to workaround it (but I will look if there is something better): https://community.yellowfinbi.com/knowledge-base/article/moving-the-jdbc-odbc-bridge-from-java-7-to-java-8

You can find information on how to connect to MS Access databse here: http://www.javaxt.com/Tutorials/JDBC/How_to_Open_a_JDBC_Connection_to_Microsoft_Access

Aleh Maksimovich
  • 2,622
  • 8
  • 19