我正在尝试在学校学习基本的SQL测试,但不幸的是,我将本应使用的类复制到了我的PC上的项目中,但出现以下错误:
java.sql.SQLException:[Microsoft] [ODBC驱动程序管理器]找不到数据源名称,也未指定默认驱动程序
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 ();
经过大约一个小时的研究,我仍然和以前一样感到困惑。如果有人能用婴儿的语言解释问题以及我如何解决这个问题,可以帮助这个nub程序员,我将不胜感激。
尝试使用32位JVM。尝试从64位JVM连接时,出现相同的错误消息。