java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver Exception occurring. Why?

后端 未结 5 1826
暗喜
暗喜 2020-11-22 09:13

I have created an MS Access database and assigned a DSN to it. I want to access it through my Java application.

This is what I am doing:

public class         


        
相关标签:
5条回答
  • 2020-11-22 09:50

    Setup:

    My OS windows 8 64bit
    Eclipse version Standard/SDK Kepler Service Release 2
    My JDK is jdk-8u5-windows-i586
    My JRE is jre-8u5-windows-i586
    

    This how I overcome my error.

    At the very first my Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") also didn't work. Then I login to this website and downloaded the UCanAccess 2.0.8 zip (as Mr.Gord Thompson said) file and unzip it.

    Then you will also able to find these *.jar files in that unzip folder:

    ucanaccess-2.0.8.jar
    commons-lang-2.6.jar
    commons-logging-1.1.1.jar
    hsqldb.jar
    jackcess-2.0.4.jar
    

    Then what I did was I copied all these 5 files and paste them in these 2 locations:

    C:\Program Files (x86)\eclipse\lib
    C:\Program Files (x86)\eclipse\lib\ext
    

    (I did that funny thing becoz I was unable to import these libraries to my project)

    Then I reopen the eclipse with my project.then I see all that *.jar files in my project's JRE System Library folder.

    Finally my code works.

    public static void main(String[] args) 
    {
    
        try
        {
    
            Connection conn=DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\Hasith\\Documents\\JavaDatabase1.mdb");
            Statement stment = conn.createStatement();
            String qry = "SELECT * FROM Table1";
    
            ResultSet rs = stment.executeQuery(qry);
            while(rs.next())
            {
                String id    = rs.getString("ID") ;
                String fname = rs.getString("Nama");
    
                System.out.println(id + fname);
            }
        }
        catch(Exception err)
        {
            System.out.println(err);
        }
    
    
        //System.out.println("Hasith Sithila");
    
    }
    
    0 讨论(0)
  • 2020-11-22 09:56

    For Java 7 you can simply omit the Class.forName() statement as it is not really required.

    For Java 8 you cannot use the JDBC-ODBC Bridge because it has been removed. You will need to use something like UCanAccess instead. For more information, see

    Manipulating an Access database from Java without ODBC

    0 讨论(0)
  • 2020-11-22 10:08

    in JDK 8, jdbc odbc bridge is no longer used and thus removed fro the JDK. to use Microsoft Access database in JAVA, you need 5 extra JAR libraries.

    1- hsqldb.jar

    2- jackcess 2.0.4.jar

    3- commons-lang-2.6.jar

    4- commons-logging-1.1.1.jar

    5- ucanaccess-2.0.8.jar

    add these libraries to your java project and start with following lines.

    Connection conn=DriverManager.getConnection("jdbc:ucanaccess://<Path to your database i.e. MS Access DB>");
    Statement s = conn.createStatement();
    

    path could be like E:/Project/JAVA/DBApp

    and then your query to be executed. Like

    ResultSet rs = s.executeQuery("SELECT * FROM Course");
    while(rs.next())
        System.out.println(rs.getString("Title") + " " + rs.getString("Code") + " " + rs.getString("Credits"));
    

    certain imports to be used. try catch block must be used and some necessary things no to be forgotten.

    Remember, no need of bridging drivers like jdbc odbc or any stuff.

    0 讨论(0)
  • 2020-11-22 10:09

    add these dependecies to your .pom file:

    <dependency>
      <groupId>org.hsqldb</groupId>
      <artifactId>hsqldb</artifactId>
      <version>2.5.0</version>
      <scope>test</scope>
    </dependency>
    
    <dependency>
      <groupId>com.healthmarketscience.jackcess</groupId>
      <artifactId>jackcess-encrypt</artifactId>
      <version>3.0.0</version>
    </dependency>
    
    <dependency>
      <groupId>net.sf.ucanaccess</groupId>
      <artifactId>ucanaccess</artifactId>
      <version>5.0.0</version>
    </dependency>
    
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.9</version>
    </dependency>
    
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>
    

    and add to your code to call a driver:

    Connection conn = DriverManager.getConnection("jdbc:ucanaccess://{file_location}/{accessdb_file_name.mdb};memory=false");
    
    0 讨论(0)
  • 2020-11-22 10:13

    Make sure you have closed your MSAccess file before running the java program.

    0 讨论(0)
提交回复
热议问题