Click here to Skip to main content
15,902,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am trying to Create a DSN-less Connection for MS-Access. I know to how connect access database using DSN(go to control panel--->etc etc) but i am trying to connect it without Creating DSN.
Here is my code:-
Java
import java.sql.*;
public class Withoutdsn {

    public static void main(String[] args) 
    {
        try
        {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con=DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb,*.accdb)};Dbq=dsn.accdb");
        Statement st=con.createStatement();
        ResultSet rs=st.executeQuery("Insert into tab values('Anoop','Delhi')");
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

Please if you find any mistake done in the above code, then help me to correct it.
Thanks in Advance
Posted

1 solution

You are not setting the file name in the connection string. You need to identify where the database file is. This should be the full path to the database.

See ConnectionStrings.com Access Connection Strings[^] for more information on how to format the connection string.
 
Share this answer
 
v2
Comments
Anoop Kr Sharma 21-Jun-13 12:36pm    
Means I have to provide Complete link of data base file(e.g = C:\project\dsn.accdb). Am i right
Anoop Kr Sharma 21-Jun-13 12:54pm    
Connection con=DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb,*.accdb)};Dbq=C:\\Documents and Settings\\Administrator\\My Documents\\NetBeansProjects\\withoutdsn\\src\\withoutdsn\\dsn.accdb");

Still getting error
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Ron Beyer 21-Jun-13 13:28pm    
See this: http://www.selikoff.net/2011/07/26/connecting-to-ms-access-file-via-jdbc-in-64-bit-java/

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