{"id":183742,"date":"2024-11-10T07:21:21","date_gmt":"2024-11-09T23:21:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/183742\/"},"modified":"2024-11-10T07:21:22","modified_gmt":"2024-11-09T23:21:22","slug":"%e4%bd%bf%e7%94%a8java%e7%b7%a8%e5%af%ab%e7%88%ac%e8%9f%b2%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e6%8a%80%e5%b7%a7%e5%88%86%e4%ba%ab-java-%e7%88%ac%e8%9f%b2","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/183742\/","title":{"rendered":"\u4f7f\u7528Java\u7de8\u5beb\u722c\u87f2\u5be6\u73fe\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab\u6280\u5de7\u5206\u4eab (java \u722c\u87f2 \u5beb\u5165\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8java%e7%b7%a8%e5%af%ab%e7%88%ac%e8%9f%b2%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e6%8a%80%e5%b7%a7%e5%88%86%e4%ba%ab-AlQlwZDQsu\">\u4f7f\u7528Java\u7de8\u5beb\u722c\u87f2\u5be6\u73fe\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab\u6280\u5de7\u5206\u4eab<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u6642\u4ee3\uff0c\u7db2\u7d61\u722c\u87f2\u6210\u70ba\u4e86\u7372\u53d6\u548c\u5206\u6790\u6578\u64da\u7684\u91cd\u8981\u5de5\u5177\u3002Java\u4f5c\u70ba\u4e00\u7a2e\u5f37\u5927\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u5eab\u548c\u6846\u67b6\u4f86\u8f14\u52a9\u958b\u767c\u722c\u87f2\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Java\u7de8\u5beb\u722c\u87f2\u4e26\u5c07\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab\u7684\u6280\u5de7\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u9ad8\u6548\u5730\u5b8c\u6210\u9019\u4e00\u4efb\u52d9\u3002<\/p>\n<h2 id=\"%e4%b8%80%e3%80%81%e7%88%ac%e8%9f%b2%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-AlQlwZDQsu\">\u4e00\u3001\u722c\u87f2\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u7db2\u7d61\u722c\u87f2\u662f\u4e00\u7a2e\u81ea\u52d5\u8a2a\u554f\u4e92\u806f\u7db2\u4e26\u63d0\u53d6\u4fe1\u606f\u7684\u7a0b\u5e8f\u3002\u722c\u87f2\u901a\u5e38\u6703\u9075\u5faa\u7db2\u7ad9\u7684robots.txt\u6587\u4ef6\uff0c\u78ba\u4fdd\u4e0d\u6703\u9055\u53cd\u7db2\u7ad9\u7684\u4f7f\u7528\u689d\u6b3e\u3002\u722c\u87f2\u7684\u57fa\u672c\u6d41\u7a0b\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u767c\u9001HTTP\u8acb\u6c42\u4ee5\u7372\u53d6\u7db2\u9801\u5167\u5bb9\u3002<\/li>\n<li>\u89e3\u6790HTML\u6587\u6a94\u4ee5\u63d0\u53d6\u6240\u9700\u6578\u64da\u3002<\/li>\n<li>\u5c07\u63d0\u53d6\u7684\u6578\u64da\u5b58\u5132\u5230\u6578\u64da\u5eab\u4e2d\u3002<\/li>\n<\/ul>\n<h2 id=\"%e4%ba%8c%e3%80%81%e4%bd%bf%e7%94%a8java%e7%b7%a8%e5%af%ab%e7%88%ac%e8%9f%b2-AlQlwZDQsu\">\u4e8c\u3001\u4f7f\u7528Java\u7de8\u5beb\u722c\u87f2<\/h2>\n<p>\u5728Java\u4e2d\uff0c\u958b\u767c\u722c\u87f2\u7684\u5e38\u7528\u5eab\u5305\u62ecJsoup\u548cApache HttpClient\u3002\u9019\u4e9b\u5eab\u63d0\u4f9b\u4e86\u7c21\u55ae\u7684API\u4f86\u767c\u9001\u8acb\u6c42\u548c\u89e3\u6790HTML\u3002<\/p>\n<h3 id=\"1-%e4%bd%bf%e7%94%a8jsoup%e7%8d%b2%e5%8f%96%e7%b6%b2%e9%a0%81%e5%85%a7%e5%ae%b9-AlQlwZDQsu\">1. \u4f7f\u7528Jsoup\u7372\u53d6\u7db2\u9801\u5167\u5bb9<\/h3>\n<pre><code>import org.jsoup.Jsoup;\nimport org.jsoup.nodes.Document;\n\npublic class WebCrawler {\n    public static void main(String[] args) {\n        try {\n            \/\/ \u7372\u53d6\u7db2\u9801\u5167\u5bb9\n            Document doc = Jsoup.connect(\"https:\/\/example.com\").get();\n            System.out.println(doc.title());\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n}<\/code><\/pre>\n<h3 id=\"2-%e8%a7%a3%e6%9e%90html%e4%b8%a6%e6%8f%90%e5%8f%96%e6%95%b8%e6%93%9a-AlQlwZDQsu\">2. \u89e3\u6790HTML\u4e26\u63d0\u53d6\u6578\u64da<\/h3>\n<p>\u4f7f\u7528Jsoup\u53ef\u4ee5\u8f15\u9b06\u5730\u9078\u64c7\u548c\u63d0\u53d6HTML\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u63d0\u53d6\u6240\u6709\u7684\u93c8\u63a5\uff1a<\/p>\n<pre><code>import org.jsoup.nodes.Element;\nimport org.jsoup.select.Elements;\n\nElements links = doc.select(\"a[href]\");\nfor (Element link : links) {\n    System.out.println(link.attr(\"href\"));\n}<\/code><\/pre>\n<h2 id=\"%e4%b8%89%e3%80%81%e5%b0%87%e6%95%b8%e6%93%9a%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab-AlQlwZDQsu\">\u4e09\u3001\u5c07\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab<\/h2>\n<p>\u5728\u63d0\u53d6\u5230\u6240\u9700\u6578\u64da\u5f8c\uff0c\u4e0b\u4e00\u6b65\u662f\u5c07\u9019\u4e9b\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab\u3002Java\u4e2d\u5e38\u7528\u7684\u6578\u64da\u5eab\u9023\u63a5\u6280\u8853\u662fJDBC\uff08Java Database Connectivity\uff09\u3002\u4ee5\u4e0b\u662f\u5c07\u6578\u64da\u5beb\u5165MySQL\u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e6%b7%bb%e5%8a%a0jdbc%e4%be%9d%e8%b3%b4-AlQlwZDQsu\">1. \u6dfb\u52a0JDBC\u4f9d\u8cf4<\/h3>\n<p>\u5728\u4f60\u7684\u9805\u76ee\u4e2d\u6dfb\u52a0MySQL JDBC\u9a45\u52d5\u7684\u4f9d\u8cf4\uff0c\u4f8b\u5982\u5728Maven\u4e2d\uff1a<\/p>\n<pre><code>&lt;dependency&gt;\n    &lt;groupId&gt;mysql&lt;\/groupId&gt;\n    &lt;artifactId&gt;mysql-connector-java&lt;\/artifactId&gt;\n    &lt;version&gt;8.0.26&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h3 id=\"2-%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-AlQlwZDQsu\">2. \u9023\u63a5\u6578\u64da\u5eab<\/h3>\n<pre><code>import java.sql.Connection;\nimport java.sql.DriverManager;\nimport java.sql.PreparedStatement;\n\npublic class DatabaseUtil {\n    public static Connection getConnection() throws Exception {\n        String url = \"jdbc:mysql:\/\/localhost:3306\/your_database\";\n        String user = \"your_username\";\n        String password = \"your_password\";\n        return DriverManager.getConnection(url, user, password);\n    }\n}<\/code><\/pre>\n<h3 id=\"3-%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a-AlQlwZDQsu\">3. \u5beb\u5165\u6578\u64da<\/h3>\n<p>\u4f7f\u7528PreparedStatement\u4f86\u57f7\u884c\u63d2\u5165\u64cd\u4f5c\uff1a<\/p>\n<pre><code>public void insertData(String link) {\n    String sql = \"INSERT INTO links (url) VALUES (?)\";\n    try (Connection conn = getConnection();\n         PreparedStatement pstmt = conn.prepareStatement(sql)) {\n        pstmt.setString(1, link);\n        pstmt.executeUpdate();\n    } catch (Exception e) {\n        e.printStackTrace();\n    }\n}<\/code><\/pre>\n<h2 id=\"%e5%9b%9b%e3%80%81%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-AlQlwZDQsu\">\u56db\u3001\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u7de8\u5beb\u722c\u87f2\u6642\uff0c\u958b\u767c\u8005\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u9075\u5faa\u7db2\u7ad9\u7684robots.txt\u898f\u7bc4\uff0c\u907f\u514d\u5c0d\u7db2\u7ad9\u9020\u6210\u8ca0\u64d4\u3002<\/li>\n<li>\u8a2d\u7f6e\u5408\u7406\u7684\u8acb\u6c42\u9593\u9694\uff0c\u9632\u6b62\u88ab\u5c01\u7981\u3002<\/li>\n<li>\u8655\u7406\u7570\u5e38\u60c5\u6cc1\uff0c\u78ba\u4fdd\u722c\u87f2\u7684\u7a69\u5b9a\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-AlQlwZDQsu\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528Java\u7de8\u5beb\u722c\u87f2\u4e26\u5c07\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab\u662f\u4e00\u500b\u76f8\u5c0d\u7c21\u55ae\u7684\u904e\u7a0b\uff0c\u53ea\u9700\u638c\u63e1\u57fa\u672c\u7684\u5eab\u548c\u6280\u8853\u3002\u901a\u904e\u672c\u6587\u7684\u4ecb\u7d39\uff0c\u60a8\u61c9\u8a72\u80fd\u5920\u958b\u59cb\u69cb\u5efa\u81ea\u5df1\u7684\u722c\u87f2\uff0c\u4e26\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u5b58\u5132\u3002\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684\u74b0\u5883\u4f86\u904b\u884c\u60a8\u7684\u722c\u87f2\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u670d\u52d9\uff0c\u4ee5\u78ba\u4fdd\u9ad8\u6548\u548c\u7a69\u5b9a\u7684\u6578\u64da\u6293\u53d6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u4f7f\u7528Java\u7de8\u5beb\u722c\u87f2\u7684\u6280\u5de7\uff0c\u5b78\u7fd2\u5982\u4f55\u9ad8\u6548\u5730\u5c07\u6293\u53d6\u7684\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u80fd\u529b\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-183742","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183742","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=183742"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183742\/revisions"}],"predecessor-version":[{"id":183743,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183742\/revisions\/183743"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=183742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=183742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=183742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}