錯誤代碼的方式太多。開始閱讀JDBC tutorial。
嘗試這樣:
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;
}
}
上面的行的ResultSet RC = cmd2.execute(計數);拋出相同的錯誤「類型不匹配:無法從布爾轉換爲ResultSet」 – user2385057
請閱讀本教程並學習如何編寫Java和JDBC。自Java於1995年問世以來,這個問題已經解決了上百萬次。你不是第一個。我的建議是一次一個查詢。你有三種方法。這是*錯誤*。一種方法應該做好一件事。採取這些查詢之一,並使其工作在一種方法。然後做其他人。 – duffymo