{"id":76512,"date":"2024-10-16T07:20:19","date_gmt":"2024-10-15T23:20:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/76512\/"},"modified":"2024-10-16T07:20:19","modified_gmt":"2024-10-15T23:20:19","slug":"mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e5%92%8cjava%e7%9a%84%e6%99%82%e9%96%93%e9%a1%9e%e5%9e%8b%e8%a9%b3%e7%b4%b0%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/76512\/","title":{"rendered":"MySQL\u6578\u64da\u5eab\u548cJava\u7684\u6642\u9593\u985e\u578b\u8a73\u7d30\u89e3\u6790"},"content":{"rendered":"<h1 id=\"mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e5%92%8cjava%e7%9a%84%e6%99%82%e9%96%93%e9%a1%9e%e5%9e%8b%e8%a9%b3%e7%b4%b0%e8%a7%a3%e6%9e%90-LKKhgBpjor\">MySQL\u6578\u64da\u5eab\u548cJava\u7684\u6642\u9593\u985e\u578b\u8a73\u7d30\u89e3\u6790<\/h1>\n<p>\u5728\u73fe\u4ee3\u61c9\u7528\u7a0b\u5e8f\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u548c\u7de8\u7a0b\u8a9e\u8a00\u4e4b\u9593\u7684\u4e92\u52d5\u81f3\u95dc\u91cd\u8981\u3002MySQL\u4f5c\u70ba\u4e00\u500b\u6d41\u884c\u7684\u958b\u6e90\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u8207Java\u7684\u7d50\u5408\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u9ad8\u6548\u5730\u8655\u7406\u6578\u64da\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eMySQL\u4e2d\u7684\u6642\u9593\u985e\u578b\u53ca\u5176\u5728Java\u4e2d\u7684\u61c9\u7528\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7406\u89e3\u9019\u4e00\u4e3b\u984c\u3002<\/p>\n<h2 id=\"mysql%e4%b8%ad%e7%9a%84%e6%99%82%e9%96%93%e9%a1%9e%e5%9e%8b-LKKhgBpjor\">MySQL\u4e2d\u7684\u6642\u9593\u985e\u578b<\/h2>\n<p>MySQL\u63d0\u4f9b\u4e86\u591a\u7a2e\u6642\u9593\u985e\u578b\uff0c\u4e3b\u8981\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>DATE<\/strong>: \u7528\u65bc\u5b58\u5132\u65e5\u671f\uff0c\u683c\u5f0f\u70ba &#8216;YYYY-MM-DD&#8217;\u3002<\/li>\n<li><strong>TIME<\/strong>: \u7528\u65bc\u5b58\u5132\u6642\u9593\uff0c\u683c\u5f0f\u70ba &#8216;HH:MM:SS&#8217;\u3002<\/li>\n<li><strong>DATETIME<\/strong>: \u7528\u65bc\u5b58\u5132\u65e5\u671f\u548c\u6642\u9593\uff0c\u683c\u5f0f\u70ba &#8216;YYYY-MM-DD HH:MM:SS&#8217;\u3002<\/li>\n<li><strong>TIMESTAMP<\/strong>: \u8207DATETIME\u985e\u4f3c\uff0c\u4f46\u5b83\u6703\u6839\u64da\u6642\u5340\u81ea\u52d5\u8abf\u6574\u3002<\/li>\n<li><strong>YEAR<\/strong>: \u7528\u65bc\u5b58\u5132\u5e74\u4efd\uff0c\u683c\u5f0f\u70ba &#8216;YYYY&#8217;\u3002<\/li>\n<\/ul>\n<p>\u9019\u4e9b\u985e\u578b\u5404\u6709\u5176\u7279\u9ede\uff0c\u958b\u767c\u8005\u9700\u8981\u6839\u64da\u5177\u9ad4\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u985e\u578b\u3002\u4f8b\u5982\uff0c\u82e5\u53ea\u9700\u5b58\u5132\u65e5\u671f\u800c\u4e0d\u9700\u8981\u6642\u9593\uff0c\u5247\u4f7f\u7528DATE\u985e\u578b\u6703\u66f4\u70ba\u5408\u9069\u3002<\/p>\n<h2 id=\"java%e4%b8%ad%e7%9a%84%e6%99%82%e9%96%93%e9%a1%9e%e5%9e%8b-LKKhgBpjor\">Java\u4e2d\u7684\u6642\u9593\u985e\u578b<\/h2>\n<p>Java\u5728\u8655\u7406\u6642\u9593\u548c\u65e5\u671f\u65b9\u9762\u4e5f\u63d0\u4f9b\u4e86\u591a\u7a2e\u985e\u578b\uff0c\u4e3b\u8981\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>java.util.Date<\/strong>: \u65e9\u671f\u7684\u65e5\u671f\u6642\u9593\u985e\u578b\uff0c\u5305\u542b\u65e5\u671f\u548c\u6642\u9593\uff0c\u4f46\u4e0d\u652f\u6301\u6642\u5340\u3002<\/li>\n<li><strong>java.util.Calendar<\/strong>: \u63d0\u4f9b\u4e86\u66f4\u9748\u6d3b\u7684\u65e5\u671f\u6642\u9593\u64cd\u4f5c\uff0c\u4f46\u4f7f\u7528\u8f03\u70ba\u7e41\u7463\u3002<\/li>\n<li><strong>java.time.LocalDate<\/strong>: Java 8\u5f15\u5165\u7684\u985e\u578b\uff0c\u7528\u65bc\u8868\u793a\u4e0d\u5e36\u6642\u5340\u7684\u65e5\u671f\u3002<\/li>\n<li><strong>java.time.LocalTime<\/strong>: \u7528\u65bc\u8868\u793a\u4e0d\u5e36\u6642\u5340\u7684\u6642\u9593\u3002<\/li>\n<li><strong>java.time.LocalDateTime<\/strong>: \u7528\u65bc\u8868\u793a\u4e0d\u5e36\u6642\u5340\u7684\u65e5\u671f\u548c\u6642\u9593\u3002<\/li>\n<li><strong>java.time.ZonedDateTime<\/strong>: \u7528\u65bc\u8868\u793a\u5e36\u6642\u5340\u7684\u65e5\u671f\u548c\u6642\u9593\u3002<\/li>\n<\/ul>\n<p>Java 8\u53ca\u4ee5\u5f8c\u7248\u672c\u7684\u6642\u9593API\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u53ef\u8b80\u6027\u548c\u53ef\u64cd\u4f5c\u6027\uff0c\u5efa\u8b70\u958b\u767c\u8005\u4f7f\u7528\u9019\u4e9b\u65b0\u985e\u578b\u4f86\u8655\u7406\u6642\u9593\u6578\u64da\u3002<\/p>\n<h2 id=\"mysql%e8%88%87java%e7%9a%84%e6%99%82%e9%96%93%e9%a1%9e%e5%9e%8b%e6%98%a0%e5%b0%84-LKKhgBpjor\">MySQL\u8207Java\u7684\u6642\u9593\u985e\u578b\u6620\u5c04<\/h2>\n<p>\u5728Java\u4e2d\u4f7f\u7528JDBC\u9023\u63a5MySQL\u6642\uff0c\u958b\u767c\u8005\u9700\u8981\u6ce8\u610fMySQL\u7684\u6642\u9593\u985e\u578b\u8207Java\u6642\u9593\u985e\u578b\u4e4b\u9593\u7684\u6620\u5c04\u95dc\u4fc2\uff1a<\/p>\n<ul>\n<li><strong>MySQL DATE<\/strong> \u5c0d\u61c9 <strong>java.time.LocalDate<\/strong><\/li>\n<li><strong>MySQL TIME<\/strong> \u5c0d\u61c9 <strong>java.time.LocalTime<\/strong><\/li>\n<li><strong>MySQL DATETIME<\/strong> \u5c0d\u61c9 <strong>java.time.LocalDateTime<\/strong><\/li>\n<li><strong>MySQL TIMESTAMP<\/strong> \u5c0d\u61c9 <strong>java.time.ZonedDateTime<\/strong><\/li>\n<\/ul>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5f9eMySQL\u4e2d\u8b80\u53d6DATETIME\u985e\u578b\u4e26\u8f49\u63db\u70baJava\u7684LocalDateTime\uff1a<\/p>\n<pre><code>import java.sql.Connection;\nimport java.sql.DriverManager;\nimport java.sql.ResultSet;\nimport java.sql.Statement;\nimport java.time.LocalDateTime;\n\npublic class MySQLToJava {\n    public static void main(String[] args) {\n        try {\n            Connection connection = DriverManager.getConnection(\"jdbc:mysql:\/\/localhost:3306\/your_database\", \"username\", \"password\");\n            Statement statement = connection.createStatement();\n            ResultSet resultSet = statement.executeQuery(\"SELECT your_datetime_column FROM your_table\");\n\n            while (resultSet.next()) {\n                LocalDateTime dateTime = resultSet.getTimestamp(\"your_datetime_column\").toLocalDateTime();\n                System.out.println(dateTime);\n            }\n\n            connection.close();\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-LKKhgBpjor\">\u7d50\u8ad6<\/h2>\n<p>\u7406\u89e3MySQL\u548cJava\u4e2d\u7684\u6642\u9593\u985e\u578b\u53ca\u5176\u6620\u5c04\u95dc\u4fc2\u5c0d\u65bc\u958b\u767c\u8005\u4f86\u8aaa\u81f3\u95dc\u91cd\u8981\u3002\u6b63\u78ba\u7684\u9078\u64c7\u548c\u4f7f\u7528\u9019\u4e9b\u985e\u578b\u53ef\u4ee5\u63d0\u9ad8\u6578\u64da\u8655\u7406\u7684\u6548\u7387\u548c\u6e96\u78ba\u6027\u3002\u5728\u9078\u64c7VPS\u6216\u5176\u4ed6\u4f3a\u670d\u5668\u89e3\u6c7a\u65b9\u6848\u6642\uff0c\u78ba\u4fdd\u9078\u64c7\u80fd\u5920\u652f\u6301\u9019\u4e9b\u6280\u8853\u7684\u670d\u52d9\u63d0\u4f9b\u5546\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u82e5\u60a8\u9700\u8981\u66f4\u591a\u6709\u95dc<code><a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a><\/code>\u6216<code><a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a><\/code>\u7684\u8cc7\u8a0a\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u89e3\u6790MySQL\u6578\u64da\u5eab\u8207Java\u7684\u6642\u9593\u985e\u578b\uff0c\u63a2\u8a0e\u5176\u7279\u6027\u3001\u4f7f\u7528\u65b9\u6cd5\u53ca\u6700\u4f73\u5be6\u8e10\uff0c\u52a9\u4f60\u9ad8\u6548\u7ba1\u7406\u6642\u9593\u6578\u64da\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-76512","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76512","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=76512"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76512\/revisions"}],"predecessor-version":[{"id":76513,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76512\/revisions\/76513"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=76512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=76512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=76512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}