📄 queryrunner.java
字号:
* * @param sql The SQL statement to execute. * @param param The replacement parameter. * @param rsh The handler used to create the result object from * the <code>ResultSet</code>. * * @return An object generated by the handler. * @throws SQLException */ public Object query(String sql, Object param, ResultSetHandler rsh) throws SQLException { return this.query(sql, new Object[] { param }, rsh); } /** * Executes the given SELECT SQL query and returns a result object. * The <code>Connection</code> is retrieved from the * <code>DataSource</code> set in the constructor. * * @param sql The SQL statement to execute. * @param params Initialize the PreparedStatement's IN parameters with * this array. * * @param rsh The handler used to create the result object from * the <code>ResultSet</code>. * * @return An object generated by the handler. * @throws SQLException */ public Object query(String sql, Object[] params, ResultSetHandler rsh) throws SQLException { Connection conn = this.ds.getConnection(); try { return this.query(conn, sql, params, rsh); } finally { DbUtils.close(conn); } } /** * Executes the given SELECT SQL without any replacement parameters. * The <code>Connection</code> is retrieved from the * <code>DataSource</code> set in the constructor. * * @param sql The SQL statement to execute. * @param rsh The handler used to create the result object from * the <code>ResultSet</code>. * * @return An object generated by the handler. * @throws SQLException */ public Object query(String sql, ResultSetHandler rsh) throws SQLException { return this.query(sql, (Object[]) null, rsh); } /** * Throws a new exception with a more informative error message. * * @param cause The original exception that will be chained to the new * exception when it's rethrown. * * @param sql The query that was executing when the exception happened. * * @param params The query replacement paramaters; <code>null</code> is a * valid value to pass in. * * @throws SQLException */ protected void rethrow(SQLException cause, String sql, Object[] params) throws SQLException { StringBuffer msg = new StringBuffer(cause.getMessage()); msg.append(" Query: "); msg.append(sql); msg.append(" Parameters: "); if (params == null) { msg.append("[]"); } else { msg.append(Arrays.asList(params)); } SQLException e = new SQLException(msg.toString()); e.setNextException(cause); throw e; } /** * Sets the <code>DataSource</code> this runner will use to get * database connections from. This should be called after creating a * runner with the default constructor if you intend to use the * execute methods without passing in a <code>Connection</code>. * * @param dataSource The DataSource to use. */ public void setDataSource(DataSource dataSource) { this.ds = dataSource; } /** * Execute an SQL INSERT, UPDATE, or DELETE query without replacement * parameters. * * @param conn The connection to use to run the query. * @param sql The SQL to execute. * @return The number of rows updated. * @throws SQLException */ public int update(Connection conn, String sql) throws SQLException { return this.update(conn, sql, (Object[]) null); } /** * Execute an SQL INSERT, UPDATE, or DELETE query with a single replacement * parameter. * * @param conn The connection to use to run the query. * @param sql The SQL to execute. * @param param The replacement parameter. * @return The number of rows updated. * @throws SQLException */ public int update(Connection conn, String sql, Object param) throws SQLException { return this.update(conn, sql, new Object[] { param }); } /** * Execute an SQL INSERT, UPDATE, or DELETE query. * * @param conn The connection to use to run the query. * @param sql The SQL to execute. * @param params The query replacement parameters. * @return The number of rows updated. * @throws SQLException */ public int update(Connection conn, String sql, Object[] params) throws SQLException { PreparedStatement stmt = null; int rows = 0; try { stmt = this.prepareStatement(conn, sql); this.fillStatement(stmt, params); rows = stmt.executeUpdate(); } catch (SQLException e) { this.rethrow(e, sql, params); } finally { DbUtils.close(stmt); } return rows; } /** * Executes the given INSERT, UPDATE, or DELETE SQL statement without * any replacement parameters. The <code>Connection</code> is retrieved * from the <code>DataSource</code> set in the constructor. * * @param sql The SQL statement to execute. * @throws SQLException * @return The number of rows updated. */ public int update(String sql) throws SQLException { return this.update(sql, (Object[]) null); } /** * Executes the given INSERT, UPDATE, or DELETE SQL statement with * a single replacement parameter. The <code>Connection</code> is * retrieved from the <code>DataSource</code> set in the constructor. * * @param sql The SQL statement to execute. * @param param The replacement parameter. * @throws SQLException * @return The number of rows updated. */ public int update(String sql, Object param) throws SQLException { return this.update(sql, new Object[] { param }); } /** * Executes the given INSERT, UPDATE, or DELETE SQL statement. The * <code>Connection</code> is retrieved from the <code>DataSource</code> * set in the constructor. * * @param sql The SQL statement to execute. * @param params Initializes the PreparedStatement's IN (i.e. '?') * parameters. * @throws SQLException * @return The number of rows updated. */ public int update(String sql, Object[] params) throws SQLException { Connection conn = this.ds.getConnection(); try { return this.update(conn, sql, params); } finally { DbUtils.close(conn); } } /** * Wrap the <code>ResultSet</code> in a decorator before processing it. * This implementation returns the <code>ResultSet</code> it is given * without any decoration. * * <p> * Often, the implementation of this method can be done in an anonymous * inner class like this: * </p> * <pre> * QueryRunner run = new QueryRunner() { * protected ResultSet wrap(ResultSet rs) { * return StringTrimmedResultSet.wrap(rs); * } * }; * </pre> * * @param rs The <code>ResultSet</code> to decorate; never * <code>null</code>. * @return The <code>ResultSet</code> wrapped in some decorator. */ protected ResultSet wrap(ResultSet rs) { return rs; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -