java中实现mysql的增删查改实验报告
时间: 2023-12-03 11:45:27 浏览: 133
java mysql实现增删改查
实验目的:
1. 了解MySQL数据库的基本操作;
2. 掌握Java连接MySQL数据库的方法;
3. 学会使用Java实现MySQL的增删查改操作。
实验环境:
- 操作系统:Windows 10
- 开发工具:IntelliJ IDEA 2021.1
- 数据库管理工具:Navicat Premium 15
- 数据库:MySQL 8.0
实验步骤:
1. 安装并配置MySQL数据库和Navicat Premium数据库管理工具;
2. 在Navicat Premium中创建一个名为"test"的数据库,并新建一个名为"students"的表,包含以下字段:
| Field | Type | Null | Key | Default | Extra |
| ----- | ------------ | ---- | --- | ------- | -------------- |
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | NO | | NULL | |
| age | int(11) | NO | | NULL | |
| sex | varchar(10) | NO | | NULL | |
3. 使用Java连接MySQL数据库,实现以下操作:
- 插入一条记录;
- 查询所有记录;
- 根据id查询一条记录;
- 根据id更新一条记录;
- 根据id删除一条记录。
```java
import java.sql.*;
public class MySQLDemo {
private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
private static final String USER_NAME = "root";
private static final String PASSWORD = "123456";
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
// 加载驱动
Class.forName(DRIVER);
// 建立连接
connection = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
// 插入一条记录
String insertSql = "INSERT INTO students(name, age, sex) VALUES (?, ?, ?)";
preparedStatement = connection.prepareStatement(insertSql);
preparedStatement.setString(1, "Tom");
preparedStatement.setInt(2, 18);
preparedStatement.setString(3, "Male");
preparedStatement.executeUpdate();
System.out.println("插入成功!");
// 查询所有记录
String selectSql = "SELECT * FROM students";
preparedStatement = connection.prepareStatement(selectSql);
resultSet = preparedStatement.executeQuery();
System.out.println("所有记录:");
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
String sex = resultSet.getString("sex");
System.out.println(id + " " + name + " " + age + " " + sex);
}
// 根据id查询一条记录
String selectOneSql = "SELECT * FROM students WHERE id=?";
preparedStatement = connection.prepareStatement(selectOneSql);
preparedStatement.setInt(1, 1);
resultSet = preparedStatement.executeQuery();
System.out.println("id=1的记录:");
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
String sex = resultSet.getString("sex");
System.out.println(id + " " + name + " " + age + " " + sex);
}
// 根据id更新一条记录
String updateSql = "UPDATE students SET age=? WHERE id=?";
preparedStatement = connection.prepareStatement(updateSql);
preparedStatement.setInt(1, 20);
preparedStatement.setInt(2, 1);
preparedStatement.executeUpdate();
System.out.println("更新成功!");
// 根据id删除一条记录
String deleteSql = "DELETE FROM students WHERE id=?";
preparedStatement = connection.prepareStatement(deleteSql);
preparedStatement.setInt(1, 1);
preparedStatement.executeUpdate();
System.out.println("删除成功!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (resultSet != null) {
resultSet.close();
}
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
实验结果:
```
插入成功!
所有记录:
1 Tom 18 Male
id=1的记录:
1 Tom 18 Male
更新成功!
删除成功!
```
实验总结:
通过本次实验,我们学会了如何使用Java连接MySQL数据库,并实现了MySQL的增删查改操作。在实际开发中,我们需要根据自己的需求来进行相应的操作,这些操作可以帮助我们更好地管理数据库,并提高开发效率。
阅读全文