{"id":61915,"date":"2024-10-13T14:41:56","date_gmt":"2024-10-13T06:41:56","guid":{"rendered":"https:\/\/server.hk\/cnblog\/61915\/"},"modified":"2024-10-13T14:41:56","modified_gmt":"2024-10-13T06:41:56","slug":"java%e6%8a%80%e5%b7%a7%ef%bc%9a%e8%bc%95%e9%ac%86%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e5%ba%ab%e8%a1%a8%e6%a0%bc%e5%8f%af%e7%b7%a8%e8%bc%af%e7%95%8c%e9%9d%a2-java%e7%95%8c%e9%9d%a2%e5%8f%af%e6%9b%b4","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/61915\/","title":{"rendered":"Java\u6280\u5de7\uff1a\u8f15\u9b06\u5be6\u73fe\u6578\u64da\u5eab\u8868\u683c\u53ef\u7de8\u8f2f\u754c\u9762 (java\u754c\u9762\u53ef\u66f4\u6539\u8868\u4e2d\u7684\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"java%e6%8a%80%e5%b7%a7%ef%bc%9a%e8%bc%95%e9%ac%86%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e5%ba%ab%e8%a1%a8%e6%a0%bc%e5%8f%af%e7%b7%a8%e8%bc%af%e7%95%8c%e9%9d%a2-tNCloNrBXc\">Java\u6280\u5de7\uff1a\u8f15\u9b06\u5be6\u73fe\u6578\u64da\u5eab\u8868\u683c\u53ef\u7de8\u8f2f\u754c\u9762<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u8edf\u4ef6\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u64cd\u4f5c\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u7121\u8ad6\u662f\u4f01\u696d\u61c9\u7528\u9084\u662f\u500b\u4eba\u9805\u76ee\uff0c\u80fd\u5920\u8f15\u9b06\u5730\u7de8\u8f2f\u6578\u64da\u5eab\u4e2d\u7684\u6578\u64da\u90fd\u662f\u4e00\u9805\u91cd\u8981\u7684\u6280\u80fd\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Java\u4f86\u5be6\u73fe\u4e00\u500b\u53ef\u7de8\u8f2f\u7684\u6578\u64da\u5eab\u8868\u683c\u754c\u9762\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u4ee3\u78bc\u793a\u4f8b\u3002<\/p>\n<h2 id=\"1-%e7%92%b0%e5%a2%83%e6%ba%96%e5%82%99-tNCloNrBXc\">1. \u74b0\u5883\u6e96\u5099<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u5df2\u7d93\u5b89\u88dd\u4e86\u4ee5\u4e0b\u74b0\u5883\uff1a<\/p>\n<ul>\n<li>Java Development Kit (JDK)<\/li>\n<li>\u4e00\u500b\u652f\u6301JDBC\u7684\u6578\u64da\u5eab\uff08\u5982MySQL\u3001PostgreSQL\u7b49\uff09<\/li>\n<li>\u4e00\u500bIDE\uff08\u5982Eclipse\u6216IntelliJ IDEA\uff09<\/li>\n<\/ul>\n<h2 id=\"2-%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-tNCloNrBXc\">2. \u6578\u64da\u5eab\u9023\u63a5<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5efa\u7acb\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528JDBC\u9023\u63a5MySQL\u6578\u64da\u5eab\u7684\u57fa\u672c\u4ee3\u78bc\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 connect() {\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=\"3-%e5%89%b5%e5%bb%ba%e5%8f%af%e7%b7%a8%e8%bc%af%e7%9a%84%e8%a1%a8%e6%a0%bc%e7%95%8c%e9%9d%a2-tNCloNrBXc\">3. \u5275\u5efa\u53ef\u7de8\u8f2f\u7684\u8868\u683c\u754c\u9762<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u4f7f\u7528Swing\u4f86\u5275\u5efa\u4e00\u500b\u53ef\u7de8\u8f2f\u7684\u8868\u683c\u754c\u9762\u3002\u4ee5\u4e0b\u662f\u5275\u5efa\u8868\u683c\u7684\u57fa\u672c\u4ee3\u78bc\uff1a<\/p>\n<pre><code>import javax.swing.*;\nimport javax.swing.table.DefaultTableModel;\nimport java.awt.*;\nimport java.sql.*;\n\npublic class EditableTable {\n    private JTable table;\n    private DefaultTableModel model;\n\n    public EditableTable() {\n        JFrame frame = new JFrame(\"\u53ef\u7de8\u8f2f\u7684\u6578\u64da\u5eab\u8868\u683c\");\n        model = new DefaultTableModel();\n        table = new JTable(model);\n        JScrollPane scrollPane = new JScrollPane(table);\n        \n        frame.add(scrollPane, BorderLayout.CENTER);\n        frame.setSize(600, 400);\n        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\n        frame.setVisible(true);\n        \n        loadData();\n    }\n\n    private void loadData() {\n        Connection connection = DatabaseConnection.connect();\n        try {\n            Statement statement = connection.createStatement();\n            ResultSet resultSet = statement.executeQuery(\"SELECT * FROM your_table\");\n            ResultSetMetaData metaData = resultSet.getMetaData();\n            int columnCount = metaData.getColumnCount();\n\n            for (int i = 1; i &lt;= columnCount; i++) {\n                model.addColumn(metaData.getColumnName(i));\n            }\n\n            while (resultSet.next()) {\n                Object[] row = new Object[columnCount];\n                for (int i = 0; i &lt; columnCount; i++) {\n                    row[i] = resultSet.getObject(i + 1);\n                }\n                model.addRow(row);\n            }\n        } catch (SQLException e) {\n            e.printStackTrace();\n        }\n    }\n\n    public static void main(String[] args) {\n        SwingUtilities.invokeLater(EditableTable::new);\n    }\n}\n<\/code><\/pre>\n<h2 id=\"4-%e6%9b%b4%e6%96%b0%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e7%9a%84%e6%95%b8%e6%93%9a-tNCloNrBXc\">4. \u66f4\u65b0\u6578\u64da\u5eab\u4e2d\u7684\u6578\u64da<\/h2>\n<p>\u7576\u7528\u6236\u5728\u8868\u683c\u4e2d\u7de8\u8f2f\u6578\u64da\u5f8c\uff0c\u6211\u5011\u9700\u8981\u5c07\u66f4\u6539\u4fdd\u5b58\u56de\u6578\u64da\u5eab\u3002\u4ee5\u4e0b\u662f\u66f4\u65b0\u6578\u64da\u7684\u57fa\u672c\u4ee3\u78bc\uff1a<\/p>\n<pre><code>private void updateData(int rowIndex) {\n    Connection connection = DatabaseConnection.connect();\n    try {\n        String query = \"UPDATE your_table SET column1 = ?, column2 = ? WHERE id = ?\";\n        PreparedStatement preparedStatement = connection.prepareStatement(query);\n        preparedStatement.setString(1, model.getValueAt(rowIndex, 0).toString());\n        preparedStatement.setString(2, model.getValueAt(rowIndex, 1).toString());\n        preparedStatement.setInt(3, (Integer) model.getValueAt(rowIndex, 2)); \/\/ \u5047\u8a2dID\u5728\u7b2c\u4e09\u5217\n        preparedStatement.executeUpdate();\n    } catch (SQLException e) {\n        e.printStackTrace();\n    }\n}\n<\/code><\/pre>\n<h2 id=\"5-%e7%b5%90%e8%ab%96-tNCloNrBXc\">5. \u7d50\u8ad6<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5730\u4f7f\u7528Java\u5275\u5efa\u4e00\u500b\u53ef\u7de8\u8f2f\u7684\u6578\u64da\u5eab\u8868\u683c\u754c\u9762\u3002\u9019\u4e0d\u50c5\u63d0\u9ad8\u4e86\u7528\u6236\u7684\u64cd\u4f5c\u6548\u7387\uff0c\u4e5f\u4f7f\u5f97\u6578\u64da\u7ba1\u7406\u8b8a\u5f97\u66f4\u52a0\u76f4\u89c0\u3002\u96a8\u8457\u6280\u8853\u7684\u767c\u5c55\uff0c\u9019\u7a2e\u754c\u9762\u5728\u5404\u985e\u61c9\u7528\u4e2d\u5c07\u8d8a\u4f86\u8d8a\u666e\u904d\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u90e8\u7f72\u60a8\u7684Java\u61c9\u7528\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Java\u6280\u5de7\uff0c\u8f15\u9b06\u5be6\u73fe\u53ef\u7de8\u8f2f\u7684\u6578\u64da\u5eab\u8868\u683c\u754c\u9762\uff0c\u8b93\u7528\u6236\u65b9\u4fbf\u5730\u66f4\u6539\u6578\u64da\u5eab\u4e2d\u7684\u6578\u64da\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-61915","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61915","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=61915"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61915\/revisions"}],"predecessor-version":[{"id":61916,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61915\/revisions\/61916"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=61915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=61915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=61915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}