错误的代码太多了。开始阅读JDBC 教程。
尝试这样的事情:
ResultSet rc = cmd2.execute(count);
int r_count = 0;
while (rc.next()) {
r_count = rc.getInt(1);
}
您没有关闭任何 JDBC 资源,这只会让您伤心。
您应该外部化您的连接信息。
我会将您的更多内容封装成小的、孤立的方法,而不是将多个语句以这种方式混杂在一起。
您应该考虑一个定义明确的、基于接口的持久层。
进行那些 SQL 查询private static final String
。他们不需要是本地的。
我建议您尝试更多类似的东西。从一个界面开始:
package persistence;
import java.util.List;
import java.util.Map;
/**
* EventDao
* @author Michael
* @link http://stackoverflow.com/questions/5016730/creating-a-dsn-less-connection-for-ms-access-within-java
* @since 6/25/13 5:19 AM
*/
public interface EventDao {
List<Map<String, Object>> findAllEvents();
}
然后写一个实现:
package persistence;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* EventDaoImpl
* @author Michael
* @link http://stackoverflow.com/questions/17213307/execute-access-query-in-resultset-java/17213356?noredirect=1#comment25072519_17213356
* @since 6/25/13 5:15 AM
*/
public class EventDaoImpl implements EventDao {
private static final String DEFAULT_URL = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)}; DBQ=C:\\test\\TestEJFolder\\BWC_Ejournal.mdb;";
private static final String SQL_FIND_ALL_EVENTS = "SELECT * FROM Events";
private Connection connection;
public EventDaoImpl(Connection connection) {
this.connection = connection;
}
@Override
public List<Map<String, Object>> findAllEvents() {
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
Statement st = null;
ResultSet rs = null;
try {
st = this.connection.createStatement();
rs = st.executeQuery(SQL_FIND_ALL_EVENTS);
results = map(rs);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
close(rs);
close(st);
}
return results;
}
public static Connection createConnection(String driver, String url, String username, String password) throws ClassNotFoundException, SQLException {
Class.forName(driver);
if ((username == null) || (password == null) || (username.trim().length() == 0) || (password.trim().length() == 0)) {
return DriverManager.getConnection(url);
} else {
return DriverManager.getConnection(url, username, password);
}
}
public static void close(Connection connection) {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Statement st) {
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void rollback(Connection connection) {
try {
if (connection != null) {
connection.rollback();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static List<Map<String, Object>> map(ResultSet rs) throws SQLException {
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
try {
if (rs != null) {
ResultSetMetaData meta = rs.getMetaData();
int numColumns = meta.getColumnCount();
while (rs.next()) {
Map<String, Object> row = new HashMap<String, Object>();
for (int i = 1; i <= numColumns; ++i) {
String name = meta.getColumnName(i);
Object value = rs.getObject(i);
row.put(name, value);
}
results.add(row);
}
}
} finally {
close(rs);
}
return results;
}
public static List<Map<String, Object>> query(Connection connection, String sql, List<Object> parameters) throws SQLException {
List<Map<String, Object>> results = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = connection.prepareStatement(sql);
int i = 0;
for (Object parameter : parameters) {
ps.setObject(++i, parameter);
}
rs = ps.executeQuery();
results = map(rs);
} finally {
close(rs);
close(ps);
}
return results;
}
}