importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;publicclassAccessDb{privateConnection...
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class AccessDb {
private Connection conn = null;
private ResultSet rs = null;
// 建立连接,形参mdbFile,就是要访问的数据库文件路径(.mdb格式)
public void createConnection(String mdbFile) throws ClassNotFoundException, SQLException {
String url = "jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=" + mdbFile;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(url);
}
// 执行查询,返回结果集,形参是SQL语句
public void getResultSet(String querySQL) throws SQLException {
Statement stmtSelect = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = stmtSelect.executeQuery(querySQL);
//int count1 = rs.getInt(2);
rs.last();
String count = rs.getString(1);
System.out.println(/*"first:" + count1+ */"second:"+ count);
}
public void close() {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
AccessDb adb = new AccessDb();
adb.createConnection("C:\\Users\\Allen\\Desktop\\FAJS_2012_3.mdb");
String sql ="select University ''";
adb.getResultSet(sql);
adb.close();
}
}
代码是这样的,是想简单的访问以下数据库里面的信息,得到里面的一些数据,但是出现了下面的错误:
展开