企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 创建表结构和数据 ```mysql CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键', `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '姓名', `age` int(11) NULL DEFAULT NULL COMMENT '年龄', PRIMARY KEY (`id`) USING BTREE ); INSERT INTO `student` VALUES (1, 'Apollo', 26); INSERT INTO `student` VALUES (2, 'Paul', 37); INSERT INTO `student` VALUES (3, 'Tom', 22); ``` # 插入操作 ```java package com.gosuncn; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class ConnectionMain { public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection connect = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root"); String sql = "INSERT INTO `student`(`name`, `age`) VALUES (?, ?)"; PreparedStatement statement = connect.prepareStatement(sql); statement.setString(1, "SXS");// 索引从1开始 statement.setInt(2, 80); boolean flag = statement.execute(); System.out.println(flag); statement.close(); connect.close(); } } ``` # 修改操作 ```java package com.gosuncn; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class ConnectionMain { public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection connect = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root"); String sql = "UPDATE `student` SET `name` = ?, `age` = ? WHERE `id` = 1"; PreparedStatement statement = connect.prepareStatement(sql); statement.setString(1, "Apollo-UPDATE");// 索引从1开始 statement.setInt(2, 1000); boolean flag = statement.execute(); System.out.println(flag); statement.close(); connect.close(); } } ``` # 删除操作 ```java package com.gosuncn; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class ConnectionMain { public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection connect = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root"); String sql = "DELETE FROM student WHERE id = ?"; PreparedStatement statement = connect.prepareStatement(sql); statement.setInt(1, 1); boolean flag = statement.execute(); System.out.println(flag); statement.close(); connect.close(); } } ```