简体   繁体   中英

Cannot connect to database Error: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

I am having errors with connecting to a MS Access Database (CDCollection.accdb) from Java. The IDE we are using is Netbeans . I have done research on possible solutions as to the error, but due to being inexperienced in the topic, I am not too sure how to solve it. The rest of my class are having the same problem and the teacher does no know how to solve the problem either.

My class is now in the final chapter, Learning Unit 10: Accessing a Database from a Java program, of Funworks 's exploring IT: Java Programming. Grade 11.

I would post links to the three sources I have found most useful, but Stack Overflow says I need to have 10 reputation points to post more than 2 links. I have searched both Stack Overflow and the web to find the solution to this error, but those were more advanced than where I currently am. Edit: Here are the links, thanks to those who upvoted me. The solution they suggest makes sense, but does not seem to work...

http://community.microfocus.com/borland/managetrack/starteam/w/knowledge_base/16933.error-java-sql-sqlexception-microsoft-odbc-driver-manager-data-source-name-not-found-and-no-default-driver-specified.aspx

http://www.coderanch.com/t/440574/JDBC/databases/java-sql-SQLException-ODBC-Driver

http://www.codeproject.com/Articles/35018/Access-MS-Access-Databases-from-Java

From what I understand, it is due to the school computers and my laptop all being 64-bit and ODBC (Open DataBase Connectivity) being 32-bit. My teacher does not know how to solve this problem, as it is her first time encountering it. When the grade 11s of last year reached this section of the book, the school computers were still 32-bit.

According to what I've read, I need to go to the OBDC Data Source Administrator and, under the System DSN tab, add "Microsoft Access Driver (*.mdb, *.accdb)". I noted that it was already there under the User DSN tab. Find the OBDC Data Source Administrator at C:\\Windows\\SysWOW64\\odbcad32.exe or Control Panel\\System and Security\\Administrative Tools\\Data Sources (ODBC) . The first is preferable, as it seems to have more Drivers available even though the Control Panel shortcut targets the same executable file.

However, I still get the same error once I have added the driver.

Here is my code:

The DB class

package cd;
import java.sql.*;

public class DB
{
    private static final String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    private static final String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ={CDCollection.accdb}";
    private Connection connection;
    private PreparedStatement statement;
    private ResultSet resultSet;

    public DB()
    {
        try
        {
            Class.forName(driver);
            System.out.println("Driver successfully loaded");
        }
        catch (ClassNotFoundException c)
        {
            System.out.println("Unable to load driver");
            System.out.println(c);
        }

        try
        {
            connection = DriverManager.getConnection(url);
            System.out.println("Connection successful");
        }
        catch (SQLException e)
        {
            System.out.println("Unable to connect");
            System.out.println(e);
        }
    }
}

And the UseDB class:

package cd;
import java.sql.*;

public class UseDB
{
    static DB db = new DB();

    public static void main(String[] args)
    {

    }
}

For the

DBQ={CDCollection.accdb}

I have tried the full path to the file ( C:\\Users\\\\Documents\\IT\\Java\\Databases\\CD\\CDCollection.accdb ), using the double backslash due to the first marking an escape character. I have also tried with forward slashes (/) and I have tried without a path, as it is in the code, and placing the database in the \\CD (the project folder), \\CD\\src (the source folder), \\CD\\src\\cd (the package folder).

Yet, despite this, I and my classmates still get the same output every single we run the program:

run:
Driver successfully loaded
Unable to connect
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
BUILD SUCCESSFUL (total time: 0 seconds)

Could I please have help? Keep in mind that my class has only just started this module today; we have only been doing Access and SQL for 1 month and have been doing Java for a year and a half.

Thank you!

I would suggest you adding a SYSTEM DATASOURCE at ODBC configuration panel. Let's suppose you name it cdcollection . Then your url variable just needs to be:

private static final String url = "jdbc:odbc:cdcollection";

Here's aa nice STEP BY STEP article on how to do just that.

Welcome to StackOverflow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM