{"id":135,"date":"2018-06-25T10:37:43","date_gmt":"2018-06-25T02:37:43","guid":{"rendered":"http:\/\/chenhaitao.me\/?p=135"},"modified":"2018-06-25T10:37:43","modified_gmt":"2018-06-25T02:37:43","slug":"%e5%b8%a6%e4%ba%8b%e5%8a%a1%e7%9a%84queryrunner","status":"publish","type":"post","link":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/2018\/06\/25\/%e5%b8%a6%e4%ba%8b%e5%8a%a1%e7%9a%84queryrunner\/","title":{"rendered":"\u5e26\u4e8b\u52a1\u7684QueryRunner"},"content":{"rendered":"<p>DBUtils\u7b80\u5316\u4e86\u5bf9\u6570\u636e\u5e93\u7684\u64cd\u4f5c\uff0c\u518d\u52a0\u4e0a\u6211\u4eec\u4e0a\u4e00\u8282\u8bb2\u7684JdbcUtils\uff0c\u4e24\u8005\u914d\u5408\u8d77\u6765\u7528\uff0c\u4e5f\u662f\u975e\u5e38\u65b9\u4fbf\uff0c\u4f46\u662f\u64cd\u4f5c\u5728\u64cd\u4f5c\u4e8b\u52a1\u65f6\uff0c\u4e5f\u4f1a\u4ea7\u751f\u4e00\u4e9b\u5197\u4f59\u7684\u4ee3\u7801\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<p>Jdbcutils<\/p>\n<p>package cn.ccnu.jdbc;<\/p>\n<p>import java.sql.Connection;<br \/>\nimport java.sql.SQLException;<\/p>\n<p>import javax.sql.DataSource;<br \/>\nimport com.mchange.v2.c3p0.ComboPooledDataSource;<\/p>\n<p>public class JdbcUtils {<br \/>\n\/\/\u901a\u8fc7c3p0\u8fde\u63a5\u6c60\u5f97\u5230DataSource<br \/>\nprivate static DataSource ds = new ComboPooledDataSource();<br \/>\n\/\/\u5b9a\u4e49\u4e00\u4e2aConnection\u6765\u5224\u65ad\u662f\u5426\u6709\u4e8b\u52a1<br \/>\nprivate static Connection con = null;<br \/>\n\/\/\u8fd4\u56deDataSource<br \/>\npublic static DataSource getDataSource(){<br \/>\nreturn ds;<br \/>\n}<\/p>\n<p>\/\/\u901a\u8fc7DataSource\u5f97\u5230Connection<br \/>\npublic static Connection getConnection() throws SQLException{<br \/>\n\/\/\u5982\u679c\u5f00\u542f\u4e86\u4e8b\u52a1\uff0c\u5219con\u4e0d\u4e3a\u7a7a\uff0c\u5e94\u8be5\u76f4\u63a5\u8fd4\u56decon<br \/>\nif(con != null){<br \/>\nreturn con;<br \/>\n}<br \/>\nreturn ds.getConnection();<br \/>\n}<\/p>\n<p>\/\/ \u5f00\u542f\u4e8b\u52a1<br \/>\npublic static void beginTransaction() throws SQLException {<br \/>\n\/\/\u5224\u65adcon\u662f\u5426\u4e3a\u7a7a\uff0c\u5982\u679c\u4e0d\u4e3a\u7a7a\uff0c\u5219\u8bf4\u660e\u4e8b\u52a1\u5df2\u7ecf\u5f00\u542f<br \/>\nif(con != null){<br \/>\nthrow new SQLException(&#8220;\u4e8b\u52a1\u5df2\u7ecf\u5f00\u542f\u4e86,\u4e0d\u80fd\u91cd\u590d\u5f00\u542f\u4e8b\u52a1&#8221;);<br \/>\n}<br \/>\n\/\/\u5982\u679c\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5f00\u542f\u4e8b\u52a1<br \/>\ncon = getConnection();<br \/>\n\/\/\u8bbe\u7f6e\u4e8b\u52a1\u63d0\u4ea4\u4e3a\u624b\u52a8<br \/>\ncon.setAutoCommit(false);<br \/>\n}<\/p>\n<p>\/\/ \u63d0\u4ea4\u4e8b\u52a1<br \/>\npublic static void commitTransaction() throws SQLException {<br \/>\n\/\/\u5224\u65adcon\u662f\u5426\u4e3a\u7a7a\uff0c\u5982\u679c\u4e3a\u7a7a\uff0c\u5219\u8bf4\u660e\u6ca1\u6709\u5f00\u542f\u4e8b\u52a1<br \/>\nif(con == null){<br \/>\nthrow new SQLException(&#8220;\u6ca1\u6709\u5f00\u542f\u4e8b\u52a1,\u4e0d\u80fd\u63d0\u4ea4\u4e8b\u52a1&#8221;);<br \/>\n}<br \/>\n\/\/\u5982\u679ccon\u4e0d\u4e3a\u7a7a,\u63d0\u4ea4\u4e8b\u52a1<br \/>\ncon.commit();<br \/>\n\/\/\u4e8b\u52a1\u63d0\u4ea4\u540e\uff0c\u5173\u95ed\u8fde\u63a5<br \/>\ncon.close();<br \/>\n\/\/\u8868\u793a\u4e8b\u52a1\u5df2\u7ecf\u7ed3\u675f<br \/>\ncon = null;<br \/>\n}<\/p>\n<p>\/\/ \u56de\u6eda\u4e8b\u52a1<br \/>\npublic static void rollbackTransaction() throws SQLException {<br \/>\n\/\/\u5224\u65adcon\u662f\u5426\u4e3a\u7a7a\uff0c\u5982\u679c\u4e3a\u7a7a\uff0c\u5219\u8bf4\u660e\u6ca1\u6709\u5f00\u542f\u4e8b\u52a1\uff0c\u4e5f\u5c31\u4e0d\u80fd\u56de\u6eda\u4e8b\u52a1<br \/>\nif(con == null){<br \/>\nthrow new SQLException(&#8220;\u6ca1\u6709\u5f00\u542f\u4e8b\u52a1,\u4e0d\u80fd\u56de\u6eda\u4e8b\u52a1&#8221;);<br \/>\n}<br \/>\n\/\/\u4e8b\u52a1\u56de\u6eda<br \/>\ncon.rollback();<br \/>\n\/\/\u4e8b\u52a1\u56de\u6eda\u540e\uff0c\u5173\u95ed\u8fde\u63a5<br \/>\ncon.close();<br \/>\n\/\/\u628acon\u7f6e\u4e3anull\uff0c\u8868\u793a\u4e8b\u52a1\u5df2\u7ecf\u7ed3\u675f<br \/>\ncon = null;<br \/>\n}<\/p>\n<p>\/\/ \u5173\u95ed\u4e8b\u52a1<br \/>\npublic static void releaseConnection(Connection connection) throws SQLException {<br \/>\n\/\/\u5982\u679c\u53c2\u6570\u8fde\u63a5\u4e0e\u5f53\u524d\u4e8b\u52a1\u8fde\u63a5\u4e0d\u76f8\u7b49\uff0c\u5219\u8bf4\u660e\u53c2\u6570\u8fde\u63a5\u4e0d\u662f\u4e8b\u52a1\u8fde\u63a5\uff0c\u53ef\u4ee5\u5173\u95ed\uff0c\u5426\u5219\u7531\u4e8b\u52a1\u5173\u95ed<br \/>\nif(connection != null &amp;&amp; con != connection){<br \/>\n\/\/\u5982\u679c\u8fde\u63a5\u6ca1\u6709\u88ab\u5173\u95ed\uff0c\u5173\u95ed\u4e4b<br \/>\nif(!connection.isClosed()){<br \/>\nconnection.close();<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p>\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n<p>package cn.ccnu.jdbc;<\/p>\n<p>import java.sql.Connection;<br \/>\nimport java.sql.PreparedStatement;<br \/>\nimport java.sql.SQLException;<\/p>\n<p>import org.apache.commons.dbutils.QueryRunner;<br \/>\nimport org.junit.Test;<\/p>\n<p>public class JdbcUtilsTest {<br \/>\n@Test<br \/>\npublic void test1() throws SQLException {<br \/>\nString sql = &#8220;update user set salary = ? where id = ?&#8221;;<br \/>\nQueryRunner qr = new QueryRunner();<br \/>\nConnection conn = JdbcUtils.getConnection();<br \/>\nqr.update(conn, sql, &#8220;800&#8221;, 3);<br \/>\nJdbcUtils.releaseConnection(conn);<br \/>\n}<\/p>\n<p>\/\/ \u5bf9\u4e8b\u52a1\u7684\u6d4b\u8bd5\u4e4b\u6ca1\u6709\u56de\u6eda<br \/>\n@Test<br \/>\npublic void test2() throws SQLException {<br \/>\nJdbcUtils.beginTransaction();<br \/>\nConnection conn = JdbcUtils.getConnection();<br \/>\nQueryRunner qr = new QueryRunner();<br \/>\nString sql1 = &#8220;update user set salary = salary + ? where id = 1;&#8221;;<br \/>\nString sql2 = &#8220;update user set salary = salary &#8211; ? where id = 2;&#8221;;<br \/>\nqr.update(conn, sql1, 200);<br \/>\nqr.update(conn, sql2, 200);<br \/>\nJdbcUtils.commitTransaction();<br \/>\nJdbcUtils.releaseConnection(conn);<br \/>\n}<\/p>\n<p>\/\/ \u5bf9\u4e8b\u52a1\u7684\u6d4b\u8bd5\u4e4b\u6709\u4f1a\u56de\u6eda<br \/>\n@Test<br \/>\npublic void test3() {<br \/>\ntry {<br \/>\n\/\/\u5f00\u542f\u4e8b\u52a1<br \/>\nJdbcUtils.beginTransaction();<br \/>\n\/\/\u5f97\u5230\u8fde\u63a5<br \/>\nConnection conn = JdbcUtils.getConnection();<br \/>\n\/\/\u5f97\u5230QueryRunner<br \/>\nQueryRunner qr = new QueryRunner();<\/p>\n<p>String sql1 = &#8220;update user set salary = salary + ? where id = 1;&#8221;;<br \/>\nString sql2 = &#8220;update user set salary = salary &#8211; ? where id = 2;&#8221;;<br \/>\n\/\/\u6267\u884c\u7b2c\u4e00\u6761sql<br \/>\nqr.update(conn, sql1, 100);<br \/>\n\/\/\u8ba4\u4e3a\u5236\u9020\u5f02\u5e38<br \/>\nint b = 1\/0;<br \/>\nqr.update(conn, sql2, 100);<br \/>\n\/\/\u63d0\u4ea4\u4e8b\u52a1<br \/>\nJdbcUtils.commitTransaction();<br \/>\n\/\/\u5173\u95ed\u8fde\u63a5\uff0c\u5728\u5904\u7406\u4e8b\u52a1\u65f6\uff0c\u8fd9\u53e5\u53ef\u4ee5\u4e0d\u5199\uff0c\u56e0\u4e3a\u5728\u63d0\u4ea4\u6216\u56de\u6eda\u65f6\u4f1a\u5173\u95ed\u8fde\u63a5<br \/>\nJdbcUtils.releaseConnection(conn);<br \/>\n} catch (Exception e) {<br \/>\ntry {<br \/>\nJdbcUtils.rollbackTransaction();<br \/>\n} catch (SQLException e1) {<br \/>\ne1.printStackTrace();<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p>\u4ece\u4e0a\u9762\u7684\u4ee3\u7801\u793a\u4f8b\u53ef\u4ee5\u770b\u51fa\uff0c\u529f\u80fd\u53ef\u4ee5\u88ab\u5b9e\u73b0\uff0c\u4f46\u662f\u4ea7\u751f\u4e86\u4ee3\u7801\u5197\u4f59\uff0c\u6bd4\u5982\u8bf4\uff0c\u6bcf\u6b21\u90fd\u8981\u5f97\u5230\u8fde\u63a5\uff0c\u518d\u628a\u8fde\u63a5\u4f5c\u4e3a\u53c2\u6570\u4f20\u7ed9QueryRunner\uff0c\u6700\u540e\u5173\u95ed\u8fde\u63a5\u3002\u6bcf\u6b21\u4e8b\u52a1\u64cd\u4f5c\u65f6\uff0c\u90fd\u8981\u8fdb\u884c\u8fd9\u51e0\u6b65\uff0c\u90a3\u6211\u4eec\uff0c\u662f\u4e0d\u662f\u53ef\u4ee5\u628a\u8fd9\u51e0\u6b65\u62bd\u53d6\u51fa\u6765\uff1f\uff1f\uff1f\u6211\u4eec\u7684\u89e3\u51b3\u529e\u6cd5\u662f\uff0c\u91cd\u5199\u4e00\u4e2a\u7c7bTSQueryRunner\u6765\u7ee7\u627fQueryRunner\uff0c\u7136\u540e\u8986\u76d6\u5176\u4e2d\u5e26\u6709Connection\u7684\u65b9\u6cd5\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u4e0d\u7528\u518d\u4f20Connection\u8fdb\u53bb\u4e86\u3002\u4f46\u95ee\u9898\u5c31\u5728\u4e8e\u5f00\u542f\u4e8b\u52a1\uff0c\u63d0\u4ea4\u4e8b\u52a1\uff0c\u56de\u6eda\u4e8b\u52a1\u65f6\uff0c\u6240\u6709\u7684\u8fde\u63a5\u8981\u662f\u540c\u4e00\u4e2a\u8fde\u63a5\uff0c\u6211\u4eec\u5982\u4f55\u4fdd\u8bc1\u5462\uff1f\u6211\u4eec\u6240\u6709\u7684\u65b9\u6cd5\u662f\uff0c\u5728JdbcUtils\u4e2d\u5728\u5f00\u542f\u4e8b\u52a1\u65f6\u5f97\u5230\u7684\u7684Connection\u5b58\u653e\u5728ThreadLocal\u4e2d\uff0c\u8fd9\u6837\u63d0\u4ea4\u4e8b\u52a1\uff0c\u56de\u6eda\u4e8b\u52a1\u65f6\u6240\u6709\u7684\u8fde\u63a5\u5c31\u662f\u540c\u4e00\u4e2a\u8fde\u63a5\u4e86\u3002<\/p>\n<p>TSQueryRunner<\/p>\n<p>package cn.ccnu.queryrunner;<\/p>\n<p>import java.sql.Connection;<br \/>\nimport java.sql.SQLException;<\/p>\n<p>import org.apache.commons.dbutils.QueryRunner;<br \/>\nimport org.apache.commons.dbutils.ResultSetHandler;<\/p>\n<p>public class TSQueryRunner extends QueryRunner {<\/p>\n<p>@Override<br \/>\npublic int[] batch(String sql, Object[][] params)<br \/>\nthrows SQLException {<br \/>\nConnection conn = JdbcUtils.getConnection();<br \/>\nint[] result = super.batch(conn, sql, params);<br \/>\nJdbcUtils.releaseConnection(conn);<br \/>\nreturn result;<br \/>\n}<\/p>\n<p>@Override<br \/>\npublic &lt;T&gt; T query(String sql, ResultSetHandler&lt;T&gt; rsh,<br \/>\nObject&#8230; params) throws SQLException {<br \/>\nConnection conn = JdbcUtils.getConnection();<br \/>\nT result = super.query(conn, sql, rsh, params);<br \/>\nJdbcUtils.releaseConnection(conn);<br \/>\nreturn result;<br \/>\n}<\/p>\n<p>@Override<br \/>\npublic &lt;T&gt; T query(String sql, ResultSetHandler&lt;T&gt; rsh)<br \/>\nthrows SQLException {<br \/>\nConnection conn = JdbcUtils.getConnection();<br \/>\nT result = super.query(conn, sql, rsh);<br \/>\nJdbcUtils.releaseConnection(conn);<br \/>\nreturn result;<br \/>\n}<\/p>\n<p>@Override<br \/>\npublic int update(String sql, Object&#8230; params)<br \/>\nthrows SQLException {<br \/>\nConnection conn = JdbcUtils.getConnection();<br \/>\nint result = super.update(conn, sql, params);<br \/>\nJdbcUtils.releaseConnection(conn);<br \/>\nreturn result;<br \/>\n}<\/p>\n<p>@Override<br \/>\npublic int update(String sql, Object param)<br \/>\nthrows SQLException {<br \/>\nConnection conn = JdbcUtils.getConnection();<br \/>\nint result = super.update(conn, sql, param);<br \/>\nJdbcUtils.releaseConnection(conn);<br \/>\nreturn result;<br \/>\n}<\/p>\n<p>@Override<br \/>\npublic int update(String sql) throws SQLException {<br \/>\nConnection conn = JdbcUtils.getConnection();<br \/>\nint result = super.update(conn, sql);<br \/>\nJdbcUtils.releaseConnection(conn);<br \/>\nreturn result;<br \/>\n}<\/p>\n<p>}<\/p>\n<p>JdbcUtils<\/p>\n<p>package cn.ccnu.queryrunner;<\/p>\n<p>import java.sql.Connection;<br \/>\nimport java.sql.SQLException;<\/p>\n<p>import javax.sql.DataSource;<br \/>\nimport com.mchange.v2.c3p0.ComboPooledDataSource;<\/p>\n<p>public class JdbcUtils {<br \/>\n\/\/\u901a\u8fc7c3p0\u8fde\u63a5\u6c60\u5f97\u5230DataSource<br \/>\nprivate static DataSource ds = new ComboPooledDataSource();<br \/>\n\/\/\u5b9a\u4e49\u4e00\u4e2aConnection\u6765\u5224\u65ad\u662f\u5426\u6709\u4e8b\u52a1,\u5c06con\u653e\u5728ThreadLocal\u4e2d<br \/>\n\/\/\u90a3\u4e48QueryRunner\u5728\u4f7f\u7528\u4e8b\u52a1\u65f6\uff0c\u5c31\u53ef\u4ee5\u4e0d\u7528\u4f20con\uff0c\u800c\u4ea4\u7531util\u63a7\u5236<br \/>\nprivate static ThreadLocal&lt;Connection&gt; conn = new ThreadLocal&lt;Connection&gt;();<br \/>\n\/\/\u8fd4\u56deDataSource<br \/>\npublic static DataSource getDataSource(){<br \/>\nreturn ds;<br \/>\n}<\/p>\n<p>\/\/\u901a\u8fc7DataSource\u5f97\u5230Connection<br \/>\npublic static Connection getConnection() throws SQLException{<br \/>\n\/\/\u5f97\u5230ThreadLocal\u4e2d\u7684connection<br \/>\nConnection con = conn.get();<br \/>\n\/\/\u5982\u679c\u5f00\u542f\u4e86\u4e8b\u52a1\uff0c\u5219con\u4e0d\u4e3a\u7a7a\uff0c\u5e94\u8be5\u76f4\u63a5\u8fd4\u56decon<br \/>\nif(con != null){<br \/>\nreturn con;<br \/>\n}<br \/>\nreturn ds.getConnection();<br \/>\n}<\/p>\n<p>\/\/ \u5f00\u542f\u4e8b\u52a1<br \/>\npublic static void beginTransaction() throws SQLException {<br \/>\n\/\/\u5f97\u5230ThreadLocal\u4e2d\u7684connection<br \/>\nConnection con = conn.get();<br \/>\n\/\/\u5224\u65adcon\u662f\u5426\u4e3a\u7a7a\uff0c\u5982\u679c\u4e0d\u4e3a\u7a7a\uff0c\u5219\u8bf4\u660e\u4e8b\u52a1\u5df2\u7ecf\u5f00\u542f<br \/>\nif(con != null){<br \/>\nthrow new SQLException(&#8220;\u4e8b\u52a1\u5df2\u7ecf\u5f00\u542f\u4e86,\u4e0d\u80fd\u91cd\u590d\u5f00\u542f\u4e8b\u52a1&#8221;);<br \/>\n}<br \/>\n\/\/\u5982\u679c\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5f00\u542f\u4e8b\u52a1<br \/>\ncon = getConnection();<br \/>\n\/\/\u8bbe\u7f6e\u4e8b\u52a1\u63d0\u4ea4\u4e3a\u624b\u52a8<br \/>\ncon.setAutoCommit(false);<br \/>\n\/\/\u628a\u5f53\u524d\u5f00\u542f\u7684\u4e8b\u52a1\u653e\u5165ThreadLocal\u4e2d<br \/>\nconn.set(con);<br \/>\n}<\/p>\n<p>\/\/ \u63d0\u4ea4\u4e8b\u52a1<br \/>\npublic static void commitTransaction() throws SQLException {<br \/>\n\/\/\u5f97\u5230ThreadLocal\u4e2d\u7684connection<br \/>\nConnection con = conn.get();<br \/>\n\/\/\u5224\u65adcon\u662f\u5426\u4e3a\u7a7a\uff0c\u5982\u679c\u4e3a\u7a7a\uff0c\u5219\u8bf4\u660e\u6ca1\u6709\u5f00\u542f\u4e8b\u52a1<br \/>\nif(con == null){<br \/>\nthrow new SQLException(&#8220;\u6ca1\u6709\u5f00\u542f\u4e8b\u52a1,\u4e0d\u80fd\u63d0\u4ea4\u4e8b\u52a1&#8221;);<br \/>\n}<br \/>\n\/\/\u5982\u679ccon\u4e0d\u4e3a\u7a7a,\u63d0\u4ea4\u4e8b\u52a1<br \/>\ncon.commit();<br \/>\n\/\/\u4e8b\u52a1\u63d0\u4ea4\u540e\uff0c\u5173\u95ed\u8fde\u63a5<br \/>\ncon.close();<br \/>\n\/\/\u5c06\u8fde\u63a5\u79fb\u51faThreadLocal<br \/>\nconn.remove();<br \/>\n}<\/p>\n<p>\/\/ \u56de\u6eda\u4e8b\u52a1<br \/>\npublic static void rollbackTransaction() throws SQLException {<br \/>\n\/\/\u5f97\u5230ThreadLocal\u4e2d\u7684connection<br \/>\nConnection con = conn.get();<br \/>\n\/\/\u5224\u65adcon\u662f\u5426\u4e3a\u7a7a\uff0c\u5982\u679c\u4e3a\u7a7a\uff0c\u5219\u8bf4\u660e\u6ca1\u6709\u5f00\u542f\u4e8b\u52a1\uff0c\u4e5f\u5c31\u4e0d\u80fd\u56de\u6eda\u4e8b\u52a1<br \/>\nif(con == null){<br \/>\nthrow new SQLException(&#8220;\u6ca1\u6709\u5f00\u542f\u4e8b\u52a1,\u4e0d\u80fd\u56de\u6eda\u4e8b\u52a1&#8221;);<br \/>\n}<br \/>\n\/\/\u4e8b\u52a1\u56de\u6eda<br \/>\ncon.rollback();<br \/>\n\/\/\u4e8b\u52a1\u56de\u6eda\u540e\uff0c\u5173\u95ed\u8fde\u63a5<br \/>\ncon.close();<br \/>\n\/\/\u5c06\u8fde\u63a5\u79fb\u51faThreadLocal<br \/>\nconn.remove();<br \/>\n}<\/p>\n<p>\/\/ \u5173\u95ed\u4e8b\u52a1<br \/>\npublic static void releaseConnection(Connection connection) throws SQLException {<br \/>\n\/\/\u5f97\u5230ThreadLocal\u4e2d\u7684connection<br \/>\nConnection con = conn.get();<br \/>\n\/\/\u5982\u679c\u53c2\u6570\u8fde\u63a5\u4e0e\u5f53\u524d\u4e8b\u52a1\u8fde\u63a5\u4e0d\u76f8\u7b49\uff0c\u5219\u8bf4\u660e\u53c2\u6570\u8fde\u63a5\u4e0d\u662f\u4e8b\u52a1\u8fde\u63a5\uff0c\u53ef\u4ee5\u5173\u95ed\uff0c\u5426\u5219\u4ea4\u7531\u4e8b\u52a1\u5173\u95ed<br \/>\nif(connection != null &amp;&amp; con != connection){<br \/>\n\/\/\u5982\u679c\u8fde\u63a5\u6ca1\u6709\u88ab\u5173\u95ed\uff0c\u5173\u95ed\u4e4b<br \/>\nif(!connection.isClosed()){<br \/>\nconnection.close();<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p>\u6d4b\u8bd5\u4ee3\u7801<\/p>\n<div class=\"dp-highlighter bg_java\">\n<div class=\"bar\">\n<p>package cn.ccnu.queryrunner;<\/p>\n<p>import java.sql.Connection;<br \/>\nimport java.sql.PreparedStatement;<br \/>\nimport java.sql.SQLException;<\/p>\n<p>import org.apache.commons.dbutils.QueryRunner;<br \/>\nimport org.junit.Test;<\/p>\n<p>public class JdbcUtilsTest {<br \/>\n\/\/ \u6ca1\u6709\u4e8b\u52a1\u7684\u6d4b\u8bd5<br \/>\n@Test<br \/>\npublic void test1() throws SQLException {<br \/>\nString sql = &#8220;update user set salary = ? where id = ?&#8221;;<br \/>\nQueryRunner qr = new TSQueryRunner();<br \/>\nqr.update(sql, 800, 3);<br \/>\n}<\/p>\n<p>\/\/ \u5bf9\u4e8b\u52a1\u7684\u6d4b\u8bd5\u4e4b\u6ca1\u6709\u56de\u6eda<br \/>\n@Test<br \/>\npublic void test2() throws SQLException {<br \/>\nJdbcUtils.beginTransaction();<br \/>\nQueryRunner qr = new TSQueryRunner();<br \/>\nString sql1 = &#8220;update user set salary = salary + ? where id = 1;&#8221;;<br \/>\nString sql2 = &#8220;update user set salary = salary &#8211; ? where id = 2;&#8221;;<br \/>\nqr.update(sql1, 200);<br \/>\nqr.update(sql2, 200);<br \/>\nJdbcUtils.commitTransaction();<br \/>\n}<\/p>\n<p>\/\/ \u5bf9\u4e8b\u52a1\u7684\u6d4b\u8bd5\u4e4b\u6709\u4f1a\u56de\u6eda<br \/>\n@Test<br \/>\npublic void test3() {<br \/>\ntry {<br \/>\n\/\/\u5f00\u542f\u4e8b\u52a1<br \/>\nJdbcUtils.beginTransaction();<br \/>\n\/\/\u5f97\u5230QueryRunner<br \/>\nQueryRunner qr = new TSQueryRunner();<br \/>\nString sql1 = &#8220;update user set salary = salary + ? where id = 1;&#8221;;<br \/>\nString sql2 = &#8220;update user set salary = salary &#8211; ? where id = 2;&#8221;;<br \/>\n\/\/\u6267\u884c\u7b2c\u4e00\u6761sql<br \/>\nqr.update(sql1, 100);<br \/>\n\/\/\u4eba\u4e3a\u5236\u9020\u5f02\u5e38<br \/>\nint b = 1\/0;<br \/>\nqr.update(sql2, 100);<br \/>\n\/\/\u63d0\u4ea4\u4e8b\u52a1<br \/>\nJdbcUtils.commitTransaction();<br \/>\n} catch (Exception e) {<br \/>\ntry {<br \/>\nJdbcUtils.rollbackTransaction();<br \/>\n} catch (SQLException e1) {<br \/>\ne1.printStackTrace();<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>DBUtils\u7b80\u5316\u4e86\u5bf9\u6570\u636e\u5e93\u7684\u64cd\u4f5c\uff0c\u518d\u52a0\u4e0a\u6211\u4eec\u4e0a\u4e00\u8282\u8bb2\u7684JdbcUtils\uff0c\u4e24\u8005\u914d\u5408\u8d77\u6765\u7528\uff0c\u4e5f\u662f\u975e\u5e38\u65b9\u4fbf\uff0c\u4f46\u662f\u64cd\u4f5c\u5728\u64cd\u4f5c\u4e8b\u52a1\u65f6\uff0c\u4e5f\u4f1a\u4ea7\u751f\u4e00\u4e9b\u5197\u4f59\u7684\u4ee3\u7801\u3002\u4ee3\u7801\u793a\u4f8b&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/chenhaitao.com\/.wp-toolkit_I\/2018\/06\/25\/%e5%b8%a6%e4%ba%8b%e5%8a%a1%e7%9a%84queryrunner\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[30],"tags":[11],"class_list":["post-135","post","type-post","status-publish","format-standard","hentry","category-30","tag-java"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9HWWB-2b","jetpack-related-posts":[{"id":38,"url":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/2018\/03\/10\/springboot%e6%95%b4%e5%90%88mybatis\/","url_meta":{"origin":135,"position":0},"title":"SpringBoot\u6574\u5408Mybatis","author":"\u6770\u514b\u9648","date":"2018\u5e743\u670810\u65e5","format":false,"excerpt":"\u65b9\u5f0f\u4e00\uff1a\u4f7f\u7528Annotation\u81ea\u52a8\u5316\u96c6\u6210 \u5f53\u6211\u6309\u7167\u7f51\u4e0a\u7684\u6559\u7a0b\u914d\u7f6e\u540e\uff0c\u5f88\u7b80\u5355\u561b\uff0c\u57fa\u672c\u4e0a\u5565\u4e5f\u4e0d\u7528\u5e72\uff0c\u914d\u2026","rel":"","context":"\u5728\u201c\u6280\u672f\u201d\u4e2d","block_context":{"text":"\u6280\u672f","link":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/category\/%e6%8a%80%e6%9c%af\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":84,"url":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/2018\/04\/11\/spring-boot%e4%bd%bf%e7%94%a8%ef%bc%9a%e5%ba%94%e7%94%a8%e5%90%af%e5%8a%a8%e5%90%8e%e5%81%9a%e7%9a%84%e4%b8%80%e4%ba%9b%e4%ba%8b\/","url_meta":{"origin":135,"position":1},"title":"Spring Boot\u4f7f\u7528\uff1a\u5e94\u7528\u542f\u52a8\u540e\u505a\u7684\u4e00\u4e9b\u4e8b","author":"\u6770\u514b\u9648","date":"2018\u5e744\u670811\u65e5","format":false,"excerpt":"\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5728\u5e94\u7528\u542f\u52a8\u6216\u8005\u91cd\u542f\u540e\u505a\u4e00\u4e9b\u5de5\u4f5c\uff0c\u6bd4\u5982\u5220\u9664\u4e00\u4e9b\u4e34\u65f6\u6587\u4ef6\u6216\u8005Redis\u4e2d\u7684\u7f13\u5b58\uff0c\u52a0\u8f7d\u4e00\u4e9b\u9891\u2026","rel":"","context":"\u5728\u201c\u5f00\u53d1\u201d\u4e2d","block_context":{"text":"\u5f00\u53d1","link":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/category\/%e5%bc%80%e5%8f%91\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":58,"url":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/2018\/03\/20\/c%e4%b8%8ejava%e8%b7%a8%e5%b9%b3%e5%8f%b0%e5%8e%8b%e7%bc%a9%e4%b8%8e%e8%a7%a3%e5%8e%8b%e7%bc%a9%e9%80%9a%e8%ae%af\/","url_meta":{"origin":135,"position":2},"title":"C#\u4e0ejava\u8de8\u5e73\u53f0\u538b\u7f29\u4e0e\u89e3\u538b\u7f29\u901a\u8baf","author":"\u6770\u514b\u9648","date":"2018\u5e743\u670820\u65e5","format":false,"excerpt":"\u5173\u952e\u6280\u672f\u70b9\u548c\u4f53\u4f1a: \u538b\u7f29\u7684\u7ed3\u679c\u91c7\u7528Base64\u7f16\u7801\uff0c\u65b9\u4fbf\u5728Java\u7aef\u6216\u8005C#\u7aef\u6253\u5370\u51fa\u6765\u8c03\u8bd5\uff0c\u4e5f\u65b9\u4fbf\u5728\u2026","rel":"","context":"\u5728\u201c\u6280\u672f\u201d\u4e2d","block_context":{"text":"\u6280\u672f","link":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/category\/%e6%8a%80%e6%9c%af\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":56,"url":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/2018\/03\/19\/oracle%e8%a7%a6%e5%8f%91%e5%99%a8%e5%88%a4%e6%96%ad%e6%9b%b4%e6%96%b0%e8%a1%a8%e6%9f%90%e4%ba%9b%e5%ad%97%e6%ae%b5%e4%b8%8d%e6%93%8d%e4%bd%9c\/","url_meta":{"origin":135,"position":3},"title":"oracle\u89e6\u53d1\u5668\u5224\u65ad\u66f4\u65b0\u8868\u67d0\u4e9b\u5b57\u6bb5\u4e0d\u64cd\u4f5c","author":"\u6770\u514b\u9648","date":"2018\u5e743\u670819\u65e5","format":false,"excerpt":"\u4eca\u5929\u770b\u5230\u4e00\u7bc7\u6587\u7ae0oracle\u89e6\u53d1\u5668\u5224\u65ad\u66f4\u65b0\u8868\u65f6\uff0c\u5982\u679c\u53ea\u662f\u66f4\u65b0\u5b57\u6bb5C\uff0c\u90a3\u4e48\u4e0d\u5e0c\u671b\u89e6\u53d1\u8be5\u8be5\u8868\u7684\u89e6\u53d1\u5668\u53bb\u5904\u2026","rel":"","context":"\u5728\u201c\u6280\u672f\u201d\u4e2d","block_context":{"text":"\u6280\u672f","link":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/category\/%e6%8a%80%e6%9c%af\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5,"url":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/2018\/03\/03\/lombok%e7%9a%84%e4%bd%bf%e7%94%a8\/","url_meta":{"origin":135,"position":4},"title":"lombok\u7684\u4f7f\u7528","author":"\u6770\u514b\u9648","date":"2018\u5e743\u67083\u65e5","format":false,"excerpt":"\u80cc\u666f \u5728\u65e5\u5e38\u7684\u5f00\u53d1\u4e2d,\u6709\u5f88\u591a\u5de5\u4f5c\u90fd\u662f\u91cd\u590d\u7684,\u6bd4\u5982: - \u6211\u4eec\u4f1a\u5b9a\u4e49\u5f88\u591a\u7684domain\u5b9e\u4f53\u7c7b,\u9700\u8981\u7ed9\u6bcf\u2026","rel":"","context":"\u5728\u201c\u6280\u672f\u201d\u4e2d","block_context":{"text":"\u6280\u672f","link":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/category\/%e6%8a%80%e6%9c%af\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":23,"url":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/2018\/03\/06\/springboot-schedule-%e9%85%8d%e7%bd%ae\/","url_meta":{"origin":135,"position":5},"title":"SpringBoot Schedule \u914d\u7f6e","author":"\u6770\u514b\u9648","date":"2018\u5e743\u67086\u65e5","format":false,"excerpt":"1. \u5b9a\u65f6\u4efb\u52a1\u5b9e\u73b0\u65b9\u5f0f \u5b9a\u65f6\u4efb\u52a1\u5b9e\u73b0\u65b9\u5f0f\uff1a Java\u81ea\u5e26\u7684java.util.Timer\u7c7b\uff0c\u8fd9\u4e2a\u7c7b\u5141\u2026","rel":"","context":"\u5728\u201c\u6280\u672f\u201d\u4e2d","block_context":{"text":"\u6280\u672f","link":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/category\/%e6%8a%80%e6%9c%af\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/wp-json\/wp\/v2\/posts\/135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/wp-json\/wp\/v2\/comments?post=135"}],"version-history":[{"count":1,"href":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/wp-json\/wp\/v2\/posts\/135\/revisions"}],"predecessor-version":[{"id":136,"href":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/wp-json\/wp\/v2\/posts\/135\/revisions\/136"}],"wp:attachment":[{"href":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/wp-json\/wp\/v2\/media?parent=135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/wp-json\/wp\/v2\/categories?post=135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/wp-json\/wp\/v2\/tags?post=135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}