<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FDatabase_SQL_JDBC%2FSelect_Query</id>
		<title>Java/Database SQL JDBC/Select Query - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FDatabase_SQL_JDBC%2FSelect_Query"/>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/Database_SQL_JDBC/Select_Query&amp;action=history"/>
		<updated>2026-04-22T14:10:58Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java/Database_SQL_JDBC/Select_Query&amp;diff=6911&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/Database_SQL_JDBC/Select_Query&amp;diff=6911&amp;oldid=prev"/>
				<updated>2010-06-01T06:33:56Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 06:33, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java/Database_SQL_JDBC/Select_Query&amp;diff=6910&amp;oldid=prev</id>
		<title> в 18:01, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/Database_SQL_JDBC/Select_Query&amp;diff=6910&amp;oldid=prev"/>
				<updated>2010-05-31T18:01:44Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Executes SQL queries with pluggable strategies for handling ResultSets.  This class is thread safe. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
/*&lt;br /&gt;
 * Licensed to the Apache Software Foundation (ASF) under one or more&lt;br /&gt;
 * contributor license agreements.  See the NOTICE file distributed with&lt;br /&gt;
 * this work for additional information regarding copyright ownership.&lt;br /&gt;
 * The ASF licenses this file to You under the Apache License, Version 2.0&lt;br /&gt;
 * (the &amp;quot;License&amp;quot;); you may not use this file except in compliance with&lt;br /&gt;
 * the License.  You may obtain a copy of the License at&lt;br /&gt;
 *&lt;br /&gt;
 *      http://www.apache.org/licenses/LICENSE-2.0&lt;br /&gt;
 *&lt;br /&gt;
 * Unless required by applicable law or agreed to in writing, software&lt;br /&gt;
 * distributed under the License is distributed on an &amp;quot;AS IS&amp;quot; BASIS,&lt;br /&gt;
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.&lt;br /&gt;
 * See the License for the specific language governing permissions and&lt;br /&gt;
 * limitations under the License.&lt;br /&gt;
 */&lt;br /&gt;
import java.beans.IntrospectionException;&lt;br /&gt;
import java.beans.Introspector;&lt;br /&gt;
import java.beans.PropertyDescriptor;&lt;br /&gt;
import java.lang.reflect.InvocationTargetException;&lt;br /&gt;
import java.lang.reflect.Method;&lt;br /&gt;
import java.sql.Connection;&lt;br /&gt;
import java.sql.ParameterMetaData;&lt;br /&gt;
import java.sql.PreparedStatement;&lt;br /&gt;
import java.sql.ResultSet;&lt;br /&gt;
import java.sql.SQLException;&lt;br /&gt;
import java.sql.Types;&lt;br /&gt;
import java.util.Arrays;&lt;br /&gt;
import javax.sql.DataSource;&lt;br /&gt;
/**&lt;br /&gt;
 * Executes SQL queries with pluggable strategies for handling &lt;br /&gt;
 * &amp;lt;code&amp;gt;ResultSet&amp;lt;/code&amp;gt;s.  This class is thread safe.&lt;br /&gt;
 * &lt;br /&gt;
 * @see ResultSetHandler&lt;br /&gt;
 */&lt;br /&gt;
public class QueryRunner {&lt;br /&gt;
    /**&lt;br /&gt;
     * Is {@link ParameterMetaData#getParameterType(int)} broken (have we tried it yet)?&lt;br /&gt;
     */&lt;br /&gt;
    private volatile boolean pmdKnownBroken = false;&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * The DataSource to retrieve connections from.&lt;br /&gt;
     */&lt;br /&gt;
    protected final DataSource ds;&lt;br /&gt;
    /**&lt;br /&gt;
     * Constructor for QueryRunner.&lt;br /&gt;
     */&lt;br /&gt;
    public QueryRunner() {&lt;br /&gt;
        super();&lt;br /&gt;
        ds = null;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Constructor for QueryRunner, allows workaround for Oracle drivers&lt;br /&gt;
     * @param pmdKnownBroken Oracle drivers don&amp;quot;t support {@link ParameterMetaData#getParameterType(int) };&lt;br /&gt;
     * if &amp;lt;code&amp;gt;pmdKnownBroken&amp;lt;/code&amp;gt; is set to true, we won&amp;quot;t even try it; if false, we&amp;quot;ll try it,&lt;br /&gt;
     * and if it breaks, we&amp;quot;ll remember not to use it again.&lt;br /&gt;
     */&lt;br /&gt;
    public QueryRunner(boolean pmdKnownBroken) {&lt;br /&gt;
        super();&lt;br /&gt;
        this.pmdKnownBroken = pmdKnownBroken; &lt;br /&gt;
        ds = null;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * Constructor for QueryRunner, allows workaround for Oracle drivers.  Methods that do not take a &lt;br /&gt;
     * &amp;lt;code&amp;gt;Connection&amp;lt;/code&amp;gt; parameter will retrieve connections from this&lt;br /&gt;
     * &amp;lt;code&amp;gt;DataSource&amp;lt;/code&amp;gt;.&lt;br /&gt;
     * &lt;br /&gt;
     * @param ds The &amp;lt;code&amp;gt;DataSource&amp;lt;/code&amp;gt; to retrieve connections from.&lt;br /&gt;
     */&lt;br /&gt;
    public QueryRunner(DataSource ds) {&lt;br /&gt;
        super();&lt;br /&gt;
        this.ds = ds;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * Constructor for QueryRunner, allows workaround for Oracle drivers.  Methods that do not take a &lt;br /&gt;
     * &amp;lt;code&amp;gt;Connection&amp;lt;/code&amp;gt; parameter will retrieve connections from this&lt;br /&gt;
     * &amp;lt;code&amp;gt;DataSource&amp;lt;/code&amp;gt;.&lt;br /&gt;
     * &lt;br /&gt;
     * @param ds The &amp;lt;code&amp;gt;DataSource&amp;lt;/code&amp;gt; to retrieve connections from.&lt;br /&gt;
     * @param pmdKnownBroken Oracle drivers don&amp;quot;t support {@link ParameterMetaData#getParameterType(int) };&lt;br /&gt;
     * if &amp;lt;code&amp;gt;pmdKnownBroken&amp;lt;/code&amp;gt; is set to true, we won&amp;quot;t even try it; if false, we&amp;quot;ll try it,&lt;br /&gt;
     * and if it breaks, we&amp;quot;ll remember not to use it again.&lt;br /&gt;
     */&lt;br /&gt;
    public QueryRunner(DataSource ds, boolean pmdKnownBroken) {&lt;br /&gt;
        super();&lt;br /&gt;
        this.pmdKnownBroken = pmdKnownBroken;&lt;br /&gt;
        this.ds = ds;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * Execute a batch of SQL INSERT, UPDATE, or DELETE queries.&lt;br /&gt;
     * &lt;br /&gt;
     * @param conn The Connection to use to run the query.  The caller is&lt;br /&gt;
     * responsible for closing this Connection.&lt;br /&gt;
     * @param sql The SQL to execute.&lt;br /&gt;
     * @param params An array of query replacement parameters.  Each row in&lt;br /&gt;
     * this array is one set of batch replacement values. &lt;br /&gt;
     * @return The number of rows updated per statement.&lt;br /&gt;
     * @throws SQLException if a database access error occurs&lt;br /&gt;
     * @since DbUtils 1.1&lt;br /&gt;
     */&lt;br /&gt;
    public int[] batch(Connection conn, String sql, Object[][] params)&lt;br /&gt;
        throws SQLException {&lt;br /&gt;
        PreparedStatement stmt = null;&lt;br /&gt;
        int[] rows = null;&lt;br /&gt;
        try {&lt;br /&gt;
            stmt = this.prepareStatement(conn, sql);&lt;br /&gt;
            for (int i = 0; i &amp;lt; params.length; i++) {&lt;br /&gt;
                this.fillStatement(stmt, params[i]);&lt;br /&gt;
                stmt.addBatch();&lt;br /&gt;
            }&lt;br /&gt;
            rows = stmt.executeBatch();&lt;br /&gt;
        } catch (SQLException e) {&lt;br /&gt;
            this.rethrow(e, sql, params);&lt;br /&gt;
        } finally {&lt;br /&gt;
            stmt.close();&lt;br /&gt;
        }&lt;br /&gt;
        return rows;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Execute a batch of SQL INSERT, UPDATE, or DELETE queries.  The &lt;br /&gt;
     * &amp;lt;code&amp;gt;Connection&amp;lt;/code&amp;gt; is retrieved from the &amp;lt;code&amp;gt;DataSource&amp;lt;/code&amp;gt; &lt;br /&gt;
     * set in the constructor.  This &amp;lt;code&amp;gt;Connection&amp;lt;/code&amp;gt; must be in &lt;br /&gt;
     * auto-commit mode or the update will not be saved. &lt;br /&gt;
     * &lt;br /&gt;
     * @param sql The SQL to execute.&lt;br /&gt;
     * @param params An array of query replacement parameters.  Each row in&lt;br /&gt;
     * this array is one set of batch replacement values. &lt;br /&gt;
     * @return The number of rows updated per statement.&lt;br /&gt;
     * @throws SQLException if a database access error occurs&lt;br /&gt;
     * @since DbUtils 1.1&lt;br /&gt;
     */&lt;br /&gt;
    public int[] batch(String sql, Object[][] params) throws SQLException {&lt;br /&gt;
        Connection conn = this.prepareConnection();&lt;br /&gt;
        try {&lt;br /&gt;
            return this.batch(conn, sql, params);&lt;br /&gt;
        } finally {&lt;br /&gt;
            conn.close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Fill the &amp;lt;code&amp;gt;PreparedStatement&amp;lt;/code&amp;gt; replacement parameters with &lt;br /&gt;
     * the given objects.&lt;br /&gt;
     * @param stmt PreparedStatement to fill&lt;br /&gt;
     * @param params Query replacement parameters; &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; is a valid&lt;br /&gt;
     * value to pass in.&lt;br /&gt;
     * @throws SQLException if a database access error occurs&lt;br /&gt;
     */&lt;br /&gt;
    public void fillStatement(PreparedStatement stmt, Object[] params)&lt;br /&gt;
        throws SQLException {&lt;br /&gt;
        if (params == null) {&lt;br /&gt;
            return;&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        ParameterMetaData pmd = stmt.getParameterMetaData();&lt;br /&gt;
        if (pmd.getParameterCount() &amp;lt; params.length) {&lt;br /&gt;
            throw new SQLException(&amp;quot;Too many parameters: expected &amp;quot;&lt;br /&gt;
                    + pmd.getParameterCount() + &amp;quot;, was given &amp;quot; + params.length);&lt;br /&gt;
        }&lt;br /&gt;
        for (int i = 0; i &amp;lt; params.length; i++) {&lt;br /&gt;
            if (params[i] != null) {&lt;br /&gt;
                stmt.setObject(i + 1, params[i]);&lt;br /&gt;
            } else {&lt;br /&gt;
                // VARCHAR works with many drivers regardless&lt;br /&gt;
                // of the actual column type.  Oddly, NULL and &lt;br /&gt;
                // OTHER don&amp;quot;t work with Oracle&amp;quot;s drivers.&lt;br /&gt;
                int sqlType = Types.VARCHAR;&lt;br /&gt;
                if (!pmdKnownBroken) {&lt;br /&gt;
                    try {&lt;br /&gt;
                        sqlType = pmd.getParameterType(i + 1);&lt;br /&gt;
                    } catch (SQLException e) {&lt;br /&gt;
                        pmdKnownBroken = true;&lt;br /&gt;
                    }&lt;br /&gt;
                }&lt;br /&gt;
                stmt.setNull(i + 1, sqlType);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Fill the &amp;lt;code&amp;gt;PreparedStatement&amp;lt;/code&amp;gt; replacement parameters with the&lt;br /&gt;
     * given object&amp;quot;s bean property values.&lt;br /&gt;
     * &lt;br /&gt;
     * @param stmt&lt;br /&gt;
     *            PreparedStatement to fill&lt;br /&gt;
     * @param bean&lt;br /&gt;
     *            a JavaBean object&lt;br /&gt;
     * @param properties&lt;br /&gt;
     *            an ordered array of properties; this gives the order to insert&lt;br /&gt;
     *            values in the statement&lt;br /&gt;
     * @throws SQLException&lt;br /&gt;
     *             if a database access error occurs&lt;br /&gt;
     */&lt;br /&gt;
    public void fillStatementWithBean(PreparedStatement stmt, Object bean,&lt;br /&gt;
            PropertyDescriptor[] properties) throws SQLException {&lt;br /&gt;
        Object[] params = new Object[properties.length];&lt;br /&gt;
        for (int i = 0; i &amp;lt; properties.length; i++) {&lt;br /&gt;
            PropertyDescriptor property = properties[i];&lt;br /&gt;
            Object value = null;&lt;br /&gt;
            Method method = property.getReadMethod();&lt;br /&gt;
            if (method == null) {&lt;br /&gt;
                throw new RuntimeException(&amp;quot;No read method for bean property &amp;quot;&lt;br /&gt;
                        + bean.getClass() + &amp;quot; &amp;quot; + property.getName());&lt;br /&gt;
            }&lt;br /&gt;
            try {&lt;br /&gt;
                value = method.invoke(bean, new Object[0]);&lt;br /&gt;
            } catch (InvocationTargetException e) {&lt;br /&gt;
                throw new RuntimeException(&amp;quot;Couldn&amp;quot;t invoke method: &amp;quot; + method, e);&lt;br /&gt;
            } catch (IllegalArgumentException e) {&lt;br /&gt;
                throw new RuntimeException(&amp;quot;Couldn&amp;quot;t invoke method with 0 arguments: &amp;quot; + method, e);&lt;br /&gt;
            } catch (IllegalAccessException e) {&lt;br /&gt;
                throw new RuntimeException(&amp;quot;Couldn&amp;quot;t invoke method: &amp;quot; + method, e);&lt;br /&gt;
            } &lt;br /&gt;
            params[i] = value;&lt;br /&gt;
        }&lt;br /&gt;
        fillStatement(stmt, params);&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Fill the &amp;lt;code&amp;gt;PreparedStatement&amp;lt;/code&amp;gt; replacement parameters with the&lt;br /&gt;
     * given object&amp;quot;s bean property values.&lt;br /&gt;
     * &lt;br /&gt;
     * @param stmt&lt;br /&gt;
     *            PreparedStatement to fill&lt;br /&gt;
     * @param bean&lt;br /&gt;
     *            a JavaBean object&lt;br /&gt;
     * @param propertyNames&lt;br /&gt;
     *            an ordered array of property names (these should match the&lt;br /&gt;
     *            getters/setters); this gives the order to insert values in the&lt;br /&gt;
     *            statement&lt;br /&gt;
     * @throws SQLException&lt;br /&gt;
     *             if a database access error occurs&lt;br /&gt;
     */&lt;br /&gt;
    public void fillStatementWithBean(PreparedStatement stmt, Object bean,&lt;br /&gt;
            String[] propertyNames) throws SQLException {&lt;br /&gt;
        PropertyDescriptor[] descriptors;&lt;br /&gt;
        try {&lt;br /&gt;
            descriptors = Introspector.getBeanInfo(bean.getClass())&lt;br /&gt;
                    .getPropertyDescriptors();&lt;br /&gt;
        } catch (IntrospectionException e) {&lt;br /&gt;
            throw new RuntimeException(&amp;quot;Couldn&amp;quot;t introspect bean &amp;quot; + bean.getClass().toString(), e);&lt;br /&gt;
        }&lt;br /&gt;
        PropertyDescriptor[] sorted = new PropertyDescriptor[propertyNames.length];&lt;br /&gt;
        for (int i = 0; i &amp;lt; propertyNames.length; i++) {&lt;br /&gt;
            String propertyName = propertyNames[i];&lt;br /&gt;
            if (propertyName == null) {&lt;br /&gt;
                throw new NullPointerException(&amp;quot;propertyName can&amp;quot;t be null: &amp;quot; + i);&lt;br /&gt;
            }&lt;br /&gt;
            boolean found = false;&lt;br /&gt;
            for (int j = 0; j &amp;lt; descriptors.length; j++) {&lt;br /&gt;
                PropertyDescriptor descriptor = descriptors[j];&lt;br /&gt;
                if (propertyName.equals(descriptor.getName())) {&lt;br /&gt;
                    sorted[i] = descriptor;&lt;br /&gt;
                    found = true;&lt;br /&gt;
                    break;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            if (!found) {&lt;br /&gt;
                throw new RuntimeException(&amp;quot;Couldn&amp;quot;t find bean property: &amp;quot;&lt;br /&gt;
                        + bean.getClass() + &amp;quot; &amp;quot; + propertyName);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        fillStatementWithBean(stmt, bean, sorted);&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the &amp;lt;code&amp;gt;DataSource&amp;lt;/code&amp;gt; this runner is using.  &lt;br /&gt;
     * &amp;lt;code&amp;gt;QueryRunner&amp;lt;/code&amp;gt; methods always call this method to get the&lt;br /&gt;
     * &amp;lt;code&amp;gt;DataSource&amp;lt;/code&amp;gt; so subclasses can provide specialized&lt;br /&gt;
     * behavior.&lt;br /&gt;
     *&lt;br /&gt;
     * @return DataSource the runner is using&lt;br /&gt;
     */&lt;br /&gt;
    public DataSource getDataSource() {&lt;br /&gt;
        return this.ds;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Factory method that creates and initializes a &lt;br /&gt;
     * &amp;lt;code&amp;gt;PreparedStatement&amp;lt;/code&amp;gt; object for the given SQL.  &lt;br /&gt;
     * &amp;lt;code&amp;gt;QueryRunner&amp;lt;/code&amp;gt; methods always call this method to prepare &lt;br /&gt;
     * statements for them.  Subclasses can override this method to provide &lt;br /&gt;
     * special PreparedStatement configuration if needed.  This implementation&lt;br /&gt;
     * simply calls &amp;lt;code&amp;gt;conn.prepareStatement(sql)&amp;lt;/code&amp;gt;.&lt;br /&gt;
     *  &lt;br /&gt;
     * @param conn The &amp;lt;code&amp;gt;Connection&amp;lt;/code&amp;gt; used to create the &lt;br /&gt;
     * &amp;lt;code&amp;gt;PreparedStatement&amp;lt;/code&amp;gt;&lt;br /&gt;
     * @param sql The SQL statement to prepare.&lt;br /&gt;
     * @return An initialized &amp;lt;code&amp;gt;PreparedStatement&amp;lt;/code&amp;gt;.&lt;br /&gt;
     * @throws SQLException if a database access error occurs&lt;br /&gt;
     */&lt;br /&gt;
    protected PreparedStatement prepareStatement(Connection conn, String sql)&lt;br /&gt;
        throws SQLException {&lt;br /&gt;
            &lt;br /&gt;
        return conn.prepareStatement(sql);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * Factory method that creates and initializes a &lt;br /&gt;
     * &amp;lt;code&amp;gt;Connection&amp;lt;/code&amp;gt; object.  &amp;lt;code&amp;gt;QueryRunner&amp;lt;/code&amp;gt; methods &lt;br /&gt;
     * always call this method to retrieve connections from its DataSource.  &lt;br /&gt;
     * Subclasses can override this method to provide &lt;br /&gt;
     * special &amp;lt;code&amp;gt;Connection&amp;lt;/code&amp;gt; configuration if needed.  This &lt;br /&gt;
     * implementation simply calls &amp;lt;code&amp;gt;ds.getConnection()&amp;lt;/code&amp;gt;.&lt;br /&gt;
     * &lt;br /&gt;
     * @return An initialized &amp;lt;code&amp;gt;Connection&amp;lt;/code&amp;gt;.&lt;br /&gt;
     * @throws SQLException if a database access error occurs&lt;br /&gt;
     * @since DbUtils 1.1&lt;br /&gt;
     */&lt;br /&gt;
    protected Connection prepareConnection() throws SQLException {&lt;br /&gt;
        if(this.getDataSource() == null) {&lt;br /&gt;
            throw new SQLException(&amp;quot;QueryRunner requires a DataSource to be &amp;quot; +&lt;br /&gt;
                &amp;quot;invoked in this way, or a Connection should be passed in&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        return this.getDataSource().getConnection();&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Execute an SQL SELECT query with a single replacement parameter. The&lt;br /&gt;
     * caller is responsible for closing the connection.&lt;br /&gt;
     * &lt;br /&gt;
     * @param conn The connection to execute the query in.&lt;br /&gt;
     * @param sql The query to execute.&lt;br /&gt;
     * @param param The replacement parameter.&lt;br /&gt;
     * @param rsh The handler that converts the results into an object.&lt;br /&gt;
     * @return The object returned by the handler.&lt;br /&gt;
     * @throws SQLException if a database access error occurs&lt;br /&gt;
     * @deprecated Use {@link #query(Connection,String,ResultSetHandler,Object[])} instead&lt;br /&gt;
     */&lt;br /&gt;
    public Object query(Connection conn, String sql, Object param,&lt;br /&gt;
            ResultSetHandler rsh) throws SQLException {&lt;br /&gt;
        return this.query(conn, sql, rsh, new Object[] { param });&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Execute an SQL SELECT query with replacement parameters.  The&lt;br /&gt;
     * caller is responsible for closing the connection.&lt;br /&gt;
     * &lt;br /&gt;
     * @param conn The connection to execute the query in.&lt;br /&gt;
     * @param sql The query to execute.&lt;br /&gt;
     * @param params The replacement parameters.&lt;br /&gt;
     * @param rsh The handler that converts the results into an object.&lt;br /&gt;
     * @return The object returned by the handler.&lt;br /&gt;
     * @throws SQLException if a database access error occurs&lt;br /&gt;
     * @deprecated Use {@link #query(Connection,String,ResultSetHandler,Object[])} instead&lt;br /&gt;
     */&lt;br /&gt;
    public Object query(Connection conn, String sql, Object[] params,&lt;br /&gt;
            ResultSetHandler rsh) throws SQLException {&lt;br /&gt;
                return query(conn, sql, rsh, params);&lt;br /&gt;
            }&lt;br /&gt;
    /**&lt;br /&gt;
     * Execute an SQL SELECT query with replacement parameters.  The&lt;br /&gt;
     * caller is responsible for closing the connection.&lt;br /&gt;
     * &lt;br /&gt;
     * @param conn The connection to execute the query in.&lt;br /&gt;
     * @param sql The query to execute.&lt;br /&gt;
     * @param rsh The handler that converts the results into an object.&lt;br /&gt;
     * @param params The replacement parameters.&lt;br /&gt;
     * @return The object returned by the handler.&lt;br /&gt;
     * @throws SQLException if a database access error occurs&lt;br /&gt;
     */&lt;br /&gt;
    public Object query(Connection conn, String sql, ResultSetHandler rsh,&lt;br /&gt;
            Object[] params) throws SQLException {&lt;br /&gt;
        PreparedStatement stmt = null;&lt;br /&gt;
        ResultSet rs = null;&lt;br /&gt;
        Object result = null;&lt;br /&gt;
        try {&lt;br /&gt;
            stmt = this.prepareStatement(conn, sql);&lt;br /&gt;
            this.fillStatement(stmt, params);&lt;br /&gt;
            rs = this.wrap(stmt.executeQuery());&lt;br /&gt;
            result = rsh.handle(rs);&lt;br /&gt;
        } catch (SQLException e) {&lt;br /&gt;
            this.rethrow(e, sql, params);&lt;br /&gt;
        } finally {&lt;br /&gt;
             rs.close();&lt;br /&gt;
             stmt.close();&lt;br /&gt;
        }&lt;br /&gt;
        return result;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Execute an SQL SELECT query without any replacement parameters.  The&lt;br /&gt;
     * caller is responsible for closing the connection.&lt;br /&gt;
     * &lt;br /&gt;
     * @param conn The connection to execute the query in.&lt;br /&gt;
     * @param sql The query to execute.&lt;br /&gt;
     * @param rsh The handler that converts the results into an object.&lt;br /&gt;
     * @return The object returned by the handler.&lt;br /&gt;
     * @throws SQLException if a database access error occurs&lt;br /&gt;
     */&lt;br /&gt;
    public Object query(Connection conn, String sql, ResultSetHandler rsh)&lt;br /&gt;
        throws SQLException {&lt;br /&gt;
        return this.query(conn, sql, rsh, (Object[]) null);&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Executes the given SELECT SQL with a single replacement parameter.&lt;br /&gt;
     * The &amp;lt;code&amp;gt;Connection&amp;lt;/code&amp;gt; is retrieved from the&lt;br /&gt;
     * &amp;lt;code&amp;gt;DataSource&amp;lt;/code&amp;gt; set in the constructor.&lt;br /&gt;
     * &lt;br /&gt;
     * @param sql The SQL statement to execute.&lt;br /&gt;
     * @param param The replacement parameter.&lt;br /&gt;
     * @param rsh The handler used to create the result object from &lt;br /&gt;
     * the &amp;lt;code&amp;gt;ResultSet&amp;lt;/code&amp;gt;.&lt;br /&gt;
     * &lt;br /&gt;
     * @return An object generated by the handler.&lt;br /&gt;
     * @throws SQLException if a database access error occurs&lt;br /&gt;
     * @deprecated Use {@link #query(String,ResultSetHandler,Object[])} instead&lt;br /&gt;
     */&lt;br /&gt;
    public Object query(String sql, Object param, ResultSetHandler rsh)&lt;br /&gt;
        throws SQLException {&lt;br /&gt;
        return this.query(sql, rsh, new Object[] { param });&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Executes the given SELECT SQL query and returns a result object.&lt;br /&gt;
     * The &amp;lt;code&amp;gt;Connection&amp;lt;/code&amp;gt; is retrieved from the &lt;br /&gt;
     * &amp;lt;code&amp;gt;DataSource&amp;lt;/code&amp;gt; set in the constructor.&lt;br /&gt;
     * &lt;br /&gt;
     * @param sql The SQL statement to execute.&lt;br /&gt;
     * @param params Initialize the PreparedStatement&amp;quot;s IN parameters with &lt;br /&gt;
     * this array.&lt;br /&gt;
     * &lt;br /&gt;
     * @param rsh The handler used to create the result object from &lt;br /&gt;
     * the &amp;lt;code&amp;gt;ResultSet&amp;lt;/code&amp;gt;.&lt;br /&gt;
     * &lt;br /&gt;
     * @return An object generated by the handler.&lt;br /&gt;
     * @throws SQLException if a database access error occurs&lt;br /&gt;
     * @deprecated Use {@link #query(String,ResultSetHandler,Object[])} instead&lt;br /&gt;
     */&lt;br /&gt;
    public Object query(String sql, Object[] params, ResultSetHandler rsh)&lt;br /&gt;
        throws SQLException {&lt;br /&gt;
            return query(sql, rsh, params);&lt;br /&gt;
        }&lt;br /&gt;
    /**&lt;br /&gt;
     * Executes the given SELECT SQL query and returns a result object.&lt;br /&gt;
     * The &amp;lt;code&amp;gt;Connection&amp;lt;/code&amp;gt; is retrieved from the &lt;br /&gt;
     * &amp;lt;code&amp;gt;DataSource&amp;lt;/code&amp;gt; set in the constructor.&lt;br /&gt;
     * &lt;br /&gt;
     * @param sql The SQL statement to execute.&lt;br /&gt;
     * @param rsh The handler used to create the result object from &lt;br /&gt;
     * the &amp;lt;code&amp;gt;ResultSet&amp;lt;/code&amp;gt;.&lt;br /&gt;
     * @param params Initialize the PreparedStatement&amp;quot;s IN parameters with &lt;br /&gt;
     * this array.&lt;br /&gt;
     * @return An object generated by the handler.&lt;br /&gt;
     * @throws SQLException if a database access error occurs&lt;br /&gt;
     */&lt;br /&gt;
    public Object query(String sql, ResultSetHandler rsh, Object[] params)&lt;br /&gt;
        throws SQLException {&lt;br /&gt;
        Connection conn = this.prepareConnection();&lt;br /&gt;
        try {&lt;br /&gt;
            return this.query(conn, sql, rsh, params);&lt;br /&gt;
        } finally {&lt;br /&gt;
            conn.close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Executes the given SELECT SQL without any replacement parameters.&lt;br /&gt;
     * The &amp;lt;code&amp;gt;Connection&amp;lt;/code&amp;gt; is retrieved from the&lt;br /&gt;
     * &amp;lt;code&amp;gt;DataSource&amp;lt;/code&amp;gt; set in the constructor.&lt;br /&gt;
     * &lt;br /&gt;
     * @param sql The SQL statement to execute.&lt;br /&gt;
     * @param rsh The handler used to create the result object from &lt;br /&gt;
     * the &amp;lt;code&amp;gt;ResultSet&amp;lt;/code&amp;gt;.&lt;br /&gt;
     * &lt;br /&gt;
     * @return An object generated by the handler.&lt;br /&gt;
     * @throws SQLException if a database access error occurs&lt;br /&gt;
     */&lt;br /&gt;
    public Object query(String sql, ResultSetHandler rsh) throws SQLException {&lt;br /&gt;
        return this.query(sql, rsh, (Object[]) null);&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Throws a new exception with a more informative error message.&lt;br /&gt;
     * &lt;br /&gt;
     * @param cause The original exception that will be chained to the new &lt;br /&gt;
     * exception when it&amp;quot;s rethrown. &lt;br /&gt;
     * &lt;br /&gt;
     * @param sql The query that was executing when the exception happened.&lt;br /&gt;
     * &lt;br /&gt;
     * @param params The query replacement parameters; &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; is a &lt;br /&gt;
     * valid value to pass in.&lt;br /&gt;
     * &lt;br /&gt;
     * @throws SQLException if a database access error occurs&lt;br /&gt;
     */&lt;br /&gt;
    protected void rethrow(SQLException cause, String sql, Object[] params)&lt;br /&gt;
        throws SQLException {&lt;br /&gt;
        String causeMessage = cause.getMessage();&lt;br /&gt;
        if (causeMessage == null) {&lt;br /&gt;
            causeMessage = &amp;quot;&amp;quot;;&lt;br /&gt;
        }&lt;br /&gt;
        StringBuffer msg = new StringBuffer(causeMessage);&lt;br /&gt;
        msg.append(&amp;quot; Query: &amp;quot;);&lt;br /&gt;
        msg.append(sql);&lt;br /&gt;
        msg.append(&amp;quot; Parameters: &amp;quot;);&lt;br /&gt;
        if (params == null) {&lt;br /&gt;
            msg.append(&amp;quot;[]&amp;quot;);&lt;br /&gt;
        } else {&lt;br /&gt;
            msg.append(Arrays.asList(params));&lt;br /&gt;
        }&lt;br /&gt;
        SQLException e = new SQLException(msg.toString(), cause.getSQLState(),&lt;br /&gt;
                cause.getErrorCode());&lt;br /&gt;
        e.setNextException(cause);&lt;br /&gt;
        throw e;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Execute an SQL INSERT, UPDATE, or DELETE query without replacement&lt;br /&gt;
     * parameters.&lt;br /&gt;
     * &lt;br /&gt;
     * @param conn The connection to use to run the query.&lt;br /&gt;
     * @param sql The SQL to execute.&lt;br /&gt;
     * @return The number of rows updated.&lt;br /&gt;
     * @throws SQLException if a database access error occurs&lt;br /&gt;
     */&lt;br /&gt;
    public int update(Connection conn, String sql) throws SQLException {&lt;br /&gt;
        return this.update(conn, sql, (Object[]) null);&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Execute an SQL INSERT, UPDATE, or DELETE query with a single replacement&lt;br /&gt;
     * parameter.&lt;br /&gt;
     * &lt;br /&gt;
     * @param conn The connection to use to run the query.&lt;br /&gt;
     * @param sql The SQL to execute.&lt;br /&gt;
     * @param param The replacement parameter.&lt;br /&gt;
     * @return The number of rows updated.&lt;br /&gt;
     * @throws SQLException if a database access error occurs&lt;br /&gt;
     */&lt;br /&gt;
    public int update(Connection conn, String sql, Object param)&lt;br /&gt;
        throws SQLException {&lt;br /&gt;
        return this.update(conn, sql, new Object[] { param });&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Execute an SQL INSERT, UPDATE, or DELETE query.&lt;br /&gt;
     * &lt;br /&gt;
     * @param conn The connection to use to run the query.&lt;br /&gt;
     * @param sql The SQL to execute.&lt;br /&gt;
     * @param params The query replacement parameters.&lt;br /&gt;
     * @return The number of rows updated.&lt;br /&gt;
     * @throws SQLException if a database access error occurs&lt;br /&gt;
     */&lt;br /&gt;
    public int update(Connection conn, String sql, Object[] params)&lt;br /&gt;
        throws SQLException {&lt;br /&gt;
        PreparedStatement stmt = null;&lt;br /&gt;
        int rows = 0;&lt;br /&gt;
        try {&lt;br /&gt;
            stmt = this.prepareStatement(conn, sql);&lt;br /&gt;
            this.fillStatement(stmt, params);&lt;br /&gt;
            rows = stmt.executeUpdate();&lt;br /&gt;
        } catch (SQLException e) {&lt;br /&gt;
            this.rethrow(e, sql, params);&lt;br /&gt;
        } finally {&lt;br /&gt;
            stmt.close();&lt;br /&gt;
        }&lt;br /&gt;
        return rows;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Executes the given INSERT, UPDATE, or DELETE SQL statement without&lt;br /&gt;
     * any replacement parameters. The &amp;lt;code&amp;gt;Connection&amp;lt;/code&amp;gt; is retrieved &lt;br /&gt;
     * from the &amp;lt;code&amp;gt;DataSource&amp;lt;/code&amp;gt; set in the constructor.  This &lt;br /&gt;
     * &amp;lt;code&amp;gt;Connection&amp;lt;/code&amp;gt; must be in auto-commit mode or the update will &lt;br /&gt;
     * not be saved. &lt;br /&gt;
     * &lt;br /&gt;
     * @param sql The SQL statement to execute.&lt;br /&gt;
     * @throws SQLException if a database access error occurs&lt;br /&gt;
     * @return The number of rows updated.&lt;br /&gt;
     */&lt;br /&gt;
    public int update(String sql) throws SQLException {&lt;br /&gt;
        return this.update(sql, (Object[]) null);&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Executes the given INSERT, UPDATE, or DELETE SQL statement with&lt;br /&gt;
     * a single replacement parameter.  The &amp;lt;code&amp;gt;Connection&amp;lt;/code&amp;gt; is &lt;br /&gt;
     * retrieved from the &amp;lt;code&amp;gt;DataSource&amp;lt;/code&amp;gt; set in the constructor.&lt;br /&gt;
     * This &amp;lt;code&amp;gt;Connection&amp;lt;/code&amp;gt; must be in auto-commit mode or the &lt;br /&gt;
     * update will not be saved. &lt;br /&gt;
     * &lt;br /&gt;
     * @param sql The SQL statement to execute.&lt;br /&gt;
     * @param param The replacement parameter.&lt;br /&gt;
     * @throws SQLException if a database access error occurs&lt;br /&gt;
     * @return The number of rows updated.&lt;br /&gt;
     */&lt;br /&gt;
    public int update(String sql, Object param) throws SQLException {&lt;br /&gt;
        return this.update(sql, new Object[] { param });&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Executes the given INSERT, UPDATE, or DELETE SQL statement.  The &lt;br /&gt;
     * &amp;lt;code&amp;gt;Connection&amp;lt;/code&amp;gt; is retrieved from the &amp;lt;code&amp;gt;DataSource&amp;lt;/code&amp;gt; &lt;br /&gt;
     * set in the constructor.  This &amp;lt;code&amp;gt;Connection&amp;lt;/code&amp;gt; must be in &lt;br /&gt;
     * auto-commit mode or the update will not be saved. &lt;br /&gt;
     * &lt;br /&gt;
     * @param sql The SQL statement to execute.&lt;br /&gt;
     * @param params Initializes the PreparedStatement&amp;quot;s IN (i.e. &amp;quot;?&amp;quot;) &lt;br /&gt;
     * parameters.&lt;br /&gt;
     * @throws SQLException if a database access error occurs&lt;br /&gt;
     * @return The number of rows updated.&lt;br /&gt;
     */&lt;br /&gt;
    public int update(String sql, Object[] params) throws SQLException {&lt;br /&gt;
        Connection conn = this.prepareConnection();&lt;br /&gt;
        try {&lt;br /&gt;
            return this.update(conn, sql, params);&lt;br /&gt;
        } finally {&lt;br /&gt;
            conn.close();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * Wrap the &amp;lt;code&amp;gt;ResultSet&amp;lt;/code&amp;gt; in a decorator before processing it.&lt;br /&gt;
     * This implementation returns the &amp;lt;code&amp;gt;ResultSet&amp;lt;/code&amp;gt; it is given&lt;br /&gt;
     * without any decoration.&lt;br /&gt;
     *&lt;br /&gt;
     * &amp;lt;p&amp;gt;&lt;br /&gt;
     * Often, the implementation of this method can be done in an anonymous &lt;br /&gt;
     * inner class like this:&lt;br /&gt;
     * &amp;lt;/p&amp;gt;&lt;br /&gt;
     * &amp;lt;pre&amp;gt; &lt;br /&gt;
     * QueryRunner run = new QueryRunner() {&lt;br /&gt;
     *     protected ResultSet wrap(ResultSet rs) {&lt;br /&gt;
     *         return StringTrimmedResultSet.wrap(rs);&lt;br /&gt;
     *     }&lt;br /&gt;
     * };&lt;br /&gt;
     * &amp;lt;/pre&amp;gt;&lt;br /&gt;
     * &lt;br /&gt;
     * @param rs The &amp;lt;code&amp;gt;ResultSet&amp;lt;/code&amp;gt; to decorate; never &lt;br /&gt;
     * &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt;.&lt;br /&gt;
     * @return The &amp;lt;code&amp;gt;ResultSet&amp;lt;/code&amp;gt; wrapped in some decorator. &lt;br /&gt;
     */&lt;br /&gt;
    protected ResultSet wrap(ResultSet rs) {&lt;br /&gt;
        return rs;&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
interface ResultSetHandler {&lt;br /&gt;
  /**&lt;br /&gt;
   * Turn the &amp;lt;code&amp;gt;ResultSet&amp;lt;/code&amp;gt; into an Object.&lt;br /&gt;
   * &lt;br /&gt;
   * @param rs The &amp;lt;code&amp;gt;ResultSet&amp;lt;/code&amp;gt; to handle.  It has not been touched&lt;br /&gt;
   * before being passed to this method.&lt;br /&gt;
   * &lt;br /&gt;
   * @return An Object initialized with &amp;lt;code&amp;gt;ResultSet&amp;lt;/code&amp;gt; data. It is&lt;br /&gt;
   * legal for implementations to return &amp;lt;code&amp;gt;null&amp;lt;/code&amp;gt; if the &lt;br /&gt;
   * &amp;lt;code&amp;gt;ResultSet&amp;lt;/code&amp;gt; contained 0 rows.&lt;br /&gt;
   * &lt;br /&gt;
   * @throws SQLException if a database access error occurs&lt;br /&gt;
   */&lt;br /&gt;
  public Object handle(ResultSet rs) throws SQLException;&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Retrieving All Rows from a Database Table ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
import java.sql.Connection;&lt;br /&gt;
import java.sql.DriverManager;&lt;br /&gt;
import java.sql.ResultSet;&lt;br /&gt;
import java.sql.Statement;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] argv) throws Exception {&lt;br /&gt;
    String driver = &amp;quot;com.mysql.jdbc.Driver&amp;quot;;&lt;br /&gt;
    String user = &amp;quot;root&amp;quot;;&lt;br /&gt;
    String pass = &amp;quot;root&amp;quot;;&lt;br /&gt;
    Class.forName(driver).newInstance();&lt;br /&gt;
    Connection con = DriverManager.getConnection(&amp;quot;jdbc:mysql://localhost:3306/jdbctutorial&amp;quot;, user, pass);&lt;br /&gt;
    Statement st = con.createStatement();&lt;br /&gt;
    ResultSet res = st.executeQuery(&amp;quot;SELECT * FROM  emp&amp;quot;);&lt;br /&gt;
    while (res.next()) {&lt;br /&gt;
      int i = res.getInt(&amp;quot;ID&amp;quot;);&lt;br /&gt;
      String s = res.getString(&amp;quot;name&amp;quot;);&lt;br /&gt;
      System.out.println(i + &amp;quot;\t\t&amp;quot; + s);&lt;br /&gt;
    }&lt;br /&gt;
    con.close();&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
			</entry>

	</feed>