{"id":62065,"date":"2024-10-13T15:53:18","date_gmt":"2024-10-13T07:53:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/62065\/"},"modified":"2024-10-13T15:53:18","modified_gmt":"2024-10-13T07:53:18","slug":"java%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c%ef%bc%9a%e6%9b%b4%e6%96%b0%e6%95%b8%e6%93%9a%e7%9a%84%e6%96%b9%e6%b3%95%e8%a9%b3%e8%a7%a3-java%e6%9b%b4%e6%96%b0%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%b8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/62065\/","title":{"rendered":"Java\u6578\u64da\u5eab\u64cd\u4f5c\uff1a\u66f4\u65b0\u6578\u64da\u7684\u65b9\u6cd5\u8a73\u89e3 (java\u66f4\u65b0\u6578\u64da\u5eab\u6578\u64da)"},"content":{"rendered":"<h1 id=\"java%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c%ef%bc%9a%e6%9b%b4%e6%96%b0%e6%95%b8%e6%93%9a%e7%9a%84%e6%96%b9%e6%b3%95%e8%a9%b3%e8%a7%a3-MtfncVJNuN\">Java\u6578\u64da\u5eab\u64cd\u4f5c\uff1a\u66f4\u65b0\u6578\u64da\u7684\u65b9\u6cd5\u8a73\u89e3<\/h1>\n<p>\u5728\u73fe\u4ee3\u61c9\u7528\u7a0b\u5e8f\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u64cd\u4f5c\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002Java\u4f5c\u70ba\u4e00\u7a2e\u5ee3\u6cdb\u4f7f\u7528\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u65b9\u6cd5\u4f86\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\uff0c\u7279\u5225\u662f\u5728\u66f4\u65b0\u6578\u64da\u65b9\u9762\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eJava\u4e2d\u66f4\u65b0\u6578\u64da\u5eab\u6578\u64da\u7684\u65b9\u6cd5\uff0c\u4e26\u63d0\u4f9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"java%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-MtfncVJNuN\">Java\u6578\u64da\u5eab\u9023\u63a5<\/h2>\n<p>\u5728\u9032\u884c\u6578\u64da\u66f4\u65b0\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5efa\u7acb\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002Java\u63d0\u4f9b\u4e86JDBC\uff08Java Database Connectivity\uff09API\u4f86\u5be6\u73fe\u9019\u4e00\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u5efa\u7acb\u6578\u64da\u5eab\u9023\u63a5\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<pre><code>import java.sql.Connection;\nimport java.sql.DriverManager;\nimport java.sql.SQLException;\n\npublic class DatabaseConnection {\n    public static Connection getConnection() {\n        Connection connection = null;\n        try {\n            String url = \"jdbc:mysql:\/\/localhost:3306\/your_database\";\n            String user = \"your_username\";\n            String password = \"your_password\";\n            connection = DriverManager.getConnection(url, user, password);\n        } catch (SQLException e) {\n            e.printStackTrace();\n        }\n        return connection;\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e6%9b%b4%e6%96%b0%e6%95%b8%e6%93%9a%e7%9a%84%e6%96%b9%e6%b3%95-MtfncVJNuN\">\u66f4\u65b0\u6578\u64da\u7684\u65b9\u6cd5<\/h2>\n<p>\u5728Java\u4e2d\uff0c\u66f4\u65b0\u6578\u64da\u901a\u5e38\u4f7f\u7528SQL\u7684UPDATE\u8a9e\u53e5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528JDBC\u9032\u884c\u6578\u64da\u66f4\u65b0\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li>\u5efa\u7acb\u6578\u64da\u5eab\u9023\u63a5\u3002<\/li>\n<li>\u5275\u5efa\u4e00\u500bStatement\u6216PreparedStatement\u5c0d\u8c61\u3002<\/li>\n<li>\u57f7\u884cUPDATE\u8a9e\u53e5\u3002<\/li>\n<li>\u95dc\u9589\u9023\u63a5\u3002<\/li>\n<\/ol>\n<h3 id=\"%e4%bd%bf%e7%94%a8statement%e6%9b%b4%e6%96%b0%e6%95%b8%e6%93%9a-MtfncVJNuN\">\u4f7f\u7528Statement\u66f4\u65b0\u6578\u64da<\/h3>\n<p>\u4f7f\u7528Statement\u5c0d\u8c61\u4f86\u66f4\u65b0\u6578\u64da\u7684\u793a\u4f8b\u4ee3\u78bc\u5982\u4e0b\uff1a<\/p>\n<pre><code>import java.sql.Connection;\nimport java.sql.SQLException;\nimport java.sql.Statement;\n\npublic class UpdateData {\n    public static void updateRecord() {\n        Connection connection = DatabaseConnection.getConnection();\n        Statement statement = null;\n        try {\n            statement = connection.createStatement();\n            String sql = \"UPDATE users SET email='newemail@example.com' WHERE id=1\";\n            int rowsAffected = statement.executeUpdate(sql);\n            System.out.println(\"\u66f4\u65b0\u4e86 \" + rowsAffected + \" \u884c\u6578\u64da\u3002\");\n        } catch (SQLException e) {\n            e.printStackTrace();\n        } finally {\n            try {\n                if (statement != null) statement.close();\n                if (connection != null) connection.close();\n            } catch (SQLException e) {\n                e.printStackTrace();\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<h3 id=\"%e4%bd%bf%e7%94%a8preparedstatement%e6%9b%b4%e6%96%b0%e6%95%b8%e6%93%9a-MtfncVJNuN\">\u4f7f\u7528PreparedStatement\u66f4\u65b0\u6578\u64da<\/h3>\n<p>\u4f7f\u7528PreparedStatement\u5c0d\u8c61\u4f86\u66f4\u65b0\u6578\u64da\u7684\u793a\u4f8b\u4ee3\u78bc\u5982\u4e0b\uff1a<\/p>\n<pre><code>import java.sql.Connection;\nimport java.sql.PreparedStatement;\nimport java.sql.SQLException;\n\npublic class UpdateDataPrepared {\n    public static void updateRecord(int userId, String newEmail) {\n        Connection connection = DatabaseConnection.getConnection();\n        PreparedStatement preparedStatement = null;\n        try {\n            String sql = \"UPDATE users SET email=? WHERE id=?\";\n            preparedStatement = connection.prepareStatement(sql);\n            preparedStatement.setString(1, newEmail);\n            preparedStatement.setInt(2, userId);\n            int rowsAffected = preparedStatement.executeUpdate();\n            System.out.println(\"\u66f4\u65b0\u4e86 \" + rowsAffected + \" \u884c\u6578\u64da\u3002\");\n        } catch (SQLException e) {\n            e.printStackTrace();\n        } finally {\n            try {\n                if (preparedStatement != null) preparedStatement.close();\n                if (connection != null) connection.close();\n            } catch (SQLException e) {\n                e.printStackTrace();\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-MtfncVJNuN\">\u7e3d\u7d50<\/h2>\n<p>\u5728Java\u4e2d\uff0c\u66f4\u65b0\u6578\u64da\u5eab\u6578\u64da\u7684\u904e\u7a0b\u76f8\u5c0d\u7c21\u55ae\uff0c\u4e3b\u8981\u4f9d\u8cf4\u65bcJDBC API\u3002\u7121\u8ad6\u662f\u4f7f\u7528Statement\u9084\u662fPreparedStatement\uff0c\u958b\u767c\u8005\u90fd\u80fd\u5920\u6709\u6548\u5730\u57f7\u884cUPDATE\u64cd\u4f5c\u3002\u9078\u64c7\u4f7f\u7528PreparedStatement\u53ef\u4ee5\u63d0\u9ad8\u5b89\u5168\u6027\uff0c\u9632\u6b62SQL\u6ce8\u5165\u653b\u64ca\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u6258\u7ba1\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u548c\u6578\u64da\u5eab\u3002\u7121\u8ad6\u662f\u5c0f\u578b\u9805\u76ee\u9084\u662f\u5927\u578b\u4f01\u696d\uff0c<a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\u90fd\u80fd\u63d0\u4f9b\u53ef\u9760\u7684\u6027\u80fd\u548c\u5b89\u5168\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u63a2\u8a0eJava\u6578\u64da\u5eab\u64cd\u4f5c\u4e2d\u7684\u6578\u64da\u66f4\u65b0\u65b9\u6cd5\uff0c\u63d0\u4f9b\u5be6\u7528\u7bc4\u4f8b\u8207\u6280\u5de7\uff0c\u52a9\u4f60\u8f15\u9b06\u638c\u63e1\u6578\u64da\u66f4\u65b0\u6d41\u7a0b\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-62065","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/62065","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=62065"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/62065\/revisions"}],"predecessor-version":[{"id":62066,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/62065\/revisions\/62066"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=62065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=62065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=62065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}