Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If i try to run below code, im getting error as " java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified".
Y am i getting dis error, What correction has to be done? Can some one correct me please.
Java
public class DbAccess {

    public static void main(String args[])throws SQLException
    {
        try{
            Connection con = null;
            Statement st = null;
            ResultSet rs = null;
            String query = "Select * from employee";
            
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String accessFileName ="D:/MS-A/NewDatabase";
            String database = "jdbc:odbc:Driver={Microsoft Access Driver(*.mdb)};DBQ="+accessFileName+".mdb;";
            con=DriverManager.getConnection(database,"username","password");
            st=con.createStatement();
            rs= st.executeQuery(query);
            while((rs!=null) && (rs.next()))
            {
                System.out.println(rs.getString(1) + " : " + rs.getString(2));
            }
            st.close();
            con.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
}
Posted
Updated 18-Jun-13 0:15am
v2

Hello Shruthi,

Try installing access database engine component (available here[^]) first. It will install the necessary drivers on the machine and then your app should be able to connect to the database.

Alternatively you can also try MS Access JDBC Driver available from Easysoft[^], though I have never tried those.

Regards,
 
Share this answer
 
From the examples I found and this article Access MS-Access Databases from Java[^] it looks like you need to add a space in your connection string.
Currently is is:
Java
"jdbc:odbc:Driver={Microsoft Access Driver(*.mdb)};DBQ="+accessFileName+".mdb;"
and it looks like it needs to be
Java
"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+accessFileName+".mdb;"

Basically you need to check the driver name in ODBC and use the exact same one in your code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900