可以直接来一个完整代码吗
时间: 2024-02-15 12:03:20 浏览: 13
当然,这里提供一个简单的 Java 程序语言连接 MySQL 数据库的管理系统的完整代码,仅供参考:
```java
import java.sql.*;
public class StudentManagementSystem {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/student";
String user = "root";
String password = "password";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 创建连接
conn = DriverManager.getConnection(url, user, password);
// 查询所有学生信息
String sql = "SELECT * FROM student_info";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
System.out.println("所有学生信息如下:");
while (rs.next()) {
System.out.println("姓名:" + rs.getString("name") + ",年龄:" + rs.getInt("age") + ",性别:" + rs.getString("gender"));
}
// 插入一条学生信息
sql = "INSERT INTO student_info (name, age, gender) VALUES ('张三', 18, '男')";
int rows = stmt.executeUpdate(sql);
if (rows > 0) {
System.out.println("成功插入" + rows + "条学生信息");
}
// 更新一条学生信息
sql = "UPDATE student_info SET age = 19 WHERE name = '张三'";
rows = stmt.executeUpdate(sql);
if (rows > 0) {
System.out.println("成功更新" + rows + "条学生信息");
}
// 删除一条学生信息
sql = "DELETE FROM student_info WHERE name = '张三'";
rows = stmt.executeUpdate(sql);
if (rows > 0) {
System.out.println("成功删除" + rows + "条学生信息");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
请注意,这只是一个简单的示例代码,实际开发中需要考虑更多的细节问题。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)