How to connect java to Ms access Database [duplicate]
Asked Answered
G

1

5

i'm trying to connect the java to ms access database but it didn't work really well

and i got an error message like this

sun.jdbc.odbc.JdbcOdbcDriver

this is my code :

import java.sql.*;

public class main{
    public static void main(String[] args) {

        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver(*.accdb)};DBQ=D:\\Andries\\testdatabase.accdb");
            Statement st = con.createStatement();
        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }

    }
}
Gatecrasher answered 25/7, 2017 at 10:11 Comment(5)
what is the exception message ?Falbala
sun.jdbc.odbc.JdbcOdbcDriverGatecrasher
@Gatecrasher - Are you using Java 8?Cachalot
@GordThompson yeah im using java 8Gatecrasher
A bit late, but for the record, jdbc-odbc-bridge can be copied from jdk7 and into jdk8. Ucanaccess is great but not for concurrent access. I'd opt for Ucanaccess if only one process shall access the database-fileAnticoagulant
S
9

you can use ucanacess.jar for connect Ms Aceess database

show some example here http://www.benchresources.net/jdbc-msaccess-database-connection-steps-in-java-8/

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MsAccessDatabaseConnectionInJava8 {

    public static void main(String[] args) {

        // variables
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        // Step 1: Loading or registering Oracle JDBC driver class
        try {

            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
        }
        catch(ClassNotFoundException cnfex) {

            System.out.println("Problem in loading or "
                    + "registering MS Access JDBC driver");
            cnfex.printStackTrace();
        }

        // Step 2: Opening database connection
        try {

            String msAccDB = "D:/WORKSPACE/TEST_WORKSPACE/Java-JDBC/Player.accdb";
            String dbURL = "jdbc:ucanaccess://" + msAccDB; 

            // Step 2.A: Create and get connection using DriverManager class
            connection = DriverManager.getConnection(dbURL); 

            // Step 2.B: Creating JDBC Statement 
            statement = connection.createStatement();

            // Step 2.C: Executing SQL & retrieve data into ResultSet
            resultSet = statement.executeQuery("SELECT * FROM PLAYER");

            System.out.println("ID\tName\t\t\tAge\tMatches");
            System.out.println("==\t================\t===\t=======");

            // processing returned data and printing into console
            while(resultSet.next()) {
                System.out.println(resultSet.getInt(1) + "\t" + 
                        resultSet.getString(2) + "\t" + 
                        resultSet.getString(3) + "\t" +
                        resultSet.getString(4));
            }
        }
        catch(SQLException sqlex){
            sqlex.printStackTrace();
        }
        finally {

            // Step 3: Closing database connection
            try {
                if(null != connection) {

                    // cleanup resources, once after processing
                    resultSet.close();
                    statement.close();

                    // and then finally close connection
                    connection.close();
                }
            }
            catch (SQLException sqlex) {
                sqlex.printStackTrace();
            }
        }
    }
}
Scepter answered 25/7, 2017 at 11:8 Comment(1)
welcome @GatecrasherScepter

© 2022 - 2024 — McMap. All rights reserved.