我正在使用JDK 1.6。这样,我想连接到Access数据库。为此,我注册了一个系统DNS &尝试使用JDBC连接连接数据库。
当我连接它&通过触发一个简单的查询来测试它,以检查连接是否已经正确建立。
下面是执行查询的代码片段。
public void testConnection() {
Connection conn = DBUtil.getConnection();
Statement stmt = null;
try {
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id FROM inventory");
System.out.println("Connection Successful");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}我不止一次调用testConnection()方法。
现在的问题是,该查询第一次运行良好,其余三次会出现错误,如下所述。
java.sql.SQLException: General error
at sun.jdbc.odbc.JdbcOdbc.throwGenericSQLException(JdbcOdbc.java:7086)
at sun.jdbc.odbc.JdbcOdbc.SQLAllocStmt(JdbcOdbc.java:173)
at sun.jdbc.odbc.JdbcOdbcConnection.prepareStatement(JdbcOdbcConnection.java:465)
at sun.jdbc.odbc.JdbcOdbcConnection.prepareStatement(JdbcOdbcConnection.java:443)请让我知道哪里出了问题?
备注:我在Windows7上运行程序,64位变体。在本地机器上使用32位驱动程序注册数据源(来自path:%windir%/SYSWOW64/odbcad32.exe)
发布于 2014-01-28 10:21:20
我对此不太确定,但尝试一下这样的方法:
public void testConnection() {
Connection conn = null;
PreparedStatement pre =null;
ResultSet rs =null;
try {
conn = DBUtil.getConnection();
PreparedStatement pre = conn.prepareStatement("select id from inventory");
rs = pre.executeQuery();
System.out.println("Connection Successful");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
conn.close();
}
}https://stackoverflow.com/questions/21240888
复制相似问题