{"id":344,"date":"2020-10-31T17:26:55","date_gmt":"2020-10-31T09:26:55","guid":{"rendered":"http:\/\/moyisuiying.com\/?p=344"},"modified":"2020-12-10T14:23:59","modified_gmt":"2020-12-10T06:23:59","slug":"mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e5%85%a5%e9%97%a8%e5%ad%a6%e4%b9%a0%ef%bc%88%e4%b8%89%ef%bc%89-%e4%bd%bf%e7%94%a8%e5%b7%a5%e5%85%b7%e7%b1%bb%e5%b0%81%e8%a3%85jdbc%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"http:\/\/moyisuiying.com\/index.php\/javastudy\/mysql\/344.html","title":{"rendered":"MySQL\u6570\u636e\u5e93\u5165\u95e8\u5b66\u4e60\uff08\u4e09\uff09\u2014\u2014\u4f7f\u7528\u5de5\u5177\u7c7b\u5c01\u88c5JDBC\u5b9e\u73b0\u5bf9\u6570\u636e\u5e93\u7684\u8fde\u63a5"},"content":{"rendered":"\n<h2>MySQL\u6570\u636e\u5e93\u5165\u95e8\u5b66\u4e60\uff08\u4e09\uff09\u2014\u2014\u4f7f\u7528\u5de5\u5177\u7c7b\u5c01\u88c5JDBC\u5b9e\u73b0\u5bf9\u6570\u636e\u5e93\u7684\u8fde\u63a5<\/h2>\n\n\n\n<h3>1.\u524d\u8a00\u56de\u987e<\/h3>\n\n\n\n<p>\u5728\u5bf9MySQL\u6570\u636e\u5e93\u8fdb\u884c\u64cd\u4f5c\u4e4b\u524d\uff0c\u6211\u4eec\u9996\u5148\u901a\u8fc7JDBC\u83b7\u53d6Connection\u5bf9\u8c61\uff0c\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u8981\u52a0\u8f7dMySQL\u6570\u636e\u5e93\u9a71\u52a8\uff0c\u7136\u540e\u624d\u80fd\u901a\u8fc7 DriverManager.getConnection\uff08url,user,password\uff09\u521b\u5efa\u4e00\u4e2a\u8fde\u63a5Connection\u5bf9\u8c61\u3002\u6211\u4eec\u9996\u5148\u89c2\u5bdfJDBC\u4e2d\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u7684\u4ee3\u7801(\u8fd9\u91cc\u53ea\u5217\u51fa\u4e86\u66f4\u65b0update\u548c\u67e5\u8be2select\uff09\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u62fc\u63a5\u9700\u8981\u6267\u884c\u7684\u66f4\u65b0sql\u8bed\u53e5\n        String sql = \"update  book  set name=?,price = ?,status = ?,discount = ?,isBorrowed = ?,createTime = ? where id = ?\";\n        int result = 0;\n        try {\n            \/\/\u52a0\u8f7d\u9a71\u52a8\n            Class.forName(\"com.mysql.cj.jdbc.Driver\");\n        } catch (ClassNotFoundException e) {\n            e.printStackTrace();\n        }\n\n        try {\n            \/\/\u83b7\u53d6\u521b\u5efa\u8fde\u63a5\n            Connection connection = DriverManager.getConnection(\"jdbc:mysql:\/\/localhost:3306\/mysqldemo?serverTimezone=UTC\", \"root\", \"root\");\n            \/\/\u521b\u5efa\u9884\u5904\u7406\u5bf9\u8c61\n            PreparedStatement preparedStatement = connection.prepareStatement(sql);\n            \/\/\u8bbe\u7f6ename\u5360\u4f4d\u7b26\u5bf9\u5e94\u7684\u503c\n            preparedStatement.setString(1, newBook.getName());\n            \/\/\u8bbe\u7f6eprice\u5360\u4f4d\u7b26\u5bf9\u5e94\u7684\u503c\n            preparedStatement.setDouble(2, newBook.getPrice());\n            \/\/\u8bbe\u7f6estatus\u5360\u4f4d\u7b26\u5bf9\u5e94\u7684\u503c\n            preparedStatement.setByte(3, newBook.getStatus());\n            \/\/\u8bbe\u7f6ediscount\u5360\u4f4d\u7b26\u5bf9\u5e94\u7684\u503c\n            preparedStatement.setFloat(4, newBook.getDiscount());\n            \/\/\u8bbe\u7f6eisBorrowed\u5bf9\u5e94\u5360\u4f4d\u7b26\u7684\u503c\n            preparedStatement.setBoolean(5, newBook.isBorrowed());\n            \/\/\u8bbe\u7f6ecreateTime\u5360\u4f4d\u7b26\u5bf9\u5e94\u7684\u503c,\u9700\u8981\u5c06 java.Util.Date \u8f6c\u5316\u4e3a java.sql.Date\u624d\u53ef\u4ee5\n            preparedStatement.setDate(6,new Date(newBook.getCreateTime().getTime()));\n            \/\/\u8bbe\u7f6eid\u5360\u4f4d\u7b26\u5bf9\u5e94\u7684\u503c\n            preparedStatement.setInt(7, newBook.getId());\n            \/\/\u5f00\u59cb\u6267\u884c\u66f4\u65b0,\u5e76\u8fd4\u56de\u5f71\u54cd\u7684\u884c\u6570\n            result = preparedStatement.executeUpdate();\n        } catch (SQLException e) {\n            \/\/ TODO Auto-generated catch block\n            e.printStackTrace();\n        }\n\n\n        return result;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public Book findBookById(int id) {\n        \/\/\u62fc\u63a5\u9700\u8981\u6267\u884c\u7684\u67e5\u627e\u7684sql\u8bed\u53e5\n        String sql = \"select * from book  where id = ?\";\n        \/\/\u7ed3\u679c\u96c6\n        ResultSet resultSet = null;\n        Book book = null;\n        try {\n            \/\/\u52a0\u8f7d\u9a71\u52a8\n            Class.forName(\"com.mysql.cj.jdbc.Driver\");\n        } catch (ClassNotFoundException e) {\n            e.printStackTrace();\n        }\n\n        try {\n            \/\/\u83b7\u53d6\u521b\u5efa\u8fde\u63a5\n            Connection connection = DriverManager.getConnection(\"jdbc:mysql:\/\/localhost:3306\/mysqldemo?serverTimezone=UTC\", \"root\", \"root\");\n            \/\/\u521b\u5efa\u9884\u5904\u7406\u5bf9\u8c61\n            PreparedStatement preparedStatement = connection.prepareStatement(sql);\n            \/\/\u8bbe\u7f6eid\u5360\u4f4d\u7b26\u5bf9\u5e94\u7684\u503c\n            preparedStatement.setInt(1, id);\n            \/\/\u5f00\u59cb\u6267\u884c\u67e5\u8be2\u8bed\u53e5,\u5e76\u8fd4\u56de\u67e5\u8be2\u7684\u7ed3\u679c\u96c6\n            resultSet = preparedStatement.executeQuery();\n            \/\/\u5982\u679c\u67e5\u8be2\u5230\u7ed3\u679c\u4e0d\u4e3a\u7a7a\u800c\u662f\u6709\u8bb0\u5f55\n            if(resultSet.next()) {\n            \/\/\u83b7\u53d6book\u7684name\n            String name = resultSet.getString(\"name\");\n            \/\/\u83b7\u53d6book\u7684status\n            byte status = resultSet.getByte(\"status\");\n            \/\/\u83b7\u53d6book\u7684price\n            double price = resultSet.getDouble(\"price\");\n            \/\/\u83b7\u53d6book\u7684discount\n            float discount = resultSet.getFloat(\"discount\");\n            \/\/\u83b7\u53d6book\u7684isBorrowed\n            boolean isBorrowed = resultSet.getBoolean(\"isBorrowed\");\n            \/\/\u83b7\u53d6book\u7684createTime\n            Date date = resultSet.getDate(\"createTime\");\n            book = new Book();\n            book.setBorrowed(isBorrowed);\n            book.setDiscount(discount);\n            book.setId(id);\n            book.setPrice(price);\n            book.setName(name);\n            book.setStatus(status);\n            book.setCreateTime(date);\n            }\n\n        } catch (SQLException e) {\n            e.printStackTrace();\n            book = null;\n        }\n\n\n        return book;\n\n    }<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u53d1\u73b0\uff0c\u65e0\u8bba\u662f\u66f4\u65b0\u64cd\u4f5c\u8fd8\u662f\u67e5\u627e\u64cd\u4f5c\uff0c\u90fd\u5b58\u5728\u7740\u91cd\u590d\u4ee3\u7801\uff1a<br>\u2460\u52a0\u8f7dMySQL\u6570\u636e\u5e93\u9a71\u52a8\u3002<br>\u2461\u521b\u5efa\u8fde\u63a5\u5bf9\u8c61Connection\u3002<br>\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u5de5\u5177\u7c7b\u6765\u5c01\u88c5\u4e00\u4e2a\u83b7\u53d6Connection\u5bf9\u8c61\u7684\u65b9\u6cd5\uff0c\u4ece\u800c\u8fbe\u5230\u4ee3\u7801\u590d\u7528\uff0c\u51cf\u5c11\u91cd\u590d\u7684\u4ee3\u7801\u3002\u540c\u65f6\u6211\u4eec\u8fde\u63a5MySQL\u6570\u636e\u7684\u914d\u7f6e\u4fe1\u606f\u6bd4\u5982 url\uff0cuser,password\u7b49\u4fe1\u606f\u53ef\u4ee5\u914d\u7f6e\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u4ee5\u540e\u6211\u4eec\u4fee\u6539\u914d\u7f6e\u4fe1\u606f\u65f6\u53ea\u9700\u8981\u5728\u6587\u4ef6\u4e2d\u4fee\u6539\u5c31\u597d\u4e86\uff0c\u4e0d\u7528\u53bb\u4fee\u6539\u6e90\u4ee3\u7801\uff0c\u4ece\u800c\u964d\u4f4e\u4ee3\u7801\u4e4b\u95f4\u7684\u8026\u5408\u5ea6\u3002\u5728\u6b64\u6b21\u4e2d\u6211\u4eec\u5c06\u94fe\u63a5\u6570\u636e\u5e93\u6240\u9700\u8981\u7684\u4fe1\u606f\u5b58\u50a8\u5230\u4e00\u4e2a mysql.properties,\u91c7\u7528 key = value\u7684\u5f62\u5f0f\u5b58\u50a8\u952e\u503c\u5bf9\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>driverClassName=com.mysql.cj.jdbc.Driver\nusername=root\npassword=root\nurl=jdbc:mysql:\/\/localhost:3306\/mysqldemo?serverTimezone=UTC<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img   class=\"lazyload\" data-src=\"https:\/\/img-blog.csdnimg.cn\/20201031170944806.png#pic_center\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"\/><\/figure >\n<noscript><img src=\"https:\/\/img-blog.csdnimg.cn\/20201031170944806.png#pic_center\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"\/><\/figure><\/noscript>\n\n\n\n<figure class=\"wp-block-image\"><img   class=\"lazyload\" data-src=\"https:\/\/img-blog.csdnimg.cn\/20201031171011340.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTE4NzAwMjI=,size_16,color_FFFFFF,t_70#pic_center\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"\/><\/figure >\n<noscript><img src=\"https:\/\/img-blog.csdnimg.cn\/20201031171011340.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTE4NzAwMjI=,size_16,color_FFFFFF,t_70#pic_center\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"\/><\/figure><\/noscript>\n\n\n\n<h3>2.\u5728util\u5305\u4e0b\u521b\u5efa\u4e00\u4e2a\u5de5\u5177\u7c7bJDBCUtil.java\u8fdb\u884c\u5c01\u88c5<\/h3>\n\n\n\n<h4>2.1\u9996\u5148\u5728\u9879\u76ee\u7684\u8d44\u6e90\u6587\u4ef6\u5939resources\u4e2d\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6mysql.properties\u5230\u7a0b\u5e8f\u7684 java.util.Properties\u4e2d\u53bb\u3002<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u7528\u4e8e\u52a0\u8f7d\u914d\u7f6e\u4fe1\u606f\u7684properties\n    private static Properties pro  = new Properties();\n    static {\n        \/\/\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\u4f5c\u4e3a\u8f93\u5165\u6d41\n        InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(\"mysql.properties\");\n        try {\n            \/\/\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\u6d41\u5230Properties\u4e2d\n            pro.load(resourceAsStream);\n\n        } catch (IOException e) {\n            e.printStackTrace();\n        }\n\n    }<\/code><\/pre>\n\n\n\n<h4>2.2\u83b7\u53d6Connection\u8fde\u63a5\u5bf9\u8c61<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n *\u83b7\u53d6Connection\u8fde\u63a5\u5bf9\u8c61 \n * @return \u8fd4\u56de Connection\u5bf9\u8c61\n *\/\npublic static Connection getConnection() {\n    Connection connection = null;\n    try {\n        \/\/\u52a0\u8f7d\u6570\u636e\u5e93\u9a71\u52a8\n        String driverClassName = pro.getProperty(\"driverClassName\");\n        \/\/\u52a0\u8f7d\u9a71\u52a8\n        Class.forName(driverClassName);\n    } catch (ClassNotFoundException e1) {\n        e1.printStackTrace();\n    }\n    try {\n        \/\/\u83b7\u53d6\u6570\u636e\u5e93\u7684URL\n         String url = pro.getProperty(\"url\");\n         \/\/\u83b7\u53d6\u7528\u6237\u540d\n         String user =pro.getProperty(\"username\");\n         \/\/\u83b7\u53d6\u5bc6\u7801\n         String password = pro.getProperty(\"password\");\n         \/\/\u83b7\u53d6\u8fde\u63a5\u5bf9\u8c61\n         connection = DriverManager.getConnection(url,user,password);\n\n    } catch (SQLException e) {\n        e.printStackTrace();\n    }\n    return connection;\n\n  }<\/code><\/pre>\n\n\n\n<h4>2.3\u5173\u95ed\u8d44\u6e90<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * \u5173\u95ed\u8d44\u6e90,\u9996\u5148\u5173\u95edresultSet\uff0c\u7136\u540e\u5173\u95edstatement\uff0c\u6700\u540e\u5173\u95edconnection\n * @param connection  \u6570\u636e\u5e93\u8fde\u63a5\u5bf9\u8c61\n * @param statement \u9884\u5904\u7406\u5bf9\u8c61\n * @param resultSet \u7ed3\u679c\u96c6\n *\/\npublic  static void close(Connection connection, Statement statement, ResultSet resultSet){\n      if (resultSet!=null){\n              try {\n                  resultSet.close();\n              } catch (SQLException e) {\n                  e.printStackTrace();\n              }\n      }\n    if (statement!=null){\n        try {\n            statement.close();\n        } catch (SQLException e) {\n            e.printStackTrace();\n        }\n    }\n    if (connection!=null){\n        try {\n            connection.close();\n        } catch (SQLException e) {\n            e.printStackTrace();\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h4>2.4\u5b8c\u6574\u7684\u5de5\u5177\u7c7b\u4ee3\u7801<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>package util;\nimport java.io.IOException;\nimport java.io.InputStream;\nimport java.sql.Connection;\nimport java.sql.DriverManager;\nimport java.sql.ResultSet;\nimport java.sql.SQLException;\nimport java.sql.Statement;\nimport java.util.Properties;\n\n\/**\n * @author \u964c\u610f\u968f\u5f71\n TODO :JDBC\u8fde\u63a5\u6570\u636e\u5e93\u5de5\u5177\u7c7b\n *2020\u5e7410\u670831\u65e5  \u4e0b\u53484:17:50\n *\/\npublic class JDBCUtil {\n    \/\/\u7528\u4e8e\u52a0\u8f7d\u914d\u7f6e\u4fe1\u606f\u7684properties\n    private static Properties pro  = new Properties();\n    static {\n        \/\/\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\u4f5c\u4e3a\u8f93\u5165\u6d41\n        InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(\"mysql.properties\");\n        try {\n            \/\/\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\u6d41\u5230Properties\u4e2d\n            pro.load(resourceAsStream);\n\n        } catch (IOException e) {\n            e.printStackTrace();\n        }\n\n    }\n \/**\n *\u83b7\u53d6Connection\u8fde\u63a5\u5bf9\u8c61 \n * @return \u8fd4\u56de Connection\u5bf9\u8c61\n *\/\npublic static Connection getConnection() {\n    Connection connection = null;\n    try {\n        \/\/\u52a0\u8f7d\u6570\u636e\u5e93\u9a71\u52a8\n        String driverClassName = pro.getProperty(\"driverClassName\");\n        \/\/\u52a0\u8f7d\u9a71\u52a8\n        Class.forName(driverClassName);\n    } catch (ClassNotFoundException e1) {\n        e1.printStackTrace();\n    }\n    try {\n        \/\/\u83b7\u53d6\u6570\u636e\u5e93\u7684URL\n         String url = pro.getProperty(\"url\");\n         \/\/\u83b7\u53d6\u7528\u6237\u540d\n         String user =pro.getProperty(\"username\");\n         \/\/\u83b7\u53d6\u5bc6\u7801\n         String password = pro.getProperty(\"password\");\n         \/\/\u83b7\u53d6\u8fde\u63a5\u5bf9\u8c61\n         connection = DriverManager.getConnection(url,user,password);\n\n    } catch (SQLException e) {\n        e.printStackTrace();\n    }\n    return connection;\n\n  }\n\/**\n * \u5173\u95ed\u8d44\u6e90,\u9996\u5148\u5173\u95edresultSet\uff0c\u7136\u540e\u5173\u95edstatement\uff0c\u6700\u540e\u5173\u95edconnection\n * @param connection  \u6570\u636e\u5e93\u8fde\u63a5\u5bf9\u8c61\n * @param statement \u9884\u5904\u7406\u5bf9\u8c61\n * @param resultSet \u7ed3\u679c\u96c6\n *\/\npublic  static void close(Connection connection, Statement statement, ResultSet resultSet){\n      if (resultSet!=null){\n              try {\n                  resultSet.close();\n              } catch (SQLException e) {\n                  e.printStackTrace();\n              }\n      }\n    if (statement!=null){\n        try {\n            statement.close();\n        } catch (SQLException e) {\n            e.printStackTrace();\n        }\n    }\n    if (connection!=null){\n        try {\n            connection.close();\n        } catch (SQLException e) {\n            e.printStackTrace();\n        }\n    }\n}\n}<\/code><\/pre>\n\n\n\n<h3>3.\u4fee\u6539BookDao.java\u4e2d\u7684\u7684\u4ee3\u7801\u4f7f\u7528\u5de5\u5177\u7c7bJDBCUtil.java\u83b7\u53d6\u8fde\u63a5\u5bf9\u8c61Connection\u548c\u5173\u95ed\u8d44\u6e90<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package dao;\n\nimport java.sql.Connection;\nimport java.sql.Date;\nimport java.sql.DriverManager;\nimport java.sql.PreparedStatement;\nimport java.sql.ResultSet;\nimport java.sql.SQLException;\nimport java.sql.Statement;\nimport java.util.ArrayList;\nimport java.util.List;\n\nimport entity.Book;\nimport util.JDBCUtil;\n\n\/**\n * @author \u964c\u610f\u968f\u5f71\n TODO : BookDao\n *2020\u5e7410\u670830\u65e5  \u4e0b\u53489:04:08\n *\/\npublic class BookDao  {\n     \/**\n     * @param book \u9700\u8981\u4fdd\u5b58\u7684\u5bf9\u8c61\n     * @return \u4fdd\u5b58\u6210\u529f\u8fd4\u56de1\u5426\u5219\u8fd4\u56de 0\n     *\/\n    public int saveByStatement(Book book) {\n        \/\/\u62fc\u63a5\u9700\u8981\u6267\u884c\u7684sql\u8bed\u53e5\n        String sql = \"insert into book(name,price,status,discount,isBorrowed,createTime) values(\\\"\"+ book.getName()+\"\\\",\\\"\"\n        + book.getPrice()+\"\\\",\\\"\"+book.getStatus()+\"\\\",\\\"\"+book.getDiscount()+\"\\\",\" + book.isBorrowed()+\",\\\"\" + book.getCreateTime().toLocaleString()+\"\\\");\";\n        System.out.println(sql);\n        int result = 0;\n        try {\n            \/\/2.\u83b7\u53d6\u521b\u5efa\u8fde\u63a5\n            Connection connection = JDBCUtil.getConnection();\n            \/\/3.\u521b\u5efa\u9884\u5904\u7406\u5bf9\u8c61\n            Statement statement = connection.createStatement();\n            \/\/4.\u6267\u884csql\u8bed\u53e5\u5e76\u8fd4\u56de\u6267\u884c\u7ed3\u679c\u5f71\u54cd\u7684\u884c\u6570\n            result = statement.executeUpdate(sql);\n            \/\/5.\u91ca\u653e\u8d44\u6e90,\u5148\u5f00\u542f\u7684\u540e\u91ca\u653e\n            JDBCUtil.close(connection, statement, null);\n        } catch (SQLException e) {\n            \/\/ TODO Auto-generated catch block\n            e.printStackTrace();\n        }\n\n       return result;\n    }\n    \/**\n     * @param book \u9700\u8981\u4fdd\u5b58\u7684\u5bf9\u8c61\n     * @return \u4fdd\u5b58\u6210\u529f\u8fd4\u56de1\u5426\u5219\u8fd4\u56de 0\n     *\/\n    public int saveByPrepareStatement(Book book) {\n        \/\/\u62fc\u63a5\u9700\u8981\u6267\u884c\u7684sql\u8bed\u53e5\n        String sql = \"insert into book(name,price,status,discount,isBorrowed,createTime) values(?,?,?,?,?,?)\";\n        int result = 0;\n        try {\n            \/\/\u83b7\u53d6\u521b\u5efa\u8fde\u63a5\n            Connection connection = JDBCUtil.getConnection();\n            \/\/\u521b\u5efa\u9884\u5904\u7406\u5bf9\u8c61\n            PreparedStatement preparedStatement = connection.prepareStatement(sql);\n            \/\/\u8bbe\u7f6ename\u5360\u4f4d\u7b26\u5bf9\u5e94\u7684\u503c\n            preparedStatement.setString(1, book.getName());\n            \/\/\u8bbe\u7f6eprice\u5360\u4f4d\u7b26\u5bf9\u5e94\u7684\u503c\n            preparedStatement.setDouble(2, book.getPrice());\n            \/\/\u8bbe\u7f6estatus\u5360\u4f4d\u7b26\u5bf9\u5e94\u7684\u503c\n            preparedStatement.setByte(3, book.getStatus());\n            \/\/\u8bbe\u7f6ediscount\u5360\u4f4d\u7b26\u5bf9\u5e94\u7684\u503c\n            preparedStatement.setFloat(4, book.getDiscount());\n            \/\/\u8bbe\u7f6eisBorrowed\u5bf9\u5e94\u5360\u4f4d\u7b26\u7684\u503c\n            preparedStatement.setBoolean(5, book.isBorrowed());\n            \/\/\u8bbe\u7f6ecreateTime\u5360\u4f4d\u7b26\u5bf9\u5e94\u7684\u503c,\u9700\u8981\u5c06 java.Util.Date \u8f6c\u5316\u4e3a java.sql.Date\u624d\u53ef\u4ee5\n            preparedStatement.setDate(6,new Date(book.getCreateTime().getTime()));\n            \/\/\u5f00\u59cb\u6267\u884c,\u5e76\u8fd4\u56de\u5f71\u54cd\u7684\u884c\u6570\n            result = preparedStatement.executeUpdate();\n            \/\/\u91ca\u653e\u8d44\u6e90,\u5148\u5f00\u542f\u7684\u540e\u91ca\u653e\n            JDBCUtil.close(connection, preparedStatement, null);\n\n        } catch (SQLException e) {\n            \/\/ TODO Auto-generated catch block\n            e.printStackTrace();\n        }\n\n       return result;\n    }\n\n\n    \/**\n     * \u66f4\u65b0book\n     * @param newBook\n     * @return \u8fd4\u56de\u53d7\u5f71\u54cd\u7684\u884c\u6570\n     *\/\n    public int updateBookByPreparedStatement(Book newBook) {\n\n        \/\/\u62fc\u63a5\u9700\u8981\u6267\u884c\u7684\u66f4\u65b0sql\u8bed\u53e5\n        String sql = \"update  book  set name=?,price = ?,status = ?,discount = ?,isBorrowed = ?,createTime = ? where id = ?\";\n        int result = 0;\n        try {\n            \/\/\u83b7\u53d6\u521b\u5efa\u8fde\u63a5\n            Connection connection = JDBCUtil.getConnection();\n            \/\/\u521b\u5efa\u9884\u5904\u7406\u5bf9\u8c61\n            PreparedStatement preparedStatement = connection.prepareStatement(sql);\n            \/\/\u8bbe\u7f6ename\u5360\u4f4d\u7b26\u5bf9\u5e94\u7684\u503c\n            preparedStatement.setString(1, newBook.getName());\n            \/\/\u8bbe\u7f6eprice\u5360\u4f4d\u7b26\u5bf9\u5e94\u7684\u503c\n            preparedStatement.setDouble(2, newBook.getPrice());\n            \/\/\u8bbe\u7f6estatus\u5360\u4f4d\u7b26\u5bf9\u5e94\u7684\u503c\n            preparedStatement.setByte(3, newBook.getStatus());\n            \/\/\u8bbe\u7f6ediscount\u5360\u4f4d\u7b26\u5bf9\u5e94\u7684\u503c\n            preparedStatement.setFloat(4, newBook.getDiscount());\n            \/\/\u8bbe\u7f6eisBorrowed\u5bf9\u5e94\u5360\u4f4d\u7b26\u7684\u503c\n            preparedStatement.setBoolean(5, newBook.isBorrowed());\n            \/\/\u8bbe\u7f6ecreateTime\u5360\u4f4d\u7b26\u5bf9\u5e94\u7684\u503c,\u9700\u8981\u5c06 java.Util.Date \u8f6c\u5316\u4e3a java.sql.Date\u624d\u53ef\u4ee5\n            preparedStatement.setDate(6,new Date(newBook.getCreateTime().getTime()));\n            \/\/\u8bbe\u7f6eid\u5360\u4f4d\u7b26\u5bf9\u5e94\u7684\u503c\n            preparedStatement.setInt(7, newBook.getId());\n            \/\/\u5f00\u59cb\u6267\u884c\u66f4\u65b0,\u5e76\u8fd4\u56de\u5f71\u54cd\u7684\u884c\u6570\n            result = preparedStatement.executeUpdate();\n            \/\/\u91ca\u653e\u8d44\u6e90,\u5148\u5f00\u542f\u7684\u540e\u91ca\u653e\n            JDBCUtil.close(connection, preparedStatement, null);\n        } catch (SQLException e) {\n            \/\/ TODO Auto-generated catch block\n            e.printStackTrace();\n        }\n\n\n        return result;\n\n    }\n    \/**\n     * \u5220\u9664book\n     * @param id \u9700\u8981\u5220\u9664\u4e66\u7c4d\u7684ID\n     * @return \u8fd4\u56de\u53d7\u5f71\u54cd\u7684\u884c\u6570\n     *\/\n    public int deleteBookById(int id) {\n\n        \/\/\u62fc\u63a5\u9700\u8981\u6267\u884c\u7684\u5220\u9664\u7684sql\u8bed\u53e5\n        String sql = \"delete from book  where id = ?\";\n        int result = 0;\n\n        try {\n            \/\/\u83b7\u53d6\u521b\u5efa\u8fde\u63a5\n            Connection connection = JDBCUtil.getConnection();\n            \/\/\u521b\u5efa\u9884\u5904\u7406\u5bf9\u8c61\n            PreparedStatement preparedStatement = connection.prepareStatement(sql);\n            \/\/\u8bbe\u7f6eid\u5360\u4f4d\u7b26\u5bf9\u5e94\u7684\u503c\n            preparedStatement.setInt(1, id);\n            \/\/\u5f00\u59cb\u6267\u884c\u66f4\u65b0,\u5e76\u8fd4\u56de\u5f71\u54cd\u7684\u884c\u6570\n            result = preparedStatement.executeUpdate();\n            \/\/\u91ca\u653e\u8d44\u6e90,\u5148\u5f00\u542f\u7684\u540e\u91ca\u653e\n            JDBCUtil.close(connection, preparedStatement, null);\n        } catch (SQLException e) {\n            e.printStackTrace();\n        }\n        return result;\n\n    }\n\n    \/**\n     * \u901a\u8fc7ID\u67e5\u8be2book\n     * @param id \u8981\u67e5\u8be2\u7684book\u7684ID\n     * @return \u8fd4\u56deID\u5bf9\u5e94\u7684book\n     *\/\n    public Book findBookById(int id) {\n        \/\/\u62fc\u63a5\u9700\u8981\u6267\u884c\u7684\u67e5\u627e\u7684sql\u8bed\u53e5\n        String sql = \"select * from book  where id = ?\";\n        \/\/\u7ed3\u679c\u96c6\n        ResultSet resultSet = null;\n        Book book = null;\n        try {\n            \/\/\u83b7\u53d6\u521b\u5efa\u8fde\u63a5\n            Connection connection = JDBCUtil.getConnection();\n            \/\/\u521b\u5efa\u9884\u5904\u7406\u5bf9\u8c61\n            PreparedStatement preparedStatement = connection.prepareStatement(sql);\n            \/\/\u8bbe\u7f6eid\u5360\u4f4d\u7b26\u5bf9\u5e94\u7684\u503c\n            preparedStatement.setInt(1, id);\n            \/\/\u5f00\u59cb\u6267\u884c\u67e5\u8be2\u8bed\u53e5,\u5e76\u8fd4\u56de\u67e5\u8be2\u7684\u7ed3\u679c\u96c6\n            resultSet = preparedStatement.executeQuery();\n            \/\/\u5982\u679c\u67e5\u8be2\u5230\u7ed3\u679c\u4e0d\u4e3a\u7a7a\u800c\u662f\u6709\u8bb0\u5f55\n            if(resultSet.next()) {\n            \/\/\u83b7\u53d6book\u7684name\n            String name = resultSet.getString(\"name\");\n            \/\/\u83b7\u53d6book\u7684status\n            byte status = resultSet.getByte(\"status\");\n            \/\/\u83b7\u53d6book\u7684price\n            double price = resultSet.getDouble(\"price\");\n            \/\/\u83b7\u53d6book\u7684discount\n            float discount = resultSet.getFloat(\"discount\");\n            \/\/\u83b7\u53d6book\u7684isBorrowed\n            boolean isBorrowed = resultSet.getBoolean(\"isBorrowed\");\n            \/\/\u83b7\u53d6book\u7684createTime\n            Date date = resultSet.getDate(\"createTime\");\n            book = new Book();\n            book.setBorrowed(isBorrowed);\n            book.setDiscount(discount);\n            book.setId(id);\n            book.setPrice(price);\n            book.setName(name);\n            book.setStatus(status);\n            book.setCreateTime(date);\n            }\n            \/\/\u91ca\u653e\u8d44\u6e90,\u5148\u5f00\u542f\u7684\u540e\u91ca\u653e\n            JDBCUtil.close(connection, preparedStatement, resultSet);\n        } catch (SQLException e) {\n            e.printStackTrace();\n            book = null;\n        }\n\n\n        return book;\n\n    }\n    \/**\n     * \u67e5\u8be2\u6240\u6709\u56fe\u4e66\n     * @return \u8fd4\u56deList&lt;book>\n     *\/\n    public List&lt;Book> findAllBook() {\n        \/\/\u62fc\u63a5\u9700\u8981\u6267\u884c\u7684\u67e5\u627e\u7684sql\u8bed\u53e5\n        String sql = \"select * from book \";\n        \/\/\u7ed3\u679c\u96c6\n        ResultSet resultSet = null;\n        List&lt;Book> bookList = null;\n        try {\n            \/\/\u83b7\u53d6\u521b\u5efa\u8fde\u63a5\n            Connection connection = JDBCUtil.getConnection();\n            \/\/\u521b\u5efa\u9884\u5904\u7406\u5bf9\u8c61\n            PreparedStatement preparedStatement = connection.prepareStatement(sql);\n            \/\/\u5f00\u59cb\u6267\u884c\u67e5\u8be2\u8bed\u53e5,\u5e76\u8fd4\u56de\u67e5\u8be2\u7684\u7ed3\u679c\u96c6\n            resultSet = preparedStatement.executeQuery();\n            bookList = new ArrayList&lt;>();\n            \/\/\u5982\u679c\u67e5\u8be2\u5230\u7ed3\u679c\u4e0d\u4e3a\u7a7a\u800c\u662f\u6709\u8bb0\u5f55\n            while(resultSet.next()) {\n            \/\/\u83b7\u53d6book\u7684ID\n            int id = resultSet.getInt(\"id\");\n            \/\/\u83b7\u53d6book\u7684name\n            String name = resultSet.getString(\"name\");\n            \/\/\u83b7\u53d6book\u7684status\n            byte status = resultSet.getByte(\"status\");\n            \/\/\u83b7\u53d6book\u7684price\n            double price = resultSet.getDouble(\"price\");\n            \/\/\u83b7\u53d6book\u7684discount\n            float discount = resultSet.getFloat(\"discount\");\n            \/\/\u83b7\u53d6book\u7684isBorrowed\n            boolean isBorrowed = resultSet.getBoolean(\"isBorrowed\");\n            \/\/\u83b7\u53d6book\u7684createTime\n            Date date = resultSet.getDate(\"createTime\");\n            Book book = new Book();\n            book.setBorrowed(isBorrowed);\n            book.setDiscount(discount);\n            book.setId(id);\n            book.setPrice(price);\n            book.setName(name);\n            book.setStatus(status);\n            book.setCreateTime(date);\n            bookList.add(book);\n            }\n\n            \/\/\u91ca\u653e\u8d44\u6e90,\u5148\u5f00\u542f\u7684\u540e\u91ca\u653e\n            JDBCUtil.close(connection, preparedStatement, resultSet);\n        } catch (SQLException e) {\n            e.printStackTrace();\n            bookList = null;\n        }\n\n\n        return bookList;\n\n    }\n\n}<\/code><\/pre>\n\n\n\n<h3>4.\u4f7f\u7528JUnit\u6d4b\u8bd5\u5355\u5143\u8fdb\u884c\u6d4b\u8bd5<\/h3>\n\n\n\n<p>\u7ecf\u8fc7\u6d4b\u8bd5BookDao.java\u4e2d\u7684\u6bcf\u4e2a\u65b9\u6cd5\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c<\/p>\n\n\n\n<h3>5.\u672c\u6b21\u5b9e\u9a8c\u73af\u5883<\/h3>\n\n\n\n<p><strong>eclipse2019, jdk1.8,mysql8.0<\/strong><\/p>\n\n\n\n<h3>6.\u6e90\u4ee3\u7801\u5df2\u7ecf\u4e0a\u4f20\u5230\u4e2a\u4eba\u670d\u52a1\u5668\uff0c\u5982\u6709\u9700\u8981\u8bf7\u81ea\u884c\u4e0b\u8f7d\uff1a<\/h3>\n\n\n\n<div class=\"wp-block-file\"><a href=\"http:\/\/moyisuiying.com\/wp-content\/uploads\/2020\/10\/mysqlProject-2.rar\">mysqlProject3<\/a><a href=\"http:\/\/moyisuiying.com\/wp-content\/uploads\/2020\/10\/mysqlProject-2.rar\" class=\"wp-block-file__button\" download>\u4e0b\u8f7d<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>MySQL\u6570\u636e\u5e93\u5165\u95e8\u5b66\u4e60\uff08\u4e09\uff09\u2014\u2014\u4f7f\u7528\u5de5\u5177\u7c7b\u5c01\u88c5JDBC\u5b9e\u73b0\u5bf9\u6570\u636e\u5e93\u7684\u8fde\u63a5 1.\u524d\u8a00\u56de\u987e \u5728\u5bf9MySQL\u6570\u636e\u5e93\u8fdb\u884c\u64cd\u4f5c\u4e4b\u524d\uff0c\u6211\u4eec\u9996 &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[94],"tags":[96,95,104,102],"_links":{"self":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts\/344"}],"collection":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/comments?post=344"}],"version-history":[{"count":1,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts\/344\/revisions"}],"predecessor-version":[{"id":346,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts\/344\/revisions\/346"}],"wp:attachment":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/media?parent=344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/categories?post=344"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/tags?post=344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}