me.icymint.libra.jdbc.query.Query Maven / Gradle / Ivy
package me.icymint.libra.jdbc.query;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import me.icymint.libra.jdbc.JdbcAccessException;
import me.icymint.libra.jdbc.param.Parameter;
import me.icymint.libra.jdbc.result.Result;
/**
* 实际查询接口。
*
* @author Daniel Yu
* @since 2013-3-14
*
* @param
* 查询结果。
* @param
* 查询参数。
*/
public class Query extends
AbstractQuery, PreparedStatement> {
private final P param;
private final Result r;
public Query(Result r, P p) {
this.param = p;
this.r = r;
}
@Override
protected PreparedStatement createStatement(Connection conn, String sql) {
try {
return conn.prepareStatement(sql);
} catch (SQLException e) {
throw new JdbcAccessException(e);
}
}
@Override
protected T query(PreparedStatement u, String sql, Parameter p) {
ResultSet rs = null;
try {
p.handle(u, param);
rs = u.executeQuery();
return r.handle(rs);
} catch (SQLException e) {
throw new JdbcAccessException("执行语句:" + sql, e);
} finally {
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
throw new JdbcAccessException(e);
}
}
}
}