因为jdk1.8环境不支持sun.jdbc.odbc.JdbcOdbcDriver驱动,所以我从别的地方拷贝了rt.jar作为外接BulidPath,可是虽然sun.jdbc.odbc.JdbcOdbcDriver驱动,但是在程序却无法连接数据库,...
因为jdk1.8环境不支持sun.jdbc.odbc.JdbcOdbcDriver驱动,所以我从别的地方拷贝了rt.jar作为外接Bulid Path ,可是虽然sun.jdbc.odbc.JdbcOdbcDriver驱动,但是在程序却无法连接数据库,返回值为Null,请问怎么解决,最好如何不通过驱动直接连接Access数据库。
package System;
import java.sql.*; // Needed for JDBC classes
public class TestAccessDB {
private Connection conn;
private String url = "jdbc:odbc:myAccess";
private String user = "sa";
private String passWord = "123";
public void getConnection(){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(url, user, passWord);
if (conn != null) {
System.out.println("数据库连接成功");
}
} catch (Exception ex) {
System.out.println("ERROR: " + ex.getMessage());
}
}
public void queryInDB(){
try {
Statement sta = conn.createStatement();
String sqlStatement= "SELECT * FROM studentInfo";
ResultSet rs = sta.executeQuery(sqlStatement);
System.out.println("\n查找到的学生信息");
System.out.println("--------------------------");
while (rs.next()) {
System.out.print(rs.getString(2)+"\t");
System.out.print(rs.getString(3)+"\t");
System.out.print(rs.getString(4)+"\t");
System.out.print(rs.getDate(5)+"\t");
System.out.print(rs.getDouble(6)+"\n");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void closeConnection(){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
TestAccessDB selectTest = new TestAccessDB();
selectTest.getConnection();
selectTest.queryInDB();
selectTest.closeConnection();
}
}
展开