您可能想要 Apache Commons 库中的 DbUtils 包:[ http://commons.apache.org/dbutils/index.html][1]
QueryRunner 类允许您执行 sql 语句,而无需手动创建 PreparedStatements,甚至为此打开连接。从示例页面:
QueryRunner run = new QueryRunner( dataSource );
try
{
// Create an object array to hold the values to insert
Object[] insertParams = {"John Doe", new Double( 1.82 )};
// Execute the SQL update statement and return the number of
// inserts that were made
int inserts = run.update( "INSERT INTO Person (name,height) VALUES (?,?)",
insertParams );
// Now it's time to rise to the occation...
Object[] updateParams = {new Double( 2.05 ), "John Doe"};
int updates = run.update( "UPDATE Person SET height=? WHERE name=?",
updateParams );
}
catch(SQLException sqle) {
// Handle it
}
因此,它基本上透明地处理准备好的语句的创建,而您真正需要知道的唯一事情是 DataSource。这也适用于非更新/插入语句,即普通选择查询,并且创建 ResultSetHandlers 的能力使您能够将 ResultSet 转换为完全准备好的 bean 或带有键的 Map是列名,值是实际的行值。当您无法实现整个 ORM 解决方案时非常有用。