jdk1.8环境下java如何连接access数据库
因为jdk1.8环境不支持sun.jdbc.odbc.JdbcOdbcDriver驱动,所以我从别的地方拷贝了rt.jar作为外接BulidPath,可是虽然sun.jd...
因为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();
}
} 展开
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();
}
} 展开
1个回答
2014-12-22
展开全部
驱动改下,下面自己参考,数据库名即access的文件名,加路径,路径为绝对路径为access在电脑上的路径,注意是双斜杆!!!,不需要用户名密码
try
{ //加载数据库驱动程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{ System.out.println("加载数据库驱动程序错误!");
}
String dbf="F:\\linguosheng\\文档\\db.mdb"; //连接一个access数据库
dburl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=" + dbf;
try
{ //创建数据库连接对象
conn = DriverManager.getConnection(dburl, "", "");//(url,账号,密码)
//创建命令执行对象(一般的查询和更新操作)
stmt = conn.createStatement();
//需要使用ResultSet对象进行更新操作时
rstmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
}
catch(SQLException e)
{ System.out.println("创建数据库连接错误!");
}
}
try
{ //加载数据库驱动程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{ System.out.println("加载数据库驱动程序错误!");
}
String dbf="F:\\linguosheng\\文档\\db.mdb"; //连接一个access数据库
dburl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=" + dbf;
try
{ //创建数据库连接对象
conn = DriverManager.getConnection(dburl, "", "");//(url,账号,密码)
//创建命令执行对象(一般的查询和更新操作)
stmt = conn.createStatement();
//需要使用ResultSet对象进行更新操作时
rstmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
}
catch(SQLException e)
{ System.out.println("创建数据库连接错误!");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询