在导入了外部包Access JDBC30.jar后,能成功加载驱动,但连接数据库执行sql 语句时
便报错:java.sql.SQLException: Failed to load database a1! For more information, please use SQLException.getNextException().
(好像数据库连接上了,但加载不了数据库的感觉)
求助!!!
代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBconnTest {
public static void main(String args[]) {
//步骤1:加载驱动程序
String sDriver="com.hxtt.sql.access.AccessDriver";
try{
Class.forName(sDriver);
}
catch(Exception e){
System.out.println("无法加载驱动程序");
return;
}
System.out.println("步骤1:加载驱动程序——成功!");
Connection dbCon=null;
Statement stmt=null;
String sCon = "jdbc:Access:///d:/1/a1.mdb";
try{
dbCon=DriverManager.getConnection(sCon);
if(dbCon!=null){
System.out.println("步骤2:连接数据库——成功!");
}
//步骤3:建立JDBC的Statement对象
stmt=dbCon.createStatement();
if(stmt!=null){
System.out.println("步骤3:建立JDBC的Statement对象——成功!");
}
}
catch(SQLException e){
System.out.println("连接错误:"+sCon);
System.out.println(e.getMessage());
if(dbCon!=null){
try{
dbCon.close();
}
catch(SQLException e2){}
}
return;
}
try{//执行数据库查询,返回结果
String sSQL="SELECT * "+" FROM table1";
ResultSet rs=stmt.executeQuery(sSQL);
}
catch(SQLException e){
System.out.println(e.getMessage());
}
finally{
try{
//关闭步骤3所开启的statement对象
stmt.close();
System.out.println("关闭statement对象");
}
catch(SQLException e){}
try{
//关闭步骤3所开启的statement对象
dbCon.close();
System.out.println("关闭数据库连接对象");
}
catch(SQLException e){}
}
}
}
输出结果:
步骤1:加载驱动程序——成功!
步骤2:连接数据库——成功!
步骤3:建立JDBC的Statement对象——成功!
Failed to load database a1! For more information, please use SQLException.getNextException().
关闭statement对象
关闭数据库连接对象