1

Can anyone help me? I've already tried to solve this for one hour and I'm still confused. Below is my code and i get this error when compiling.

Output msg::

DriverLoaded
Could Not Connect to Databasejava.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at DBConnect.<init>(DBConnect.java:11)
    at DBConnect.main(DBConnect.java:21)

Code::

import java.sql.*;

public class DBConnect {

    public DBConnect() {    
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            System.out.println("DriverLoaded");
            String url = "jdbc:odbc:; DRIVER = Microsoft Access Driver (*.mdb, *.accdb); DBQ = DB.accdb";
            Connection con = DriverManager.getConnection(url);
            System.out.println("Connection Established Successfully");
        } catch(Exception e) {
            e.printStackTrace();
            System.out.println("Could Not Connect to Database");
        }
    }

    public static void main (String args[]) {       
        DBConnect dbcon = new DBConnect();  
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Hazim Ali
  • 1,077
  • 4
  • 17
  • 28

2 Answers2

5

Overloaded methods for getConnection()

1) getConnection( String url, Properties info )

url - a database url of the form jdbc:subprotocol:subname

info - a list of arbitrary string tag/value pairs as connection arguments; normally at least a "user" and "password" property should be included

2) getConnection( String url, String user, String password )

url - a database url of the form jdbc:subprotocol:subname

user - the database user on whose behalf the Connection is being made

password - the user's password

3) getConnection( String url )

url - a database url of the form jdbc:subprotocol:subname

Considering you're using the last constructor, it seems your url syntax in incorrect. I'm not familiar with MS Access, but I'll offer a suggestion I found on another answer.

This is your syntax

"jdbc:odbc:; DRIVER = Microsoft Access Driver (*.mdb, *.accdb); DBQ = DB.accdb"

A correct syntax I found was

File f = new File("\\\\***\\***\\****\\***.accdb");

"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + f.getAbsolutePath();

Looks like you have an unnecessary semicolon after odbc and an extra space. Maybe you want to try the above syntax and see what happens. I'm not sure about the file part, but you may want to look into it if your url still fails after making the semicolon/space fix.

Check out this question also for more info on another option Connection with username and password

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
1

below is the working code for your problem...

import java.sql.*;

public class DBConnect {

    public DBConnect() {    
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            System.out.println("DriverLoaded");
            String url = "jdbc:odbc:instance";
            Connection con = DriverManager.getConnection(url);
            System.out.println("Connection Established Successfully");
        } catch(Exception e) {
            e.printStackTrace();
            System.out.println("Could Not Connect to Database");
        }
    }

    public static void main (String args[]) {       
        DBConnect dbcon = new DBConnect();  
    }
}

steps to be followed:

  1. create an access database DB.accdb in any directory of your windows xp system.
  2. open start > controlpanel > Performance and maintanance > Administrative Tools > Data sources (ODBC) >click System DSN tab > click add > choose Microsoft Access Driver (accdb,mdb) > give the name : instance , (since, getConnection("jdbc:odbc:instance") and click and browse the DB.accdb located in your hard drive ) press ok and restart your command prompt. and run the code again.

    Running the same code in eclipse

  3. create a java project.

  4. add a main class and edit the source of this main class. just copy and paste the about code.
  5. create a user library by adding the jar files from the jdk/bin directory.
  6. link the build path to the project by linking the userlibrary.
  7. run the project.
Balayesu Chilakalapudi
  • 1,386
  • 3
  • 19
  • 43