这是我的代码:
public class ConnectDB {
Connection conn;
Scanner kb=new Scanner(System.in);
public String ID;
public ConnectDB()
{
try
{
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println ("Driver successfully loaded");
}
catch (ClassNotFoundException c)
{
System.out.println ("Unable to load database driver");
}
//connect to the database
try
{
String filename = "PATPhase2DB.mdb";
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb,*.accdb)};DBQ=";
database += filename.trim () + ";DriverID=22;READONLY=true}";
conn = DriverManager.getConnection (database,"","");
System.out.println ("Connection to database successfully established");
}
catch (Exception e)
{
System.out.println ("Unable to connect to the database");
}
}
消息是:
驱动程序成功加载
无法连接到数据库
java.lang.NullPointerException
这在与我不同的计算机上工作,通过完全相同的代码连接到数据库。我也在运行 Windows 8。