我试图能够从访问数据库访问数据。
我已经在 ODBC 管理器下为 *.mdb 和 *.accdb 文件配置了 odbc 驱动程序。我正在通过并行使用 Windows 7(不确定这是否有所不同)
当我运行程序时,我收到以下错误:
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6964)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7121)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3080)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at DataAccess.main(DataAccess.java:13)
开始代码:
import java.sql.*;
public class DataAccess
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String connURL = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+"C:\\Users\\alexmac\\Desktop\\OASDatabase\\OAS_Database";
String table = "Student";
Connection conn = DriverManager.getConnection(connURL, "", "");
Statement s = conn.createStatement();
// Fetch table
String selTable = "SELECT * FROM " + table;
s.execute(selTable);
ResultSet rs = s.getResultSet();
while((rs!=null) && (rs.next()))
{
System.out.println(rs.getString(1) + " : " + rs.getString(2));
}
// close and cleanup
s.close();
conn.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}