{"id":67843,"date":"2026-01-29T08:57:05","date_gmt":"2026-01-29T00:57:05","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/67843.html"},"modified":"2026-01-29T08:57:05","modified_gmt":"2026-01-29T00:57:05","slug":"jdbc-%e5%85%a8%e8%a7%a3%e6%9e%90%ef%bc%9a-%e4%bb%8e%e5%85%a5%e9%97%a8%e5%88%b0%e7%b2%be%e9%80%9a","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/67843.html","title":{"rendered":"JDBC \u5168\u89e3\u6790\uff1a \u4ece\u5165\u95e8\u5230\u7cbe\u901a"},"content":{"rendered":"<h3>\u5f15\u8a00&#xff1a;\u5b66\u4e60\u76ee\u6807<\/h3>\n<table>\n<tr>\u77e5\u8bc6\u70b9\u8981\u6c42<\/tr>\n<tbody>\n<tr>\n<td>JDBC \u6982\u8ff0<\/td>\n<td>\u638c\u63e1<\/td>\n<\/tr>\n<tr>\n<td>\u4f7f\u7528 JDBC \u5b8c\u6210\u6dfb\u52a0\u64cd\u4f5c<\/td>\n<td>\u638c\u63e1<\/td>\n<\/tr>\n<tr>\n<td>\u4f7f\u7528 JDBC \u5b8c\u6210\u66f4\u65b0\u548c\u5220\u9664<\/td>\n<td>\u638c\u63e1<\/td>\n<\/tr>\n<tr>\n<td>DBUtils \u7684\u7b80\u5355\u5c01\u88c5<\/td>\n<td>\u638c\u63e1<\/td>\n<\/tr>\n<tr>\n<td>\u4f7f\u7528 JDBC \u5b8c\u6210\u67e5\u8be2<\/td>\n<td>\u638c\u63e1<\/td>\n<\/tr>\n<tr>\n<td>\u4f7f\u7528 JDBC \u5b8c\u6210\u5206\u9875\u67e5\u8be2<\/td>\n<td>\u638c\u63e1<\/td>\n<\/tr>\n<tr>\n<td>\u5e38\u7528\u63a5\u53e3\u8be6\u89e3<\/td>\n<td>\u638c\u63e1<\/td>\n<\/tr>\n<tr>\n<td>JDBC \u6279\u5904\u7406<\/td>\n<td>\u638c\u63e1<\/td>\n<\/tr>\n<tr>\n<td>SQL \u6ce8\u5165\u95ee\u9898<\/td>\n<td>\u638c\u63e1<\/td>\n<\/tr>\n<tr>\n<td>\u4e8b\u52a1\u5904\u7406\u89e3\u51b3\u8f6c\u8d26\u95ee\u9898<\/td>\n<td>\u638c\u63e1<\/td>\n<\/tr>\n<tr>\n<td>\u8fde\u63a5\u6c60<\/td>\n<td>\u638c\u63e1<\/td>\n<\/tr>\n<tr>\n<td>\u4f7f\u7528\u53cd\u5c04\u5bf9 DBUtils \u518d\u6b21\u7684\u5c01\u88c5<\/td>\n<td>\u638c\u63e1<\/td>\n<\/tr>\n<tr>\n<td>BaseDAO \u7684\u5c01\u88c5<\/td>\n<td>\u638c\u63e1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u4e00\u3001JDBC \u6982\u8ff0<\/h3>\n<h4>1.1 \u4e3a\u4ec0\u4e48\u8981\u5b66\u4e60 JDBC&#xff1f;<\/h4>\n<p>\u5982\u7528\u6237\u63d0\u4ea4\u8868\u5355\u65f6&#xff0c;JDBC \u80fd\u81ea\u52a8\u5c06\u6570\u636e\u5b58\u5165\u6570\u636e\u5e93&#xff0c;\u800c Navicat \u53ea\u80fd\u6bcf\u6b21\u624b\u52a8\u64cd\u4f5c&#xff0c;\u65e0\u6cd5\u5bf9\u63a5\u7a0b\u5e8f\u5b9e\u73b0\u81ea\u52a8\u5316\u64cd\u4f5c\u3002<\/p>\n<p>JDBC \u7684\u6838\u5fc3\u4ef7\u503c\u5728\u4e8e\u5b9e\u73b0 Java \u7a0b\u5e8f\u4e0e\u6570\u636e\u5e93\u7684\u65e0\u7f1d\u5bf9\u63a5&#xff0c;\u652f\u6301\u81ea\u52a8\u5316\u6570\u636e\u5904\u7406\u3002<\/p>\n<h4>1.2 \u4ec0\u4e48\u662f JDBC&#xff1f;<\/h4>\n<p>JDBC&#xff08;Java DataBase Connectivity&#xff09;\u5373 Java \u6570\u636e\u5e93\u8fde\u63a5&#xff0c;\u662f\u7528 Java \u8bed\u8a00\u64cd\u4f5c\u6570\u636e\u5e93\u7684\u89c4\u8303\u3002\u4f20\u7edf\u64cd\u4f5c\u6570\u636e\u5e93\u9700\u5728 Navicat \u7b49\u5de5\u5177\u4e2d\u6267\u884c SQL \u8bed\u53e5&#xff0c;\u800c JDBC \u5141\u8bb8\u901a\u8fc7 Java \u4ee3\u7801\u5411\u6570\u636e\u5e93\u53d1\u9001 SQL \u6307\u4ee4&#xff0c;\u5b9e\u73b0\u7a0b\u5e8f\u9a71\u52a8\u7684\u6570\u636e\u4ea4\u4e92\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260129005702-697ab05ee40f8.png\" \/><\/p>\n<h4>1.3 JDBC \u7684\u539f\u7406<\/h4>\n<ul>\n<li>SUN \u516c\u53f8\u63d0\u4f9b JDBC \u6838\u5fc3\u63a5\u53e3&#xff08;\u89c4\u8303&#xff09;&#xff0c;\u5b9a\u4e49\u6570\u636e\u5e93\u8bbf\u95ee\u6807\u51c6\u3002<\/li>\n<li>\u5404\u6570\u636e\u5e93\u5382\u5546&#xff08;\u5982 MySQL\u3001Oracle&#xff09;\u9075\u5faa\u8be5\u89c4\u8303&#xff0c;\u63d0\u4f9b\u5b9e\u73b0\u63a5\u53e3\u7684\u9a71\u52a8\u7a0b\u5e8f&#xff08;Driver&#xff09;\u3002<\/li>\n<li>Java \u7a0b\u5e8f\u901a\u8fc7 JDBC \u63a5\u53e3\u8c03\u7528\u5382\u5546\u9a71\u52a8&#xff0c;\u95f4\u63a5\u64cd\u4f5c\u6570\u636e\u5e93&#xff0c;\u5b9e\u73b0\u8de8\u6570\u636e\u5e93\u517c\u5bb9\u3002<\/li>\n<li>\u7b2c\u4e09\u65b9\u4e5f\u53ef\u80fd\u63d0\u4f9b\u975e\u5f00\u6e90\u514d\u8d39\u7684\u6570\u636e\u5e93\u9a71\u52a8\u3002<\/li>\n<\/ul>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260129005703-697ab05f16c3d.png\" \/><\/p>\n<h4>1.4 JDBC \u7684 API<\/h4>\n<p>JDBC \u6838\u5fc3\u529f\u80fd\u53ef\u6982\u62ec\u4e3a\u4e09\u70b9&#xff1a;\u4e0e\u6570\u636e\u5e93\u5efa\u7acb\u8fde\u63a5\u3001\u53d1\u9001 SQL \u8bed\u53e5\u3001\u5904\u7406\u67e5\u8be2\u7ed3\u679c\u3002\u6838\u5fc3 API \u7ec4\u4ef6\u5982\u4e0b&#xff1a;<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260129005703-697ab05f33c75.png\" \/><\/p>\n<ul>\n<li>DriverManager&#xff1a;\u7ba1\u7406 JDBC \u9a71\u52a8&#xff0c;\u8d1f\u8d23\u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/li>\n<li>Connection&#xff1a;\u4ee3\u8868\u6570\u636e\u5e93\u7269\u7406\u8fde\u63a5&#xff0c;\u8d1f\u8d23\u6570\u636e\u4f20\u8f93\u3002<\/li>\n<li>Statement&#xff1a;\u7531 Connection \u521b\u5efa&#xff0c;\u7528\u4e8e\u53d1\u9001\u6267\u884c SQL \u8bed\u53e5\u3002<\/li>\n<li>ResultSet&#xff1a;\u4fdd\u5b58 Statement \u6267\u884c\u67e5\u8be2\u540e\u7684\u7ed3\u679c\u96c6\u3002<\/li>\n<\/ul>\n<h3>\u4e8c\u3001\u5e38\u7528\u63a5\u53e3\u8be6\u89e3<\/h3>\n<h4>2.1 DriverManager<\/h4>\n<p>\u7528\u4e8e\u7ba1\u7406 JDBC \u9a71\u52a8\u7684\u670d\u52a1\u7c7b&#xff0c;\u6838\u5fc3\u65b9\u6cd5&#xff1a;<\/p>\n<p>java<\/p>\n<p>public static Connection getConnection(String url, String user, String password) throws SQLException<\/p>\n<p>\u529f\u80fd&#xff1a;\u83b7\u53d6\u6307\u5b9a URL \u5bf9\u5e94\u6570\u636e\u5e93\u7684\u8fde\u63a5&#xff0c;\u53c2\u6570\u5206\u522b\u4e3a\u6570\u636e\u5e93\u5730\u5740\u3001\u7528\u6237\u540d\u3001\u5bc6\u7801\u3002<\/p>\n<h4>2.2 Connection<\/h4>\n<p>\u6570\u636e\u5e93\u8fde\u63a5\u5bf9\u8c61&#xff0c;\u6bcf\u4e2a Connection \u5bf9\u5e94\u4e00\u4e2a\u7269\u7406\u8fde\u63a5\u4f1a\u8bdd\u3002\u5e38\u7528\u65b9\u6cd5&#xff1a;<\/p>\n<ul>\n<li>\u521b\u5efa SQL \u6267\u884c\u5bf9\u8c61&#xff1a;\n<ul>\n<li>Statement createStatement()&#xff1a;\u521b\u5efa\u666e\u901a Statement \u5bf9\u8c61\u3002<\/li>\n<li>PreparedStatement prepareStatement(String sql)&#xff1a;\u521b\u5efa\u9884\u7f16\u8bd1 Statement \u5bf9\u8c61&#xff08;\u9632 SQL \u6ce8\u5165&#xff09;\u3002<\/li>\n<li>CallableStatement prepareCall(String sql)&#xff1a;\u521b\u5efa\u8c03\u7528\u5b58\u50a8\u8fc7\u7a0b\u7684 Statement \u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u4e8b\u52a1\u63a7\u5236\u65b9\u6cd5&#xff1a;\n<ul>\n<li>setAutoCommit(boolean autoCommit)&#xff1a;\u5173\u95ed\u81ea\u52a8\u63d0\u4ea4&#xff08;\u5f00\u542f\u4e8b\u52a1&#xff09;\u3002<\/li>\n<li>commit()&#xff1a;\u63d0\u4ea4\u4e8b\u52a1\u3002<\/li>\n<li>rollback()&#xff1a;\u56de\u6eda\u4e8b\u52a1\u3002<\/li>\n<li>setSavepoint()&#xff1a;\u521b\u5efa\u4e8b\u52a1\u4fdd\u5b58\u70b9\u3002<\/li>\n<li>setTransactionIsolation(int level)&#xff1a;\u8bbe\u7f6e\u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>2.3 Statement<\/h4>\n<p>\u6267\u884c SQL \u8bed\u53e5\u7684\u5de5\u5177\u63a5\u53e3&#xff0c;\u652f\u6301 DDL\u3001DML\u3001\u67e5\u8be2\u8bed\u53e5&#xff0c;\u5e38\u7528\u65b9\u6cd5&#xff1a;<\/p>\n<ul>\n<li>ResultSet executeQuery(String sql)&#xff1a;\u6267\u884c\u67e5\u8be2\u8bed\u53e5&#xff0c;\u8fd4\u56de\u7ed3\u679c\u96c6&#xff08;\u4ec5\u652f\u6301 SELECT&#xff09;\u3002<\/li>\n<li>int executeUpdate(String sql)&#xff1a;\u6267\u884c DML&#xff08;INSERT\/UPDATE\/DELETE&#xff09;\u8fd4\u56de\u53d7\u5f71\u54cd\u884c\u6570&#xff1b;\u6267\u884c DDL \u8fd4\u56de 0\u3002<\/li>\n<li>boolean execute(String sql)&#xff1a;\u6267\u884c\u4efb\u610f SQL&#xff0c;\u8fd4\u56de true \u8868\u793a\u7ed3\u679c\u4e3a ResultSet&#xff0c;false \u8868\u793a\u53d7\u5f71\u54cd\u884c\u6570\u6216\u65e0\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<h4>2.4 PreparedStatement<\/h4>\n<p>Statement \u7684\u5b50\u63a5\u53e3&#xff0c;\u652f\u6301\u9884\u7f16\u8bd1 SQL&#xff08;\u542b\u53c2\u6570\u5360\u4f4d\u7b26?&#xff09;&#xff0c;\u4f18\u52bf&#xff1a;<\/p>\n<ul>\n<li>\u9884\u7f16\u8bd1\u540e\u91cd\u590d\u6267\u884c\u65e0\u9700\u91cd\u65b0\u7f16\u8bd1&#xff0c;\u63d0\u5347\u6548\u7387\u3002<\/li>\n<li>\u901a\u8fc7\u53c2\u6570\u7ed1\u5b9a\u907f\u514d SQL \u6ce8\u5165&#xff0c;\u66f4\u5b89\u5168\u3002\u6838\u5fc3\u65b9\u6cd5&#xff1a;<\/li>\n<\/ul>\n<p>java<\/p>\n<p>void setXxx(int parameterIndex, Xxx value)<\/p>\n<p>\u6839\u636e\u53c2\u6570\u7c7b\u578b&#xff08;\u5982 String\u3001int&#xff09;\u9009\u62e9\u5bf9\u5e94\u65b9\u6cd5&#xff0c;\u6309\u7d22\u5f15\u4e3a SQL \u5360\u4f4d\u7b26\u8d4b\u503c\u3002<\/p>\n<h4>2.5 ResultSet<\/h4>\n<p>\u67e5\u8be2\u7ed3\u679c\u96c6\u5bf9\u8c61&#xff0c;\u901a\u8fc7\u6e38\u6807&#xff08;\u6307\u9488&#xff09;\u8bbf\u95ee\u6570\u636e&#xff0c;\u5e38\u7528\u65b9\u6cd5&#xff1a;<\/p>\n<ul>\n<li>\u6e38\u6807\u79fb\u52a8&#xff1a;\n<ul>\n<li>boolean next()&#xff1a;\u6e38\u6807\u5411\u4e0b\u79fb\u52a8\u4e00\u884c&#xff0c;\u8fd4\u56de\u662f\u5426\u6307\u5411\u6709\u6548\u6570\u636e\u3002<\/li>\n<li>boolean absolute(int row)&#xff1a;\u79fb\u52a8\u5230\u6307\u5b9a\u884c&#xff08;\u8d1f\u6570\u8868\u793a\u5012\u6570&#xff09;\u3002<\/li>\n<li>boolean last()&#xff1a;\u79fb\u52a8\u5230\u6700\u540e\u4e00\u884c\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u6570\u636e\u83b7\u53d6&#xff1a;\n<ul>\n<li>getInt(int colIndex)\/getInt(String colLabel)&#xff1a;\u901a\u8fc7\u5217\u7d22\u5f15\u6216\u5217\u540d\u83b7\u53d6 int \u7c7b\u578b\u503c\u3002<\/li>\n<li>getString(int colIndex)\/getString(String colLabel)&#xff1a;\u83b7\u53d6\u5b57\u7b26\u4e32\u7c7b\u578b\u503c\u3002<\/li>\n<li>getObject(String colLabel)&#xff1a;\u83b7\u53d6\u4efb\u610f\u7c7b\u578b\u503c\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u8d44\u6e90\u91ca\u653e&#xff1a;close()&#xff1a;\u5173\u95ed\u7ed3\u679c\u96c6&#xff0c;\u91ca\u653e\u8d44\u6e90\u3002<\/li>\n<\/ul>\n<h3>\u4e09\u3001JDBC \u64cd\u4f5c\u6570\u636e\u5e93\u7684\u6b65\u9aa4<\/h3>\n<h4>3.1 \u52a0\u8f7d\u9a71\u52a8<\/h4>\n<p>\u901a\u8fc7Class.forName()\u52a0\u8f7d\u9a71\u52a8\u7c7b&#xff0c;\u5e38\u7528\u6570\u636e\u5e93\u9a71\u52a8\u52a0\u8f7d\u8bed\u53e5&#xff1a;<\/p>\n<p>java<\/p>\n<p>\/\/ Oracle<br \/>\nClass.forName(&#034;oracle.JDBC.driver.OracleDriver&#034;);<br \/>\n\/\/ SQL Server<br \/>\nClass.forName(&#034;com.microsoft.JDBC.sqlserver.SQLServerDriver&#034;);<br \/>\n\/\/ DB2<br \/>\nClass.forName(&#034;com.ibm.db2.JDBC.app.DB2Driver&#034;);<br \/>\n\/\/ MySQL 5<br \/>\nClass.forName(&#034;com.mysql.jdbc.Driver&#034;);<br \/>\n\/\/ MySQL 8<br \/>\nClass.forName(&#034;com.mysql.cj.jdbc.Driver&#034;);<\/p>\n<h4>3.2 \u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5<\/h4>\n<p>\u8c03\u7528DriverManager.getConnection()\u83b7\u53d6\u8fde\u63a5&#xff1a;<\/p>\n<p>java<\/p>\n<p>Connection conn &#061; null;<br \/>\nString url &#061; &#034;jdbc:mysql:\/\/localhost:3306\/jdbc_db?useUnicode&#061;true&amp;useSSL&#061;false&amp;characterEncoding&#061;UTF8&#034;;<br \/>\nString user &#061; &#034;root&#034;;<br \/>\nString password &#061; &#034;123456&#034;;<br \/>\nconn &#061; DriverManager.getConnection(url, user, password);<\/p>\n<h4>3.3 \u521b\u5efa Statement \u5e76\u53d1\u9001\u547d\u4ee4<\/h4>\n<p>\u6839\u636e\u9700\u6c42\u521b\u5efa Statement\/PreparedStatement \u5bf9\u8c61&#xff0c;\u6267\u884c SQL&#xff1a;<\/p>\n<p>java<\/p>\n<p>\/\/ \u666e\u901aStatement<br \/>\nStatement stmt &#061; conn.createStatement();<br \/>\nint affectedRows &#061; stmt.executeUpdate(&#034;INSERT INTO student VALUES(1,&#039;\u5c0f\u521a&#039;,32,&#039;\u7537&#039;,&#039;\u6b66\u6c49&#039;)&#034;);<\/p>\n<p>\/\/ \u9884\u7f16\u8bd1PreparedStatement<br \/>\nPreparedStatement pstmt &#061; conn.prepareStatement(&#034;INSERT INTO student VALUES(?, ?, ?, ?, ?)&#034;);<br \/>\npstmt.setInt(1, 2);<br \/>\npstmt.setString(2, &#034;\u5c0f\u660e&#034;);<br \/>\npstmt.setInt(3, 23);<br \/>\npstmt.setString(4, &#034;\u5973&#034;);<br \/>\npstmt.setString(5, &#034;\u6b66\u6c49&#034;);<br \/>\npstmt.executeUpdate();<\/p>\n<h4>3.4 \u5904\u7406 ResultSet \u7ed3\u679c<\/h4>\n<p>\u901a\u8fc7\u5faa\u73af\u904d\u5386\u7ed3\u679c\u96c6&#xff0c;\u63d0\u53d6\u6570\u636e&#xff1a;<\/p>\n<p>java<\/p>\n<p>ResultSet rs &#061; stmt.executeQuery(&#034;SELECT * FROM student&#034;);<br \/>\nwhile (rs.next()) {<br \/>\n    int id &#061; rs.getInt(1); \/\/ \u5217\u7d22\u5f15&#xff08;\u4ece1\u5f00\u59cb&#xff09;<br \/>\n    String name &#061; rs.getString(&#034;name&#034;); \/\/ \u5217\u540d<br \/>\n    int age &#061; rs.getInt(&#034;age&#034;);<br \/>\n    System.out.println(id &#043; &#034; &#034; &#043; name &#043; &#034; &#034; &#043; age);<br \/>\n}<\/p>\n<h4>3.5 \u5173\u95ed\u6570\u636e\u5e93\u8d44\u6e90<\/h4>\n<p>\u5fc5\u987b\u6309\u300cResultSet \u2192 Statement \u2192 Connection\u300d\u7684\u987a\u5e8f\u5173\u95ed&#xff08;\u4e0e\u521b\u5efa\u987a\u5e8f\u76f8\u53cd&#xff09;&#xff0c;\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f&#xff1a;<\/p>\n<p>java<\/p>\n<p>rs.close();<br \/>\nstmt.close();<br \/>\nconn.close();<\/p>\n<h3>\u56db\u3001\u51c6\u5907\u5de5\u4f5c<\/h3>\n<h4>4.1 \u521b\u5efa\u6570\u636e\u5e93\u5e76\u521b\u5efa student \u8868<\/h4>\n<li>\u521b\u5efa\u6570\u636e\u5e93jdbc_db&#xff0c;\u5b57\u7b26\u96c6utf8&#xff0c;\u6392\u5e8f\u89c4\u5219utf8_general_ci\u3002<\/li>\n<li style=\"text-align:center\">\u521b\u5efastudent\u8868&#xff1a;\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\n<p class=\"img-center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260129005703-697ab05f5f65c.png\" \/><\/p>\n<\/li>\n<h4>4.2 \u521b\u5efa\u9879\u76ee<\/h4>\n<p>\u4f7f\u7528 IDEA \u521b\u5efa Java \u9879\u76ee&#xff08;JDK \u5efa\u8bae 1.8&#043;&#xff09;&#xff0c;\u65e0\u9700\u989d\u5916\u6846\u67b6&#xff0c;\u4fdd\u6301\u57fa\u7840 Java \u5de5\u7a0b\u7ed3\u6784\u3002<\/p>\n<h4>4.3 \u521b\u5efa lib \u76ee\u5f55\u5e76\u5f15\u5165 MySQL \u9a71\u52a8\u5305<\/h4>\n<li>\u5728\u9879\u76ee\u6839\u76ee\u5f55\u521b\u5efalib\u6587\u4ef6\u5939\u3002<\/li>\n<li>\u653e\u5165 MySQL \u9a71\u52a8\u5305&#xff08;\u5982mysql-connector-java-5.1.38.jar&#xff09;\u3002<\/li>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260129005703-697ab05f78221.png\" \/><\/p>\n<h4>4.4 \u628a lib \u5305\u5f15\u5165\u9879\u76ee\u73af\u5883<\/h4>\n<li>\u53f3\u952elib\u76ee\u5f55 \u2192 \u9009\u62e9\u300cAdd as Library&#8230;\u300d\u3002<\/li>\n<li>\u9009\u62e9\u300cProject Library\u300d\u7ea7\u522b&#xff0c;\u6dfb\u52a0\u5230\u5f53\u524d\u9879\u76ee&#xff0c;\u70b9\u51fb\u300cOK\u300d\u3002<\/li>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260129005703-697ab05f8f004.png\" \/><\/p>\n<h3>\u4e94\u3001\u4f7f\u7528 JDBC \u5b8c\u6210\u6dfb\u52a0\u64cd\u4f5c<\/h3>\n<h4>5.1 \u6848\u4f8b\u5b9e\u73b0<\/h4>\n<p>java<\/p>\n<p>package com.hg.jdbc;<\/p>\n<p>import java.sql.Connection;<br \/>\nimport java.sql.DriverManager;<br \/>\nimport java.sql.SQLException;<br \/>\nimport java.sql.Statement;<\/p>\n<p>public class AddTest {<br \/>\n    \/\/ \u9a71\u52a8\u5668\u8def\u5f84<br \/>\n    private static final String DRIVER &#061; &#034;com.mysql.jdbc.Driver&#034;;<br \/>\n    \/\/ \u8fde\u63a5\u6570\u636e\u5e93\u5730\u5740<br \/>\n    private static final String URL &#061; &#034;jdbc:mysql:\/\/localhost:3306\/jdbc_db?useSSL&#061;false&amp;characterEncoding&#061;UTF8&#034;;<br \/>\n    \/\/ \u6570\u636e\u5e93\u7528\u6237\u540d<br \/>\n    private static final String USER_NAME &#061; &#034;root&#034;;<br \/>\n    \/\/ \u6570\u636e\u5e93\u5bc6\u7801<br \/>\n    private static final String USER_PASSWORD &#061; &#034;1111&#034;;<\/p>\n<p>    public static void main(String[] args) throws ClassNotFoundException, SQLException {<br \/>\n        \/\/ \u52a0\u8f7dJDBC\u8bbf\u95eemysql\u7684\u9a71\u52a8<br \/>\n        Class.forName(DRIVER);<br \/>\n        \/\/ \u5efa\u7acb\u548c\u6570\u636e\u5e93\u7684\u8fde\u63a5<br \/>\n        Connection conn &#061; DriverManager.getConnection(URL, USER_NAME, USER_PASSWORD);<br \/>\n        \/\/ \u521b\u5efaSQL\u547d\u4ee4\u53d1\u9001\u5668<br \/>\n        Statement stmt &#061; conn.createStatement();<br \/>\n        \/\/ \u4f7f\u7528SQL\u547d\u4ee4\u53d1\u9001\u5668\u53d1\u9001SQL\u547d\u4ee4\u5e76\u5f97\u5230\u7ed3\u679c<br \/>\n        String sql &#061; &#034;insert into student values(1,&#039;\u5c0f\u521a&#039;,32,&#039;\u7537&#039;,&#039;\u6e56\u5317\u7701\u6b66\u6c49\u5e02&#039;)&#034;;<br \/>\n        int n &#061; stmt.executeUpdate(sql);<br \/>\n        \/\/ \u5904\u7406\u7ed3\u679c<br \/>\n        if (n &gt; 0) {<br \/>\n            System.out.println(&#034;\u6dfb\u52a0\u6210\u529f&#034;);<br \/>\n        } else {<br \/>\n            System.out.println(&#034;\u6dfb\u52a0\u5931\u8d25&#034;);<br \/>\n        }<br \/>\n        \/\/ \u5173\u95ed\u6570\u636e\u5e93\u8d44\u6e90<br \/>\n        stmt.close();<br \/>\n        conn.close();<br \/>\n    }<br \/>\n}<\/p>\n<h4>5.2 URL \u8be6\u89e3<\/h4>\n<p>URL \u683c\u5f0f&#xff1a;\u534f\u8bae:\/\/\u670d\u52a1\u5668\u4e3b\u673a:\u7aef\u53e3\/\u670d\u52a1\u5668\u8def\u5f84?\u67e5\u8be2\u53c2\u6570<\/p>\n<ul>\n<li>\u534f\u8bae&#xff1a;jdbc:mysql:&#xff08;\u56fa\u5b9a\u683c\u5f0f&#xff09;\u3002<\/li>\n<li>\u670d\u52a1\u5668\u4e3b\u673a&#xff1a;\u6570\u636e\u5e93\u6240\u5728\u5730\u5740&#xff08;\u672c\u5730\u4e3alocalhost&#xff09;\u3002<\/li>\n<li>\u7aef\u53e3&#xff1a;MySQL \u9ed8\u8ba4 3306\u3002<\/li>\n<li>\u670d\u52a1\u5668\u8def\u5f84&#xff1a;\u6570\u636e\u5e93\u540d&#xff08;\u5982jdbc_db&#xff09;\u3002<\/li>\n<li>\u67e5\u8be2\u53c2\u6570&#xff1a;\u7f16\u7801\u683c\u5f0f\u3001SSL \u8bbe\u7f6e\u7b49&#xff08;\u5982useSSL&#061;false&amp;characterEncoding&#061;UTF8&#xff09;\u3002<\/li>\n<\/ul>\n<h3>\u516d\u3001\u4f7f\u7528 JDBC \u5b8c\u6210\u66f4\u65b0\u548c\u5220\u9664<\/h3>\n<h4>6.1 \u6848\u4f8b\u539f\u7406<\/h4>\n<p>\u66f4\u65b0\u548c\u5220\u9664\u64cd\u4f5c\u6d41\u7a0b\u4e0e\u6dfb\u52a0\u4e00\u81f4&#xff0c;\u6838\u5fc3\u533a\u522b\u5728\u4e8e SQL \u8bed\u53e5&#xff0c;\u5747\u4f7f\u7528executeUpdate()\u65b9\u6cd5\u6267\u884c\u3002<\/p>\n<h4>6.2 \u4fee\u6539\u64cd\u4f5c\u4ee3\u7801<\/h4>\n<p>java<\/p>\n<p>package com.hg.jdbc;<\/p>\n<p>import java.sql.Connection;<br \/>\nimport java.sql.DriverManager;<br \/>\nimport java.sql.SQLException;<br \/>\nimport java.sql.Statement;<\/p>\n<p>public class UpdateTest {<br \/>\n    private static final String DRIVER &#061; &#034;com.mysql.jdbc.Driver&#034;;<br \/>\n    private static final String URL &#061; &#034;jdbc:mysql:\/\/localhost:3306\/jdbc_db?useSSL&#061;false&amp;characterEncoding&#061;UTF8&#034;;<br \/>\n    private static final String USER_NAME &#061; &#034;root&#034;;<br \/>\n    private static final String USER_PASSWORD &#061; &#034;1111&#034;;<\/p>\n<p>    public static void main(String[] args) throws ClassNotFoundException, SQLException {<br \/>\n        Class.forName(DRIVER);<br \/>\n        Connection conn &#061; DriverManager.getConnection(URL, USER_NAME, USER_PASSWORD);<br \/>\n        Statement stat &#061; conn.createStatement();<br \/>\n        \/\/ \u7f16\u5199\u66f4\u65b0SQL<br \/>\n        String sql &#061; &#034;update student set name&#061;&#039;\u5c0f\u660e&#039;,age&#061;23,sex&#061;&#039;\u5973&#039;,address&#061;&#039;\u6b66\u6c49&#039; where id&#061;1&#034;;<br \/>\n        int res &#061; stat.executeUpdate(sql);<br \/>\n        if (res &gt; 0) {<br \/>\n            System.out.println(&#034;\u4fee\u6539\u6210\u529f&#034;);<br \/>\n        } else {<br \/>\n            System.out.println(&#034;\u5904\u7406\u5931\u8d25&#034;);<br \/>\n        }<br \/>\n        stat.close();<br \/>\n        conn.close();<br \/>\n    }<br \/>\n}<\/p>\n<h4>6.3 \u5220\u9664\u64cd\u4f5c\u4ee3\u7801<\/h4>\n<p>java<\/p>\n<p>package com.hg.jdbc;<\/p>\n<p>import java.sql.Connection;<br \/>\nimport java.sql.DriverManager;<br \/>\nimport java.sql.SQLException;<br \/>\nimport java.sql.Statement;<\/p>\n<p>public class DeleteTest {<br \/>\n    private static final String DRIVER &#061; &#034;com.mysql.jdbc.Driver&#034;;<br \/>\n    private static final String URL &#061; &#034;jdbc:mysql:\/\/localhost:3306\/jdbc_db?useSSL&#061;false&amp;characterEncoding&#061;UTF8&#034;;<br \/>\n    private static final String USER_NAME &#061; &#034;root&#034;;<br \/>\n    private static final String USER_PASSWORD &#061; &#034;1111&#034;;<\/p>\n<p>    public static void main(String[] args) throws ClassNotFoundException, SQLException {<br \/>\n        Class.forName(DRIVER);<br \/>\n        Connection conn &#061; DriverManager.getConnection(URL, USER_NAME, USER_PASSWORD);<br \/>\n        Statement stat &#061; conn.createStatement();<br \/>\n        \/\/ \u7f16\u5199\u5220\u9664SQL<br \/>\n        String sql &#061; &#034;delete from student where id&#061;1&#034;;<br \/>\n        int res &#061; stat.executeUpdate(sql);<br \/>\n        if (res &gt; 0) {<br \/>\n            System.out.println(&#034;\u5220\u9664\u6210\u529f&#034;);<br \/>\n        } else {<br \/>\n            System.out.println(&#034;\u5220\u9664\u5931\u8d25&#034;);<br \/>\n        }<br \/>\n        stat.close();<br \/>\n        conn.close();<br \/>\n    }<br \/>\n}<\/p>\n<h3>\u4e03\u3001DBUtils \u7684\u7b80\u5355\u5c01\u88c5<\/h3>\n<h4>7.1 \u4e3a\u4ec0\u4e48\u8981\u5c01\u88c5<\/h4>\n<p>\u6dfb\u52a0\u3001\u66f4\u65b0\u3001\u5220\u9664\u64cd\u4f5c\u4e2d&#xff0c;\u52a0\u8f7d\u9a71\u52a8\u3001\u521b\u5efa\u8fde\u63a5\u3001\u5173\u95ed\u8d44\u6e90\u7b49\u4ee3\u7801\u91cd\u590d\u5ea6\u6781\u9ad8&#xff0c;\u5c01\u88c5\u4e3a\u5de5\u5177\u7c7b\u53ef\u51cf\u5c11\u5197\u4f59&#xff0c;\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<h4>7.2 \u521b\u5efa db.properties \u914d\u7f6e\u6587\u4ef6<\/h4>\n<p>\u5728src\u76ee\u5f55\u4e0b\u521b\u5efa\u914d\u7f6e\u6587\u4ef6&#xff0c;\u5b58\u50a8\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f&#xff1a;<\/p>\n<p>properties<\/p>\n<p>driverClass&#061;com.mysql.jdbc.Driver<br \/>\nurl&#061;jdbc:mysql:\/\/localhost:3306\/jdbc_db<br \/>\nusername&#061;root<br \/>\npassword&#061;1111<\/p>\n<h4>7.3 \u5b9e\u73b0 DBUtils \u5de5\u5177\u7c7b<\/h4>\n<p>java<\/p>\n<p>import java.io.InputStream;<br \/>\nimport java.sql.Connection;<br \/>\nimport java.sql.DriverManager;<br \/>\nimport java.sql.SQLException;<br \/>\nimport java.util.Properties;<\/p>\n<p>public class DBUtils {<br \/>\n    \/\/ \u914d\u7f6e\u53d8\u91cf<br \/>\n    private static String DRIVER;<br \/>\n    private static String URL;<br \/>\n    private static String USER_NAME;<br \/>\n    private static String USER_PASSWORD;<\/p>\n<p>    \/\/ \u9759\u6001\u4ee3\u7801\u5757&#xff1a;\u52a0\u8f7d\u914d\u7f6e&#043;\u6ce8\u518c\u9a71\u52a8<br \/>\n    static {<br \/>\n        try {<br \/>\n            InputStream is &#061; DBUtils.class.getClassLoader().getResourceAsStream(&#034;db.properties&#034;);<br \/>\n            Properties prop &#061; new Properties();<br \/>\n            prop.load(is);<\/p>\n<p>            DRIVER &#061; prop.getProperty(&#034;driverClass&#034;);<br \/>\n            URL &#061; prop.getProperty(&#034;url&#034;);<br \/>\n            USER_NAME &#061; prop.getProperty(&#034;username&#034;);<br \/>\n            USER_PASSWORD &#061; prop.getProperty(&#034;password&#034;);<\/p>\n<p>            \/\/ \u6ce8\u518c\u9a71\u52a8<br \/>\n            Class.forName(DRIVER);<br \/>\n        } catch (Exception e) {<br \/>\n            e.printStackTrace();<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5<br \/>\n    public static Connection getConn() {<br \/>\n        try {<br \/>\n            return DriverManager.getConnection(URL, USER_NAME, USER_PASSWORD);<br \/>\n        } catch (SQLException e) {<br \/>\n            e.printStackTrace();<br \/>\n            System.out.println(&#034;\u521b\u5efa\u8fde\u63a5\u5bf9\u8c61\u5f02\u5e38&#034;);<br \/>\n        }<br \/>\n        return null;<br \/>\n    }<\/p>\n<p>    \/\/ \u91ca\u653e\u8d44\u6e90&#xff08;\u652f\u6301Connection\u3001Statement\u3001ResultSet&#xff09;<br \/>\n    public static void close(AutoCloseable closeable) {<br \/>\n        try {<br \/>\n            if (closeable !&#061; null) {<br \/>\n                closeable.close();<br \/>\n            }<br \/>\n        } catch (Exception e) {<br \/>\n            e.printStackTrace();<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<h4>7.4 \u6539\u9020\u539f\u6709\u4ee3\u7801<\/h4>\n<p>\u4ee5\u6dfb\u52a0\u64cd\u4f5c\u4e3a\u4f8b&#xff0c;\u6539\u9020\u540e\u4ee3\u7801&#xff1a;<\/p>\n<p>java<\/p>\n<p>package com.hg.jdbc;<\/p>\n<p>import com.hg.utils.DBUtils;<br \/>\nimport java.sql.Connection;<br \/>\nimport java.sql.SQLException;<br \/>\nimport java.sql.Statement;<\/p>\n<p>public class Test01Add {<br \/>\n    public static void main(String[] args) throws ClassNotFoundException, SQLException {<br \/>\n        Connection conn &#061; DBUtils.getConn();<br \/>\n        Statement stmt &#061; conn.createStatement();<br \/>\n        String sql &#061; &#034;insert into student values(1,&#039;\u5c0f\u521a&#039;,32,&#039;\u7537&#039;,&#039;\u6e56\u5317\u7701\u6b66\u6c49\u5e02&#039;)&#034;;<br \/>\n        int n &#061; stmt.executeUpdate(sql);<br \/>\n        if (n &gt; 0) {<br \/>\n            System.out.println(&#034;\u6dfb\u52a0\u6210\u529f&#034;);<br \/>\n        } else {<br \/>\n            System.out.println(&#034;\u6dfb\u52a0\u5931\u8d25&#034;);<br \/>\n        }<br \/>\n        \/\/ \u8c03\u7528\u5de5\u5177\u7c7b\u5173\u95ed\u8d44\u6e90<br \/>\n        DBUtils.close(stmt);<br \/>\n        DBUtils.close(conn);<br \/>\n    }<br \/>\n}<\/p>\n<h3>\u516b\u3001\u4f7f\u7528 JDBC \u5b8c\u6210\u67e5\u8be2<\/h3>\n<h4>8.1 \u73af\u5883\u51c6\u5907&#xff08;\u6279\u91cf\u6dfb\u52a0\u6d4b\u8bd5\u6570\u636e&#xff09;<\/h4>\n<p>\u5148\u6279\u91cf\u63d2\u5165 20 \u6761\u6d4b\u8bd5\u6570\u636e&#xff0c;\u65b9\u4fbf\u67e5\u8be2\u9a8c\u8bc1&#xff1a;<\/p>\n<p>java<\/p>\n<p>package com.hg.jdbc;<\/p>\n<p>import com.hg.utils.DBUtils;<br \/>\nimport java.sql.Connection;<br \/>\nimport java.sql.SQLException;<br \/>\nimport java.sql.Statement;<br \/>\nimport java.util.Random;<\/p>\n<p>public class Test01Add20 {<br \/>\n    public static void main(String[] args) throws ClassNotFoundException, SQLException {<br \/>\n        Connection conn &#061; DBUtils.getConn();<br \/>\n        Statement stmt &#061; conn.createStatement();<br \/>\n        Random random &#061; new Random();<br \/>\n        for (int i &#061; 1; i &lt;&#061; 20; i&#043;&#043;) {<br \/>\n            Integer id &#061; i;<br \/>\n            String name &#061; &#034;\u5c0f\u660e&#034; &#043; i;<br \/>\n            int age &#061; random.nextInt(100);<br \/>\n            String sex &#061; random.nextBoolean() ? &#034;\u7537&#034; : &#034;\u5973&#034;;<br \/>\n            String address &#061; &#034;\u6b66\u6c49&#034; &#043; i;<\/p>\n<p>            String sql &#061; &#034;insert into student values(&#034; &#043; i &#043; &#034;,&#039;&#034; &#043; name &#043; &#034;&#039;,&#034; &#043; age &#043; &#034;,&#039;&#034; &#043; sex &#043; &#034;&#039;,&#039;&#034; &#043; address &#043; &#034;&#039;)&#034;;<br \/>\n            int n &#061; stmt.executeUpdate(sql);<br \/>\n            if (n &gt; 0) {<br \/>\n                System.out.println(&#034;\u6dfb\u52a0\u6210\u529f&#034;);<br \/>\n            } else {<br \/>\n                System.out.println(&#034;\u6dfb\u52a0\u5931\u8d25&#034;);<br \/>\n            }<br \/>\n        }<br \/>\n        DBUtils.close(stmt);<br \/>\n        DBUtils.close(conn);<br \/>\n    }<br \/>\n}<\/p>\n<h4>8.2 \u67e5\u8be2\u6848\u4f8b\u5b9e\u73b0<\/h4>\n<p>java<\/p>\n<p>package com.hg.jdbc;<\/p>\n<p>import com.hg.utils.DBUtils;<br \/>\nimport java.sql.Connection;<br \/>\nimport java.sql.ResultSet;<br \/>\nimport java.sql.SQLException;<br \/>\nimport java.sql.Statement;<\/p>\n<p>public class Test04Query {<br \/>\n    public static void main(String[] args) throws ClassNotFoundException, SQLException {<br \/>\n        Connection conn &#061; DBUtils.getConn();<br \/>\n        Statement stmt &#061; conn.createStatement();<br \/>\n        String sql &#061; &#034;select * from student&#034;;<br \/>\n        ResultSet rs &#061; stmt.executeQuery(sql);<br \/>\n        \/\/ \u904d\u5386\u7ed3\u679c\u96c6<br \/>\n        while (rs.next()) {<br \/>\n            int id &#061; rs.getInt(1);<br \/>\n            String name &#061; rs.getString(2);<br \/>\n            int age &#061; rs.getInt(3);<br \/>\n            String sex &#061; rs.getString(4);<br \/>\n            String address &#061; rs.getString(5);<br \/>\n            System.out.println(id &#043; &#034;  &#034; &#043; name &#043; &#034;  &#034; &#043; age &#043; &#034;   &#034; &#043; sex &#043; &#034;   &#034; &#043; address);<br \/>\n        }<br \/>\n        \/\/ \u5173\u95ed\u8d44\u6e90&#xff08;ResultSet\u9700\u5148\u5173\u95ed&#xff09;<br \/>\n        DBUtils.close(rs);<br \/>\n        DBUtils.close(stmt);<br \/>\n        DBUtils.close(conn);<br \/>\n    }<br \/>\n}<\/p>\n<h3>\u4e5d\u3001\u4f7f\u7528 JDBC \u5b8c\u6210\u5206\u9875\u67e5\u8be2<\/h3>\n<p>MySQL \u5206\u9875\u4f9d\u8d56LIMIT\u5173\u952e\u5b57&#xff0c;\u8bed\u6cd5&#xff1a;LIMIT \u8d77\u59cb\u7d22\u5f15, \u6bcf\u9875\u6761\u6570&#xff08;\u8d77\u59cb\u7d22\u5f15\u4ece 0 \u5f00\u59cb&#xff09;\u3002<\/p>\n<h4>9.1 \u5206\u9875\u67e5\u8be2\u6848\u4f8b<\/h4>\n<p>java<\/p>\n<p>package com.hg.jdbc;<\/p>\n<p>import com.hg.utils.DBUtils;<br \/>\nimport java.sql.Connection;<br \/>\nimport java.sql.ResultSet;<br \/>\nimport java.sql.SQLException;<br \/>\nimport java.sql.Statement;<\/p>\n<p>public class Test05QueryForPage {<br \/>\n    public static void main(String[] args) throws ClassNotFoundException, SQLException {<br \/>\n        Connection conn &#061; DBUtils.getConn();<br \/>\n        Statement stmt &#061; conn.createStatement();<br \/>\n        int pageNum &#061; 2; \/\/ \u9875\u7801&#xff08;\u67e5\u8be2\u7b2c2\u9875&#xff09;<br \/>\n        int pageSize &#061; 5; \/\/ \u6bcf\u9875\u663e\u793a5\u6761<br \/>\n        \/\/ \u8ba1\u7b97\u8d77\u59cb\u7d22\u5f15&#xff1a;(\u9875\u7801-1)*\u6bcf\u9875\u6761\u6570<br \/>\n        String sql &#061; &#034;select * from student limit &#034; &#043; (pageNum &#8211; 1) * pageSize &#043; &#034;,&#034; &#043; pageSize;<br \/>\n        ResultSet rs &#061; stmt.executeQuery(sql);<br \/>\n        while (rs.next()) {<br \/>\n            int id &#061; rs.getInt(1);<br \/>\n            String name &#061; rs.getString(2);<br \/>\n            int age &#061; rs.getInt(3);<br \/>\n            String sex &#061; rs.getString(4);<br \/>\n            String address &#061; rs.getString(5);<br \/>\n            System.out.println(id &#043; &#034;  &#034; &#043; name &#043; &#034;  &#034; &#043; age &#043; &#034;   &#034; &#043; sex &#043; &#034;   &#034; &#043; address);<br \/>\n        }<br \/>\n        DBUtils.close(rs);<br \/>\n        DBUtils.close(stmt);<br \/>\n        DBUtils.close(conn);<br \/>\n    }<br \/>\n}<\/p>\n<h3>\u5341\u3001SQL \u6ce8\u5165\u95ee\u9898<\/h3>\n<h4>10.1 \u95ee\u9898\u5f15\u5165<\/h4>\n<h5>10.1.1 \u51c6\u5907\u6d4b\u8bd5\u73af\u5883<\/h5>\n<p>\u521b\u5efasys_user\u8868\u5e76\u521d\u59cb\u5316\u6570\u636e&#xff1a;<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260129005703-697ab05fac47f.png\" \/><\/p>\n<table>\n<tr>idusernamepassword<\/tr>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>admin<\/td>\n<td>123456<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>zhangsan<\/td>\n<td>123<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>10.1.2 \u5b58\u5728\u6ce8\u5165\u98ce\u9669\u7684\u767b\u5f55\u4ee3\u7801<\/h5>\n<p>java<\/p>\n<p>package com.hg.jdbc;<\/p>\n<p>import com.hg.utils.DBUtils;<br \/>\nimport java.sql.Connection;<br \/>\nimport java.sql.ResultSet;<br \/>\nimport java.sql.SQLException;<br \/>\nimport java.sql.Statement;<br \/>\nimport java.util.Scanner;<\/p>\n<p>public class Test06Login {<br \/>\n    public static void main(String[] args) throws ClassNotFoundException, SQLException {<br \/>\n        Connection conn &#061; DBUtils.getConn();<br \/>\n        Statement stmt &#061; conn.createStatement();<br \/>\n        Scanner scanner &#061; new Scanner(System.in);<br \/>\n        System.out.println(&#034;\u8bf7\u8f93\u5165\u7528\u6237\u540d:&#034;);<br \/>\n        String username &#061; scanner.nextLine();<br \/>\n        System.out.println(&#034;\u8bf7\u8f93\u5165\u5bc6\u7801:&#034;);<br \/>\n        String password &#061; scanner.nextLine();<br \/>\n        \/\/ \u62fc\u63a5SQL&#xff08;\u5b58\u5728\u6ce8\u5165\u98ce\u9669&#xff09;<br \/>\n        String sql &#061; &#034;select * from sys_user where username&#061;&#039;&#034; &#043; username &#043; &#034;&#039; and password&#061;&#039;&#034; &#043; password &#043; &#034;&#039;&#034;;<br \/>\n        ResultSet rs &#061; stmt.executeQuery(sql);<br \/>\n        System.out.println(sql);<br \/>\n        if (rs.next()) {<br \/>\n            System.out.println(&#034;\u767b\u9646\u6210\u529f&#034;);<br \/>\n            int id &#061; rs.getInt(1);<br \/>\n            String name &#061; rs.getString(2);<br \/>\n            String pwd &#061; rs.getString(3);<br \/>\n            System.out.println(id &#043; &#034;  &#034; &#043; name &#043; &#034;  &#034; &#043; pwd);<br \/>\n        }<br \/>\n        DBUtils.close(rs);<br \/>\n        DBUtils.close(stmt);<br \/>\n        DBUtils.close(conn);<br \/>\n    }<br \/>\n}<\/p>\n<h5>10.1.3 \u6d4b\u8bd5\u6ce8\u5165\u653b\u51fb<\/h5>\n<p>\u8f93\u5165\u4ee5\u4e0b\u5185\u5bb9&#xff0c;\u65e0\u9700\u6b63\u786e\u8d26\u53f7\u5bc6\u7801\u5373\u53ef\u767b\u5f55&#xff1a;<\/p>\n<ul>\n<li>\u7528\u6237\u540d&#xff1a;aaa<\/li>\n<li>\u5bc6\u7801&#xff1a;aaa&#039;or&#039;1&#039;&#061;&#039;1<\/li>\n<li>\u62fc\u63a5\u540e\u7684 SQL&#xff1a;select * from sys_user where username&#061;&#039;aaa&#039; and password&#061;&#039;aaa&#039;or&#039;1&#039;&#061;&#039;1&#039;<\/li>\n<li>\u539f\u7406&#xff1a;&#039;1&#039;&#061;&#039;1\u6052\u6210\u7acb&#xff0c;SQL \u6761\u4ef6\u6c38\u8fdc\u4e3a\u771f&#xff0c;\u7ed5\u8fc7\u767b\u5f55\u9a8c\u8bc1\u3002<\/li>\n<\/ul>\n<h4>10.2 \u89e3\u51b3\u529e\u6cd5&#xff1a;\u4f7f\u7528 PreparedStatement<\/h4>\n<h5>10.2.1 \u6280\u672f\u539f\u7406<\/h5>\n<ul>\n<li>PreparedStatement \u9884\u7f16\u8bd1 SQL&#xff0c;\u5360\u4f4d\u7b26?\u66ff\u4ee3\u76f4\u63a5\u62fc\u63a5\u53c2\u6570\u3002<\/li>\n<li>\u53c2\u6570\u901a\u8fc7setXxx()\u65b9\u6cd5\u7ed1\u5b9a&#xff0c;\u6570\u636e\u5e93\u4f1a\u81ea\u52a8\u8fc7\u6ee4\u7279\u6b8a\u5b57\u7b26&#xff0c;\u907f\u514d\u6ce8\u5165\u3002<\/li>\n<li>\u9884\u7f16\u8bd1\u540e\u91cd\u590d\u6267\u884c\u6548\u7387\u66f4\u9ad8\u3002<\/li>\n<\/ul>\n<h5>10.2.2 \u6539\u9020\u540e\u7684\u767b\u5f55\u4ee3\u7801<\/h5>\n<p>java<\/p>\n<p>package com.hg.jdbc;<\/p>\n<p>import com.hg.utils.DBUtils;<br \/>\nimport java.sql.Connection;<br \/>\nimport java.sql.PreparedStatement;<br \/>\nimport java.sql.ResultSet;<br \/>\nimport java.sql.SQLException;<br \/>\nimport java.util.Scanner;<\/p>\n<p>public class Test07Login {<br \/>\n    public static void main(String[] args) throws ClassNotFoundException, SQLException {<br \/>\n        Scanner scanner &#061; new Scanner(System.in);<br \/>\n        System.out.println(&#034;\u8bf7\u8f93\u5165\u7528\u6237\u540d:&#034;);<br \/>\n        String username &#061; scanner.nextLine();<br \/>\n        System.out.println(&#034;\u8bf7\u8f93\u5165\u5bc6\u7801:&#034;);<br \/>\n        String password &#061; scanner.nextLine();<\/p>\n<p>        Connection conn &#061; DBUtils.getConn();<br \/>\n        \/\/ \u4f7f\u7528\u5360\u4f4d\u7b26\u7f16\u5199SQL<br \/>\n        String sql &#061; &#034;select * from sys_user where username&#061;? and password&#061;? &#034;;<br \/>\n        \/\/ \u521b\u5efaPreparedStatement\u5bf9\u8c61&#xff08;\u9884\u7f16\u8bd1SQL&#xff09;<br \/>\n        PreparedStatement pstmt &#061; conn.prepareStatement(sql);<br \/>\n        \/\/ \u7ed1\u5b9a\u53c2\u6570&#xff08;\u7d22\u5f15\u4ece1\u5f00\u59cb&#xff09;<br \/>\n        pstmt.setString(1, username);<br \/>\n        pstmt.setString(2, password);<br \/>\n        ResultSet rs &#061; pstmt.executeQuery();<\/p>\n<p>        if (rs.next()) {<br \/>\n            System.out.println(&#034;\u767b\u9646\u6210\u529f&#034;);<br \/>\n            int id &#061; rs.getInt(1);<br \/>\n            String name &#061; rs.getString(2);<br \/>\n            String pwd &#061; rs.getString(3);<br \/>\n            System.out.println(id &#043; &#034;  &#034; &#043; name &#043; &#034;  &#034; &#043; pwd);<br \/>\n        } else {<br \/>\n            System.out.println(&#034;\u767b\u5f55\u5931\u8d25&#034;);<br \/>\n        }<\/p>\n<p>        DBUtils.close(rs);<br \/>\n        DBUtils.close(pstmt);<br \/>\n        DBUtils.close(conn);<br \/>\n    }<br \/>\n}<\/p>\n<h5>10.2.3 \u6d4b\u8bd5\u9a8c\u8bc1<\/h5>\n<p>\u518d\u6b21\u8f93\u5165\u6ce8\u5165\u5f0f\u5bc6\u7801aaa&#039;or&#039;1&#039;&#061;&#039;1&#xff0c;\u4f1a\u88ab\u5f53\u4f5c\u666e\u901a\u5b57\u7b26\u4e32\u5904\u7406&#xff0c;\u65e0\u6cd5\u7ed5\u8fc7\u9a8c\u8bc1&#xff0c;SQL \u6ce8\u5165\u88ab\u9632\u5fa1\u3002<\/p>\n<h3>\u5341\u4e00\u3001\u4e8b\u52a1\u5904\u7406\u89e3\u51b3\u8f6c\u8d26\u95ee\u9898<\/h3>\n<h4>11.1 \u4ec0\u4e48\u662f\u4e8b\u52a1<\/h4>\n<p>\u4e8b\u52a1\u662f\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u6700\u5c0f\u5de5\u4f5c\u5355\u5143&#xff0c;\u662f\u4e00\u7cfb\u5217\u4e0d\u53ef\u5206\u5272\u7684\u64cd\u4f5c\u96c6\u5408&#xff0c;\u8981\u4e48\u5168\u90e8\u6267\u884c&#xff0c;\u8981\u4e48\u5168\u90e8\u4e0d\u6267\u884c&#xff08;\u907f\u514d\u6570\u636e\u4e0d\u4e00\u81f4&#xff09;\u3002<\/p>\n<h4>11.2 \u4e8b\u52a1\u7684\u56db\u5927\u7279\u6027&#xff08;ACID&#xff09;<\/h4>\n<ul>\n<li>\u539f\u5b50\u6027&#xff08;Atomicity&#xff09;&#xff1a;\u4e8b\u52a1\u4e2d\u64cd\u4f5c\u8981\u4e48\u5168\u6210\u529f&#xff0c;\u8981\u4e48\u5168\u56de\u6eda\u3002<\/li>\n<li>\u4e00\u81f4\u6027&#xff08;Consistency&#xff09;&#xff1a;\u4e8b\u52a1\u6267\u884c\u540e&#xff0c;\u6570\u636e\u5e93\u4ece\u4e00\u4e2a\u4e00\u81f4\u72b6\u6001\u5207\u6362\u5230\u53e6\u4e00\u4e2a\u4e00\u81f4\u72b6\u6001\u3002<\/li>\n<li>\u9694\u79bb\u6027&#xff08;Isolation&#xff09;&#xff1a;\u5e76\u53d1\u4e8b\u52a1\u4e4b\u95f4\u4e92\u4e0d\u5e72\u6270\u3002<\/li>\n<li>\u6301\u4e45\u6027&#xff08;Durability&#xff09;&#xff1a;\u4e8b\u52a1\u63d0\u4ea4\u540e&#xff0c;\u6570\u636e\u4fee\u6539\u6c38\u4e45\u6709\u6548\u3002<\/li>\n<\/ul>\n<h4>11.3 \u9700\u6c42\u63cf\u8ff0<\/h4>\n<p>\u5b9e\u73b0\u8f6c\u8d26\u529f\u80fd&#xff1a;\u5f20\u4e09\u5411\u674e\u56db\u8f6c\u8d26 1000 \u5143&#xff0c;\u8981\u6c42\u6263\u51cf\u5f20\u4e09\u4f59\u989d\u548c\u589e\u52a0\u674e\u56db\u4f59\u989d\u4e24\u4e2a\u64cd\u4f5c\u8981\u4e48\u5168\u6210\u529f&#xff0c;\u8981\u4e48\u5168\u5931\u8d25\u3002<\/p>\n<h4>11.4 \u51c6\u5907\u5de5\u4f5c&#xff08;\u521b\u5efa account \u8868&#xff09;<\/h4>\n<p>sql<\/p>\n<p>CREATE TABLE account (<br \/>\n    aid INT PRIMARY KEY AUTO_INCREMENT,<br \/>\n    aname VARCHAR(30) NOT NULL,<br \/>\n    amount DECIMAL(10,2) NOT NULL<br \/>\n);<\/p>\n<p>\u521d\u59cb\u5316\u6570\u636e&#xff1a;<\/p>\n<table>\n<tr>aidanameamount<\/tr>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>\u5f20\u4e09<\/td>\n<td>10000.00<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>\u674e\u56db<\/td>\n<td>10000.00<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>11.5 \u4ee3\u7801\u5b9e\u73b0<\/h4>\n<p>java<\/p>\n<p>package com.hg.jdbc;<\/p>\n<p>import com.hg.utils.DBUtils;<br \/>\nimport java.sql.Connection;<br \/>\nimport java.sql.SQLException;<br \/>\nimport java.sql.Statement;<\/p>\n<p>public class Test08Transaction {<br \/>\n    public static void main(String[] args) {<br \/>\n        Connection conn &#061; null;<br \/>\n        Statement stmt &#061; null;<br \/>\n        try {<br \/>\n            conn &#061; DBUtils.getConn();<br \/>\n            \/\/ \u5173\u95ed\u81ea\u52a8\u63d0\u4ea4&#xff0c;\u5f00\u542f\u4e8b\u52a1<br \/>\n            conn.setAutoCommit(false);<\/p>\n<p>            \/\/ \u8f6c\u8d26SQL&#xff1a;\u5f20\u4e09\u62631000&#xff0c;\u674e\u56db\u52a01000<br \/>\n            String sql1 &#061; &#034;update account set amount &#061; amount-1000 where aid&#061;1&#034;;<br \/>\n            String sql2 &#061; &#034;update account set amount &#061; amount&#043;1000 where aid&#061;2&#034;;<\/p>\n<p>            stmt &#061; conn.createStatement();<br \/>\n            stmt.executeUpdate(sql1);<br \/>\n            \/\/ \u6a21\u62df\u5f02\u5e38&#xff08;\u6d4b\u8bd5\u4e8b\u52a1\u56de\u6eda&#xff09;&#xff1a;int i &#061; 1\/0;<br \/>\n            stmt.executeUpdate(sql2);<\/p>\n<p>            \/\/ \u65e0\u5f02\u5e38\u5219\u63d0\u4ea4\u4e8b\u52a1<br \/>\n            conn.commit();<br \/>\n            System.out.println(&#034;\u8f6c\u8d26\u6210\u529f&#034;);<br \/>\n        } catch (Exception e) {<br \/>\n            e.printStackTrace();<br \/>\n            \/\/ \u51fa\u73b0\u5f02\u5e38\u5219\u56de\u6eda\u4e8b\u52a1<br \/>\n            try {<br \/>\n                if (conn !&#061; null) {<br \/>\n                    conn.rollback();<br \/>\n                    System.out.println(&#034;\u8f6c\u8d26\u5931\u8d25&#xff0c;\u4e8b\u52a1\u56de\u6eda&#034;);<br \/>\n                }<br \/>\n            } catch (SQLException e1) {<br \/>\n                e1.printStackTrace();<br \/>\n            }<br \/>\n        } finally {<br \/>\n            \/\/ \u5173\u95ed\u8d44\u6e90<br \/>\n            DBUtils.close(stmt);<br \/>\n            DBUtils.close(conn);<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<h4>11.6 \u6d4b\u8bd5\u9a8c\u8bc1<\/h4>\n<ul>\n<li>\u6b63\u5e38\u60c5\u51b5&#xff1a;\u65e0\u5f02\u5e38\u65f6&#xff0c;\u4e8b\u52a1\u63d0\u4ea4&#xff0c;\u5f20\u4e09\u4f59\u989d 9000&#xff0c;\u674e\u56db 11000\u3002<\/li>\n<li>\u5f02\u5e38\u60c5\u51b5&#xff1a;\u5728sql1\u548csql2\u4e4b\u95f4\u6dfb\u52a0int i &#061; 1\/0;\u6a21\u62df\u5f02\u5e38&#xff0c;\u4e8b\u52a1\u56de\u6eda&#xff0c;\u4e24\u4eba\u4f59\u989d\u4fdd\u6301\u521d\u59cb\u503c 10000\u3002<\/li>\n<\/ul>\n<h3>\u5341\u4e8c\u3001BaseDAO \u7684\u5c01\u88c5&#xff08;\u96be\u70b9&#xff09;<\/h3>\n<h4>12.1 \u6982\u8ff0<\/h4>\n<p>ORM&#xff08;\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04&#xff09;\u6846\u67b6&#xff08;\u5982 MyBatis\u3001Hibernate&#xff09;\u53ef\u7b80\u5316 JDBC \u5f00\u53d1&#xff0c;BaseDAO \u901a\u8fc7\u53cd\u5c04\u673a\u5236\u6a21\u62df ORM \u6838\u5fc3\u529f\u80fd&#xff0c;\u5b9e\u73b0\u901a\u7528\u7684 CRUD \u64cd\u4f5c&#xff0c;\u51cf\u5c11\u91cd\u590d\u4ee3\u7801\u3002<\/p>\n<h4>12.2 BaseDAO \u5b9e\u73b0\u4ee3\u7801<\/h4>\n<p>java<\/p>\n<p>import com.hg.utils.DBUtils;<br \/>\nimport java.lang.reflect.Field;<br \/>\nimport java.sql.Connection;<br \/>\nimport java.sql.PreparedStatement;<br \/>\nimport java.sql.ResultSet;<br \/>\nimport java.sql.ResultSetMetaData;<br \/>\nimport java.util.ArrayList;<br \/>\nimport java.util.List;<\/p>\n<p>public class BaseDao {<br \/>\n    \/**<br \/>\n     * \u901a\u7528\u67e5\u8be2&#xff08;\u8fd4\u56de\u591a\u6761\u6570\u636e&#xff09;<br \/>\n     * &#064;param sql SQL\u6307\u4ee4<br \/>\n     * &#064;param clss \u6620\u5c04\u7684\u5b9e\u4f53\u7c7b<br \/>\n     * &#064;param params SQL\u5360\u4f4d\u7b26\u53c2\u6570<br \/>\n     * &#064;param &lt;T&gt; \u5b9e\u4f53\u7c7b\u6cdb\u578b<br \/>\n     * &#064;return \u5b9e\u4f53\u7c7b\u5217\u8868<br \/>\n     *\/<br \/>\n    public &lt;T&gt; List&lt;T&gt; selectList(String sql, Class&lt;T&gt; clss, Object&#8230; params) {<br \/>\n        List&lt;T&gt; data &#061; new ArrayList&lt;&gt;();<br \/>\n        Connection conn &#061; DBUtils.getConn();<br \/>\n        PreparedStatement prep &#061; null;<br \/>\n        ResultSet rs &#061; null;<br \/>\n        try {<br \/>\n            prep &#061; conn.prepareStatement(sql);<br \/>\n            \/\/ \u7ed1\u5b9a\u53c2\u6570<br \/>\n            for (int i &#061; 0; i &lt; params.length; i&#043;&#043;) {<br \/>\n                prep.setObject(i &#043; 1, params[i]);<br \/>\n            }<br \/>\n            \/\/ \u6267\u884c\u67e5\u8be2<br \/>\n            rs &#061; prep.executeQuery();<br \/>\n            ResultSetMetaData metaData &#061; rs.getMetaData();<br \/>\n            int columnCount &#061; metaData.getColumnCount();<\/p>\n<p>            \/\/ \u904d\u5386\u7ed3\u679c\u96c6&#xff0c;\u901a\u8fc7\u53cd\u5c04\u5c01\u88c5\u5b9e\u4f53\u7c7b<br \/>\n            while (rs.next()) {<br \/>\n                T t &#061; clss.newInstance();<br \/>\n                for (int i &#061; 0; i &lt; columnCount; i&#043;&#043;) {<br \/>\n                    \/\/ \u83b7\u53d6\u5217\u522b\u540d&#xff08;\u9700\u4e0e\u5b9e\u4f53\u7c7b\u5c5e\u6027\u540d\u4e00\u81f4&#xff09;<br \/>\n                    String columnLabel &#061; metaData.getColumnLabel(i &#043; 1);<br \/>\n                    \/\/ \u83b7\u53d6\u5217\u503c<br \/>\n                    Object columnValue &#061; rs.getObject(columnLabel);<br \/>\n                    \/\/ \u53cd\u5c04\u8bbe\u7f6e\u5b9e\u4f53\u7c7b\u5c5e\u6027\u503c<br \/>\n                    Field field &#061; clss.getDeclaredField(columnLabel);<br \/>\n                    field.setAccessible(true); \/\/ \u5141\u8bb8\u8bbf\u95ee\u79c1\u6709\u5c5e\u6027<br \/>\n                    field.set(t, columnValue);<br \/>\n                }<br \/>\n                data.add(t);<br \/>\n            }<br \/>\n        } catch (Exception e) {<br \/>\n            e.printStackTrace();<br \/>\n        } finally {<br \/>\n            DBUtils.close(rs);<br \/>\n            DBUtils.close(prep);<br \/>\n            DBUtils.close(conn);<br \/>\n        }<br \/>\n        return data;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u901a\u7528\u67e5\u8be2&#xff08;\u8fd4\u56de\u5355\u6761\u6570\u636e&#xff09;<br \/>\n     * &#064;param sql SQL\u6307\u4ee4<br \/>\n     * &#064;param clss \u6620\u5c04\u7684\u5b9e\u4f53\u7c7b<br \/>\n     * &#064;param params SQL\u5360\u4f4d\u7b26\u53c2\u6570<br \/>\n     * &#064;param &lt;T&gt; \u5b9e\u4f53\u7c7b\u6cdb\u578b<br \/>\n     * &#064;return \u5355\u4e2a\u5b9e\u4f53\u5bf9\u8c61<br \/>\n     *\/<br \/>\n    public &lt;T&gt; T selectOne(String sql, Class&lt;T&gt; clss, Object&#8230; params) {<br \/>\n        List&lt;T&gt; list &#061; selectList(sql, clss, params);<br \/>\n        return list.isEmpty() ? null : list.get(0);<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u901a\u7528\u66f4\u65b0\u64cd\u4f5c&#xff08;INSERT\/UPDATE\/DELETE&#xff09;<br \/>\n     * &#064;param sql SQL\u6307\u4ee4<br \/>\n     * &#064;param params SQL\u5360\u4f4d\u7b26\u53c2\u6570<br \/>\n     * &#064;return \u662f\u5426\u6267\u884c\u6210\u529f<br \/>\n     *\/<br \/>\n    public boolean update(String sql, Object&#8230; params) {<br \/>\n        Connection conn &#061; DBUtils.getConn();<br \/>\n        PreparedStatement prep &#061; null;<br \/>\n        try {<br \/>\n            prep &#061; conn.prepareStatement(sql);<br \/>\n            \/\/ \u7ed1\u5b9a\u53c2\u6570<br \/>\n            for (int i &#061; 0; i &lt; params.length; i&#043;&#043;) {<br \/>\n                prep.setObject(i &#043; 1, params[i]);<br \/>\n            }<br \/>\n            \/\/ \u6267\u884c\u66f4\u65b0&#xff0c;\u8fd4\u56de\u53d7\u5f71\u54cd\u884c\u6570<br \/>\n            int affectedRows &#061; prep.executeUpdate();<br \/>\n            return affectedRows &gt;&#061; 1;<br \/>\n        } catch (SQLException throwables) {<br \/>\n            throwables.printStackTrace();<br \/>\n        } finally {<br \/>\n            DBUtils.close(prep);<br \/>\n            DBUtils.close(conn);<br \/>\n        }<br \/>\n        return false;<br \/>\n    }<br \/>\n}<\/p>\n<h4>12.3 BaseDAO \u7684\u4f7f\u7528<\/h4>\n<p>\u5047\u8bbe\u5b58\u5728User\u5b9e\u4f53\u7c7b&#xff08;\u5c5e\u6027\u4e0e\u6570\u636e\u5e93\u8868\u5b57\u6bb5\u5bf9\u5e94&#xff09;&#xff0c;\u521b\u5efa DAO \u5b9e\u73b0\u7c7b&#xff1a;<\/p>\n<p>java<\/p>\n<p>package com.hg.dao.impl;<\/p>\n<p>import com.hg.dao.BaseDao;<br \/>\nimport com.hg.pojo.User;<\/p>\n<p>public class UserDao extends BaseDao {<br \/>\n    \/**<br \/>\n     * \u6839\u636e\u7528\u6237\u540d\u548c\u5bc6\u7801\u67e5\u8be2\u7528\u6237<br \/>\n     *\/<br \/>\n    public User selectUser(String username, String password) {<br \/>\n        String sql &#061; &#034;select id, username, password, realname, role from user where username &#061; ? and password&#061;?&#034;;<br \/>\n        return super.selectOne(sql, User.class, username, password);<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u66f4\u65b0\u7528\u6237\u5220\u9664\u72b6\u6001<br \/>\n     *\/<br \/>\n    public void updateState(String id, Integer deleted) {<br \/>\n        String sql &#061; &#034;update user set deleted &#061; ?, deleted_time &#061; now() where id &#061; ?&#034;;<br \/>\n        super.update(sql, deleted, id);<br \/>\n    }<br \/>\n}<\/p>\n<h4>12.4 \u5206\u9875\u5c01\u88c5<\/h4>\n<h5>12.4.1 \u521b\u5efa\u5206\u9875\u7ed3\u679c\u5c01\u88c5\u7c7b PageInfo&lt;T&gt;<\/h5>\n<p>java<\/p>\n<p>package com.hg.common;<\/p>\n<p>import java.util.List;<\/p>\n<p>public class PageInfo&lt;T&gt; {<br \/>\n    private List&lt;T&gt; data; \/\/ \u5206\u9875\u6570\u636e<br \/>\n    private Long count; \/\/ \u603b\u6570\u636e\u6761\u6570<\/p>\n<p>    \/\/ getter\/setter<br \/>\n    public List&lt;T&gt; getData() { return data; }<br \/>\n    public void setData(List&lt;T&gt; data) { this.data &#061; data; }<br \/>\n    public Long getCount() { return count; }<br \/>\n    public void setCount(Long count) { this.count &#061; count; }<br \/>\n}<\/p>\n<h5>12.4.2 \u6269\u5c55 BaseDAO \u6dfb\u52a0\u5206\u9875\u65b9\u6cd5<\/h5>\n<p>java<\/p>\n<p>\/**<br \/>\n * \u901a\u7528\u5206\u9875\u67e5\u8be2<br \/>\n * &#064;param sql \u57fa\u7840SQL&#xff08;\u4e0d\u542bLIMIT&#xff09;<br \/>\n * &#064;param cls \u5b9e\u4f53\u7c7b<br \/>\n * &#064;param page \u9875\u7801<br \/>\n * &#064;param limit \u6bcf\u9875\u6761\u6570<br \/>\n * &#064;param &lt;T&gt; \u6cdb\u578b<br \/>\n * &#064;return \u5206\u9875\u7ed3\u679c<br \/>\n *\/<br \/>\nprotected &lt;T&gt; PageInfo&lt;T&gt; selectPage(String sql, Class&lt;T&gt; cls, String page, String limit) {<br \/>\n    \/\/ \u8ba1\u7b97\u8d77\u59cb\u7d22\u5f15<br \/>\n    Integer pageNo &#061; 0;<br \/>\n    if (Integer.parseInt(page) &gt; 0) {<br \/>\n        pageNo &#061; (Integer.parseInt(page) &#8211; 1) * Integer.parseInt(limit);<br \/>\n    }<br \/>\n    \/\/ \u5206\u9875SQL<br \/>\n    String listSql &#061; sql &#043; &#034; limit &#034; &#043; pageNo &#043; &#034;,&#034; &#043; limit;<br \/>\n    \/\/ \u67e5\u8be2\u5206\u9875\u6570\u636e<br \/>\n    List&lt;T&gt; data &#061; selectList(listSql, cls);<br \/>\n    \/\/ \u67e5\u8be2\u603b\u6761\u6570<br \/>\n    Long count &#061; getCount(sql);<br \/>\n    \/\/ \u5c01\u88c5\u5206\u9875\u7ed3\u679c<br \/>\n    PageInfo&lt;T&gt; pageInfo &#061; new PageInfo&lt;&gt;();<br \/>\n    pageInfo.setData(data);<br \/>\n    pageInfo.setCount(count);<br \/>\n    return pageInfo;<br \/>\n}<\/p>\n<p>\/**<br \/>\n * \u67e5\u8be2\u603b\u6570\u636e\u6761\u6570<br \/>\n * &#064;param sql \u57fa\u7840SQL<br \/>\n * &#064;return \u603b\u6761\u6570<br \/>\n *\/<br \/>\nprivate Long getCount(String sql) {<br \/>\n    String countSql &#061; &#034;select count(1) from (&#034; &#043; sql &#043; &#034;) rs&#034;;<br \/>\n    Connection conn &#061; DBUtils.getConn();<br \/>\n    PreparedStatement prep &#061; null;<br \/>\n    ResultSet rs &#061; null;<br \/>\n    try {<br \/>\n        prep &#061; conn.prepareStatement(countSql);<br \/>\n        rs &#061; prep.executeQuery();<br \/>\n        rs.next();<br \/>\n        return rs.getLong(1);<br \/>\n    } catch (SQLException throwables) {<br \/>\n        throwables.printStackTrace();<br \/>\n    } finally {<br \/>\n        DBUtils.close(rs);<br \/>\n        DBUtils.close(prep);<br \/>\n        DBUtils.close(conn);<br \/>\n    }<br \/>\n    return 0L;<br \/>\n}<\/p>\n<h3>\u5341\u4e09\u3001\u6570\u636e\u5e93\u8fde\u63a5\u6c60<\/h3>\n<h4>13.1 \u4ec0\u4e48\u662f\u8fde\u63a5\u6c60<\/h4>\n<p>\u8fde\u63a5\u6c60\u662f\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\u7684\u7f13\u51b2\u6c60\u6280\u672f&#xff0c;\u521d\u59cb\u5316\u65f6\u521b\u5efa\u4e00\u5b9a\u6570\u91cf\u7684\u8fde\u63a5\u5e76\u5b58\u50a8\u5728\u5185\u5b58\u4e2d&#xff0c;\u7a0b\u5e8f\u9700\u8981\u65f6\u4ece\u6c60\u4e2d\u83b7\u53d6&#xff0c;\u4f7f\u7528\u5b8c\u6bd5\u540e\u5f52\u8fd8&#xff08;\u800c\u975e\u5173\u95ed&#xff09;&#xff0c;\u5b9e\u73b0\u8fde\u63a5\u590d\u7528\u3002<\/p>\n<h4>13.2 \u4e3a\u4ec0\u4e48\u4f7f\u7528\u8fde\u63a5\u6c60<\/h4>\n<ul>\n<li>\u8fde\u63a5\u7684\u521b\u5efa\u548c\u9500\u6bc1\u6d88\u8017\u8d44\u6e90&#xff0c;\u590d\u7528\u8fde\u63a5\u53ef\u63d0\u5347\u7cfb\u7edf\u6027\u80fd\u3002<\/li>\n<li>\u63a7\u5236\u6700\u5927\u8fde\u63a5\u6570&#xff0c;\u907f\u514d\u6570\u636e\u5e93\u8fde\u63a5\u8017\u5c3d\u3002<\/li>\n<li>\u7edf\u4e00\u7ba1\u7406\u8fde\u63a5&#xff0c;\u7b80\u5316\u8d44\u6e90\u91ca\u653e\u903b\u8f91\u3002<\/li>\n<\/ul>\n<h4>13.3 \u8fde\u63a5\u6c60\u7684\u5de5\u4f5c\u539f\u7406<\/h4>\n<li>\u670d\u52a1\u5668\u542f\u52a8\u65f6&#xff0c;\u8fde\u63a5\u6c60\u521d\u59cb\u5316\u6307\u5b9a\u6570\u91cf\u7684\u8fde\u63a5\u3002<\/li>\n<li>\u7a0b\u5e8f\u8bf7\u6c42\u8fde\u63a5\u65f6&#xff0c;\u4ece\u6c60\u4e2d\u53d6\u51fa\u7a7a\u95f2\u8fde\u63a5\u3002<\/li>\n<li>\u7a0b\u5e8f\u4f7f\u7528\u5b8c\u6bd5\u540e&#xff0c;\u5c06\u8fde\u63a5\u5f52\u8fd8\u6c60&#xff08;\u800c\u975e\u5173\u95ed&#xff09;\u3002<\/li>\n<li>\u8fde\u63a5\u6c60\u52a8\u6001\u7ef4\u62a4\u8fde\u63a5\u6570\u91cf&#xff08;\u5982\u8fbe\u5230\u6700\u5927\u8fde\u63a5\u6570\u5219\u7b49\u5f85&#xff0c;\u7a7a\u95f2\u8fde\u63a5\u8fc7\u5c11\u5219\u65b0\u589e&#xff09;\u3002<\/li>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260129005703-697ab05fc33cf.png\" \/><\/p>\n<h4>13.4 \u81ea\u5b9a\u4e49\u8fde\u63a5\u6c60<\/h4>\n<p>java<\/p>\n<p>import java.sql.Connection;<br \/>\nimport java.sql.DriverManager;<br \/>\nimport java.sql.SQLException;<br \/>\nimport java.util.LinkedList;<br \/>\nimport java.util.Queue;<\/p>\n<p>public class MyDataSource {<br \/>\n    \/\/ \u5b58\u50a8\u8fde\u63a5\u7684\u961f\u5217<br \/>\n    private static final Queue&lt;Connection&gt; connectionQueue &#061; new LinkedList&lt;&gt;();<br \/>\n    \/\/ \u6570\u636e\u5e93\u914d\u7f6e<br \/>\n    private static final String URL &#061; &#034;jdbc:mysql:\/\/localhost:3306\/jdbc_db&#034;;<br \/>\n    private static final String USER &#061; &#034;root&#034;;<br \/>\n    private static final String PASSWORD &#061; &#034;1111&#034;;<br \/>\n    \/\/ \u521d\u59cb\u8fde\u63a5\u6570<br \/>\n    private int initSize;<\/p>\n<p>    \/**<br \/>\n     * \u6784\u9020\u65b9\u6cd5&#xff1a;\u521d\u59cb\u5316\u8fde\u63a5\u6c60<br \/>\n     * &#064;param initSize \u521d\u59cb\u8fde\u63a5\u6570<br \/>\n     *\/<br \/>\n    public MyDataSource(int initSize) {<br \/>\n        this.initSize &#061; initSize;<br \/>\n        try {<br \/>\n            Class.forName(&#034;com.mysql.jdbc.Driver&#034;);<br \/>\n            \/\/ \u521b\u5efa\u521d\u59cb\u8fde\u63a5\u5e76\u653e\u5165\u961f\u5217<br \/>\n            for (int i &#061; 0; i &lt; initSize; i&#043;&#043;) {<br \/>\n                Connection conn &#061; DriverManager.getConnection(URL, USER, PASSWORD);<br \/>\n                connectionQueue.offer(conn);<br \/>\n            }<br \/>\n            System.out.println(&#034;\u8fde\u63a5\u6c60\u521d\u59cb\u5316\u5b8c\u6210&#xff0c;\u521d\u59cb\u8fde\u63a5\u6570&#xff1a;&#034; &#043; this.initSize);<br \/>\n        } catch (ClassNotFoundException | SQLException e) {<br \/>\n            throw new RuntimeException(&#034;\u8fde\u63a5\u6c60\u521d\u59cb\u5316\u5931\u8d25&#034;, e);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u83b7\u53d6\u8fde\u63a5<br \/>\n     *\/<br \/>\n    public Connection getConnection() {<br \/>\n        Connection conn &#061; connectionQueue.poll();<br \/>\n        if (conn &#061;&#061; null) {<br \/>\n            System.out.println(&#034;\u5f53\u524d\u65e0\u53ef\u7528\u8fde\u63a5&#xff01;&#034;);<br \/>\n            return null;<br \/>\n        }<br \/>\n        System.out.println(&#034;\u83b7\u53d6\u8fde\u63a5\u6210\u529f&#xff0c;\u5269\u4f59\u53ef\u7528\u8fde\u63a5\u6570&#xff1a;&#034; &#043; connectionQueue.size());<br \/>\n        return conn;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u5f52\u8fd8\u8fde\u63a5<br \/>\n     *\/<br \/>\n    public void releaseConnection(Connection conn) {<br \/>\n        if (conn &#061;&#061; null) {<br \/>\n            return;<br \/>\n        }<br \/>\n        connectionQueue.offer(conn);<br \/>\n        System.out.println(&#034;\u8fde\u63a5\u5f52\u8fd8\u6210\u529f&#xff0c;\u5269\u4f59\u53ef\u7528\u8fde\u63a5\u6570&#xff1a;&#034; &#043; connectionQueue.size());<br \/>\n    }<br \/>\n}<\/p>\n<h5>\u6d4b\u8bd5\u81ea\u5b9a\u4e49\u8fde\u63a5\u6c60<\/h5>\n<p>java<\/p>\n<p>import java.sql.Connection;<br \/>\nimport java.sql.PreparedStatement;<br \/>\nimport java.sql.ResultSet;<\/p>\n<p>public class MyDataSourceTest {<br \/>\n    public static void main(String[] args) throws Exception {<br \/>\n        \/\/ 1. \u521b\u5efa\u8fde\u63a5\u6c60&#xff08;\u521d\u59cb5\u4e2a\u8fde\u63a5&#xff09;<br \/>\n        MyDataSource pool &#061; new MyDataSource(5);<\/p>\n<p>        \/\/ 2. \u83b7\u53d6\u8fde\u63a5<br \/>\n        Connection conn1 &#061; pool.getConnection();<\/p>\n<p>        \/\/ 3. \u6267\u884c\u67e5\u8be2<br \/>\n        String sql &#061; &#034;SELECT * FROM student&#034;;<br \/>\n        PreparedStatement pst &#061; conn1.prepareStatement(sql);<br \/>\n        ResultSet rs &#061; pst.executeQuery();<br \/>\n        while (rs.next()) {<br \/>\n            System.out.println(rs.getInt(&#034;id&#034;) &#043; &#034;\\\\t&#034; &#043; rs.getString(&#034;name&#034;) &#043; &#034;\\\\t&#034; &#043; rs.getInt(&#034;age&#034;));<br \/>\n        }<\/p>\n<p>        \/\/ 4. \u5f52\u8fd8\u8fde\u63a5<br \/>\n        pool.releaseConnection(conn1);<\/p>\n<p>        \/\/ 5. \u518d\u6b21\u83b7\u53d6\u8fde\u63a5<br \/>\n        Connection conn2 &#061; pool.getConnection();<br \/>\n        pool.releaseConnection(conn2);<br \/>\n    }<br \/>\n}<\/p>\n<h4>13.5 \u4e3b\u6d41\u8fde\u63a5\u6c60\u4ecb\u7ecd<\/h4>\n<table>\n<tr>\u8fde\u63a5\u6c60\u7279\u70b9<\/tr>\n<tbody>\n<tr>\n<td>C3P0<\/td>\n<td>\u8001\u724c\u8fde\u63a5\u6c60&#xff0c;\u529f\u80fd\u5b8c\u5584\u4f46\u6027\u80fd\u4e00\u822c&#xff0c;\u9010\u6e10\u6dd8\u6c70<\/td>\n<\/tr>\n<tr>\n<td>DBCP<\/td>\n<td>Apache \u5f00\u6e90&#xff0c;\u7a33\u5b9a\u4f46\u914d\u7f6e\u590d\u6742<\/td>\n<\/tr>\n<tr>\n<td>Druid<\/td>\n<td>\u963f\u91cc\u5f00\u6e90&#xff0c;\u6027\u80fd\u4f18\u5f02&#xff0c;\u652f\u6301\u76d1\u63a7\u3001\u9632 SQL \u6ce8\u5165&#xff0c;\u63a8\u8350\u4f7f\u7528<\/td>\n<\/tr>\n<tr>\n<td>Hikari<\/td>\n<td>\u8f7b\u91cf\u7ea7&#xff0c;\u6027\u80fd\u6781\u81f4&#xff0c;SpringBoot \u9ed8\u8ba4\u96c6\u6210<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>13.6 Druid \u8fde\u63a5\u6c60\u5b9e\u6218<\/h4>\n<h5>\u5f15\u5165 Druid \u4f9d\u8d56&#xff08;Maven&#xff09;<\/h5>\n<p>xml<\/p>\n<p>&lt;dependency&gt;<br \/>\n    &lt;groupId&gt;com.alibaba&lt;\/groupId&gt;<br \/>\n    &lt;artifactId&gt;druid&lt;\/artifactId&gt;<br \/>\n    &lt;version&gt;1.2.15&lt;\/version&gt;<br \/>\n&lt;\/dependency&gt;<\/p>\n<h5>Druid \u4f7f\u7528\u6848\u4f8b<\/h5>\n<p>java<\/p>\n<p>import com.alibaba.druid.pool.DruidDataSource;<br \/>\nimport java.sql.Connection;<br \/>\nimport java.sql.PreparedStatement;<br \/>\nimport java.sql.ResultSet;<br \/>\nimport java.sql.SQLException;<\/p>\n<p>public class DruidDataSourceTest {<br \/>\n    \/\/ \u521d\u59cb\u5316Druid\u8fde\u63a5\u6c60<br \/>\n    private static DruidDataSource initDruidDataSource() {<br \/>\n        DruidDataSource ds &#061; new DruidDataSource();<br \/>\n        \/\/ \u57fa\u7840\u914d\u7f6e<br \/>\n        ds.setUrl(&#034;jdbc:mysql:\/\/localhost:3306\/jdbc_db&#034;);<br \/>\n        ds.setUsername(&#034;root&#034;);<br \/>\n        ds.setPassword(&#034;1111&#034;);<br \/>\n        \/\/ \u8fde\u63a5\u6c60\u914d\u7f6e<br \/>\n        ds.setInitialSize(2); \/\/ \u521d\u59cb\u8fde\u63a5\u6570<br \/>\n        ds.setMaxActive(5);   \/\/ \u6700\u5927\u6d3b\u8dc3\u8fde\u63a5\u6570<br \/>\n        ds.setMinIdle(1);     \/\/ \u6700\u5c0f\u7a7a\u95f2\u8fde\u63a5\u6570<br \/>\n        return ds;<br \/>\n    }<\/p>\n<p>    public static void main(String[] args) throws SQLException {<br \/>\n        DruidDataSource ds &#061; initDruidDataSource();<br \/>\n        \/\/ \u83b7\u53d6\u8fde\u63a5&#xff08;Druid\u81ea\u52a8\u7ba1\u7406\u8fde\u63a5\u751f\u547d\u5468\u671f&#xff09;<br \/>\n        Connection conn1 &#061; ds.getConnection();<br \/>\n        System.out.println(&#034;\u83b7\u53d6\u8fde\u63a51\u6210\u529f&#xff0c;\u5269\u4f59\u53ef\u7528\u8fde\u63a5\u6570&#xff1a;&#034; &#043; ds.getPoolingCount());<\/p>\n<p>        \/\/ \u6267\u884c\u67e5\u8be2<br \/>\n        String sql &#061; &#034;SELECT * FROM student&#034;;<br \/>\n        PreparedStatement pst &#061; conn1.prepareStatement(sql);<br \/>\n        ResultSet rs &#061; pst.executeQuery();<br \/>\n        while (rs.next()) {<br \/>\n            System.out.println(rs.getInt(&#034;id&#034;) &#043; &#034;\\\\t&#034; &#043; rs.getString(&#034;name&#034;) &#043; &#034;\\\\t&#034; &#043; rs.getInt(&#034;age&#034;));<br \/>\n        }<\/p>\n<p>        \/\/ \u5f52\u8fd8\u8fde\u63a5&#xff08;Druid\u91cd\u5199close()\u65b9\u6cd5&#xff0c;\u5b9e\u9645\u662f\u5f52\u8fd8\u8fde\u63a5&#xff09;<br \/>\n        conn1.close();<br \/>\n        System.out.println(&#034;\u5f52\u8fd8\u8fde\u63a51&#xff0c;\u5269\u4f59\u53ef\u7528\u8fde\u63a5\u6570&#xff1a;&#034; &#043; ds.getPoolingCount());<\/p>\n<p>        \/\/ \u518d\u6b21\u83b7\u53d6\u8fde\u63a5<br \/>\n        Connection conn2 &#061; ds.getConnection();<br \/>\n        System.out.println(&#034;\u83b7\u53d6\u8fde\u63a52\u6210\u529f&#xff0c;\u5269\u4f59\u53ef\u7528\u8fde\u63a5\u6570&#xff1a;&#034; &#043; ds.getPoolingCount());<br \/>\n        conn2.close();<br \/>\n    }<br \/>\n}<\/p>\n<h4 style=\"background-color:transparent\">13.7 \u6539\u9020 DBUtils \u6574\u5408 Druid<\/h4>\n<p>java<\/p>\n<p>import com.alibaba.druid.pool.DruidDataSource;<br \/>\nimport java.io.InputStream;<br \/>\nimport java.sql.Connection;<br \/>\nimport java.sql.SQLException;<br \/>\nimport java.util.Properties;<\/p>\n<p>public class DBUtils {<br \/>\n    private static String DRIVER;<br \/>\n    private static String URL;<br \/>\n    private static String USER_NAME;<br \/>\n    private static String USER_PASSWORD;<br \/>\n    private static DruidDataSource ds; \/\/ Druid\u8fde\u63a5\u6c60<\/p>\n<p>    static {<br \/>\n        try {<br \/>\n            \/\/ \u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6<br \/>\n            InputStream is &#061; DBUtils.class.getClassLoader().getResourceAsStream(&#034;db.properties&#034;);<br \/>\n            Properties prop &#061; new Properties();<br \/>\n            prop.load(is);<br \/>\n            DRIVER &#061; prop.getProperty(&#034;driverClass&#034;);<br \/>\n            URL &#061; prop.getProperty(&#034;url&#034;);<br \/>\n            USER_NAME &#061; prop.getProperty(&#034;username&#034;);<br \/>\n            USER_PASSWORD &#061; prop.getProperty(&#034;password&#034;);<\/p>\n<p>            \/\/ \u521d\u59cb\u5316Druid\u8fde\u63a5\u6c60<br \/>\n            Class.forName(DRIVER);<br \/>\n            ds &#061; new DruidDataSource();<br \/>\n            ds.setUrl(URL);<br \/>\n            ds.setUsername(USER_NAME);<br \/>\n            ds.setPassword(USER_PASSWORD);<br \/>\n            \/\/ \u53ef\u9009&#xff1a;\u914d\u7f6e\u8fde\u63a5\u6c60\u53c2\u6570<br \/>\n            ds.setInitialSize(2);<br \/>\n            ds.setMaxActive(5);<br \/>\n        } catch (Exception e) {<br \/>\n            e.printStackTrace();<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u83b7\u53d6\u8fde\u63a5&#xff08;\u4eceDruid\u6c60\u83b7\u53d6&#xff09;<br \/>\n    public static Connection getConn() {<br \/>\n        try {<br \/>\n            return ds.getConnection();<br \/>\n        } catch (SQLException e) {<br \/>\n            e.printStackTrace();<br \/>\n            System.out.println(&#034;\u521b\u5efa\u8fde\u63a5\u5bf9\u8c61\u5f02\u5e38&#034;);<br \/>\n        }<br \/>\n        return null;<br \/>\n    }<\/p>\n<p>    \/\/ \u91ca\u653e\u8d44\u6e90&#xff08;Druid\u81ea\u52a8\u5f52\u8fd8\u8fde\u63a5&#xff09;<br \/>\n    public static void close(AutoCloseable closeable) {<br \/>\n        try {<br \/>\n            if (closeable !&#061; null) {<br \/>\n                closeable.close();<br \/>\n            }<br \/>\n        } catch (Exception e) {<br \/>\n            e.printStackTrace();<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u8a00&#xff1a;\u5b66\u4e60\u76ee\u6807<br \/>\n\u77e5\u8bc6\u70b9\u8981\u6c42JDBC \u6982\u8ff0\u638c\u63e1\u4f7f\u7528 JDBC \u5b8c\u6210\u6dfb\u52a0\u64cd\u4f5c\u638c\u63e1\u4f7f\u7528 JDBC \u5b8c\u6210\u66f4\u65b0\u548c\u5220\u9664\u638c\u63e1DBUtils \u7684\u7b80\u5355\u5c01\u88c5\u638c\u63e1\u4f7f\u7528 JDBC \u5b8c\u6210\u67e5\u8be2\u638c\u63e1\u4f7f\u7528 JDBC \u5b8c\u6210\u5206\u9875\u67e5\u8be2\u638c\u63e1\u5e38\u7528\u63a5\u53e3\u8be6\u89e3\u638c\u63e1JDBC \u6279\u5904\u7406\u638c\u63e1SQL \u6ce8\u5165\u95ee\u9898\u638c\u63e1\u4e8b\u52a1\u5904\u7406\u89e3\u51b3\u8f6c\u8d26\u95ee\u9898\u638c\u63e1\u8fde\u63a5\u6c60\u638c\u63e1\u4f7f\u7528\u53cd\u5c04\u5bf9 DBUtils \u518d\u6b21\u7684\u5c01\u88c5\u638c\u63e1BaseDAO \u7684\u5c01\u88c5\u638c\u63e1<br \/>\n\u4e00\u3001JDBC \u6982\u8ff0<br \/>\n1.1 \u4e3a\u4ec0\u4e48\u8981\u5b66\u4e60 JDBC&#xff1f;<br \/>\n\u5982\u7528\u6237\u63d0\u4ea4\u8868\u5355\u65f6&amp;#xff0c<\/p>\n","protected":false},"author":2,"featured_media":67835,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[386,99,87,100],"topic":[],"class_list":["post-67843","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-intellij-idea","tag-java","tag-87","tag-100"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JDBC \u5168\u89e3\u6790\uff1a \u4ece\u5165\u95e8\u5230\u7cbe\u901a - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/67843.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JDBC \u5168\u89e3\u6790\uff1a \u4ece\u5165\u95e8\u5230\u7cbe\u901a - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u5f15\u8a00&#xff1a;\u5b66\u4e60\u76ee\u6807 \u77e5\u8bc6\u70b9\u8981\u6c42JDBC \u6982\u8ff0\u638c\u63e1\u4f7f\u7528 JDBC \u5b8c\u6210\u6dfb\u52a0\u64cd\u4f5c\u638c\u63e1\u4f7f\u7528 JDBC \u5b8c\u6210\u66f4\u65b0\u548c\u5220\u9664\u638c\u63e1DBUtils \u7684\u7b80\u5355\u5c01\u88c5\u638c\u63e1\u4f7f\u7528 JDBC \u5b8c\u6210\u67e5\u8be2\u638c\u63e1\u4f7f\u7528 JDBC \u5b8c\u6210\u5206\u9875\u67e5\u8be2\u638c\u63e1\u5e38\u7528\u63a5\u53e3\u8be6\u89e3\u638c\u63e1JDBC \u6279\u5904\u7406\u638c\u63e1SQL \u6ce8\u5165\u95ee\u9898\u638c\u63e1\u4e8b\u52a1\u5904\u7406\u89e3\u51b3\u8f6c\u8d26\u95ee\u9898\u638c\u63e1\u8fde\u63a5\u6c60\u638c\u63e1\u4f7f\u7528\u53cd\u5c04\u5bf9 DBUtils \u518d\u6b21\u7684\u5c01\u88c5\u638c\u63e1BaseDAO \u7684\u5c01\u88c5\u638c\u63e1 \u4e00\u3001JDBC \u6982\u8ff0 1.1 \u4e3a\u4ec0\u4e48\u8981\u5b66\u4e60 JDBC&#xff1f; \u5982\u7528\u6237\u63d0\u4ea4\u8868\u5355\u65f6&amp;#xff0c\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/67843.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-29T00:57:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260129005702-697ab05ee40f8.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"16 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/67843.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/67843.html\",\"name\":\"JDBC \u5168\u89e3\u6790\uff1a \u4ece\u5165\u95e8\u5230\u7cbe\u901a - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-29T00:57:05+00:00\",\"dateModified\":\"2026-01-29T00:57:05+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/67843.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/67843.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/67843.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JDBC \u5168\u89e3\u6790\uff1a \u4ece\u5165\u95e8\u5230\u7cbe\u901a\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JDBC \u5168\u89e3\u6790\uff1a \u4ece\u5165\u95e8\u5230\u7cbe\u901a - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/67843.html","og_locale":"zh_CN","og_type":"article","og_title":"JDBC \u5168\u89e3\u6790\uff1a \u4ece\u5165\u95e8\u5230\u7cbe\u901a - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u5f15\u8a00&#xff1a;\u5b66\u4e60\u76ee\u6807 \u77e5\u8bc6\u70b9\u8981\u6c42JDBC \u6982\u8ff0\u638c\u63e1\u4f7f\u7528 JDBC \u5b8c\u6210\u6dfb\u52a0\u64cd\u4f5c\u638c\u63e1\u4f7f\u7528 JDBC \u5b8c\u6210\u66f4\u65b0\u548c\u5220\u9664\u638c\u63e1DBUtils \u7684\u7b80\u5355\u5c01\u88c5\u638c\u63e1\u4f7f\u7528 JDBC \u5b8c\u6210\u67e5\u8be2\u638c\u63e1\u4f7f\u7528 JDBC \u5b8c\u6210\u5206\u9875\u67e5\u8be2\u638c\u63e1\u5e38\u7528\u63a5\u53e3\u8be6\u89e3\u638c\u63e1JDBC \u6279\u5904\u7406\u638c\u63e1SQL \u6ce8\u5165\u95ee\u9898\u638c\u63e1\u4e8b\u52a1\u5904\u7406\u89e3\u51b3\u8f6c\u8d26\u95ee\u9898\u638c\u63e1\u8fde\u63a5\u6c60\u638c\u63e1\u4f7f\u7528\u53cd\u5c04\u5bf9 DBUtils \u518d\u6b21\u7684\u5c01\u88c5\u638c\u63e1BaseDAO \u7684\u5c01\u88c5\u638c\u63e1 \u4e00\u3001JDBC \u6982\u8ff0 1.1 \u4e3a\u4ec0\u4e48\u8981\u5b66\u4e60 JDBC&#xff1f; \u5982\u7528\u6237\u63d0\u4ea4\u8868\u5355\u65f6&amp;#xff0c","og_url":"https:\/\/www.wsisp.com\/helps\/67843.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-29T00:57:05+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260129005702-697ab05ee40f8.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"16 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/67843.html","url":"https:\/\/www.wsisp.com\/helps\/67843.html","name":"JDBC \u5168\u89e3\u6790\uff1a \u4ece\u5165\u95e8\u5230\u7cbe\u901a - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-29T00:57:05+00:00","dateModified":"2026-01-29T00:57:05+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/67843.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/67843.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/67843.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"JDBC \u5168\u89e3\u6790\uff1a \u4ece\u5165\u95e8\u5230\u7cbe\u901a"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/67843","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=67843"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/67843\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/67835"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=67843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=67843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=67843"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=67843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}