run:java.sql.SQLException:[Microsoft] [ODBC驱动程序管理器]无效的连接字符串属性

时间:2012-09-24 12:25:19

标签: java

下面是我的示例代码,它工作正常,但在我的笔记本电脑上使用 Windows 7家庭高级版64位却没有。我收到以下错误:

run:
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid connection string attribute

我正在使用 NetBeans

package javaapplication1;

import static java.lang.System.*;
import java.sql.*;

public class DBDemo1
{
    public static void main(String[] args)
    {
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String sourceURL = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)}DBQ=UserDB.mdb;";
            Connection userDB = DriverManager.getConnection(sourceURL, "admin", "");
            Statement myStatement = userDB.createStatement();
            String writeString = "INSERT INTO Users(Firstname, Surname, Id) VALUES('Fred', 'Bloggs', 'bf01')";
            myStatement.executeUpdate(writeString);

            ResultSet results = myStatement.executeQuery("SELECT Firstname, Surname, Id FROM Users ORDER BY Id");

            while (results.next())
            {
                out.print(results.getString(1) + " ");
                out.print(results.getString(2) + " ");
                out.println(results.getString(3));
            }
            results.close();
        }
        //The following exceptions MUST be caught
        catch(ClassNotFoundException cnfe)
        {
            out.println(cnfe);
        }
        catch(SQLException sqle)
        {
            out.println(sqle);
        }
    }
}

感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

您的sourceURL缺少分号;

String sourceURL = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=UserDB.mdb;";
                                                                      ^

答案 1 :(得分:0)

试试这个:

String sourceURL = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)}DBQ=UserDB.mdb;DriverID=22";