我正在尝试在学校学习基本的 SQL 测试,但不幸的是,我将我们应该使用的课程复制到我的电脑上的项目中,我收到以下错误:
java.sql.SQLException: [Microsoft][ODBC Driver Manager] 未找到数据源名称且未指定默认驱动程序
package Question1;
// Your name, Q 1
import java.sql.*;
import java.io.*;
import javax.swing.*;
public class GreenWood
{
// Set up database connection
private static final String DATABASE_FILE_NAME = "WoodDB.mdb";
private static final String DRIVER = "jdbc:odbc:DRIVER=" +
"{Microsoft Access Driver (*.mdb)};" +
"DBQ=" + new File (DATABASE_FILE_NAME).getAbsolutePath ();
static
{
try
{
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException e)
{
System.out.println ("Class not found");
e.printStackTrace ();
}
}
private Connection dbcon;
private BufferedReader keyb = new BufferedReader (new InputStreamReader (System.in));
public GreenWood ()
{
System.out.println ("WoodDB Connection");
try
{
dbcon = DriverManager.getConnection (DRIVER);
Statement stmt = dbcon.createStatement ();
System.out.println ("Connection successful\n");
char choice = ' ';
do
{
//Prints options for user input
choice = keyb.readLine ().toUpperCase ().charAt (0);
System.out.println (" ");
switch (choice)
{
//calls query methods based on user input
}
}
while (choice != 'X');
dbcon.close ();
System.out.println ("Done");
Thread.sleep (1000);
System.exit (0);
} // try
catch (Exception e)
{
// process exceptions here
System.out.println ("Connection unsuccessful");
e.printStackTrace ();
System.out.println (e.toString ());
}
} // HoutSoorte constructor
//Query Methods
//Main creates new instance of GreenWood
我的 WoodDB 数据库位于项目根目录中。
我已经做了一些故障排除,我认为问题是驱动程序位置的 URL;
dbcon = DriverManager.getConnection (DRIVER);
司机是:
private static final String DRIVER = "jdbc:odbc:DRIVER=" +
"{Microsoft Access Driver (*.mdb)};" +
"DBQ=" + new File (DATABASE_FILE_NAME).getAbsolutePath ();
经过大约一个小时的研究,我仍然和以前一样困惑。如果有人可以通过用简单的语言解释问题以及我如何解决它来帮助这个小程序程序员(我),那将不胜感激。