做一个简单的Java加mysql的学生信息管理软件,并写出具体步骤谢谢
时间: 2024-03-07 10:50:03 浏览: 26
好的,下面是我为您提供的Java加mysql的学生信息管理软件的具体步骤:
1. 创建数据库和数据表
首先,我们需要在mysql数据库中创建一个数据库和一个数据表来存储学生信息。可以使用如下的SQL语句来创建:
```sql
CREATE DATABASE student;
USE student;
CREATE TABLE student_info (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(50) DEFAULT NULL,
age INT(11) DEFAULT NULL,
gender VARCHAR(10) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
```
这里我们创建了一个名为`student`的数据库,在该数据库中创建了一个名为`student_info`的数据表,其中包含`id`、`name`、`age`和`gender`四个字段,其中`id`字段为自增长的主键。
2. 连接数据库
在Java程序中,我们需要使用JDBC驱动来连接mysql数据库。可以使用如下的代码来连接数据库:
```java
import java.sql.*;
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/student?useSSL=false&characterEncoding=utf8&serverTimezone=UTC";
private static final String USERNAME = "root";
private static final String PASSWORD = "password";
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return conn;
}
}
```
其中,`URL`表示数据库的连接地址,`USERNAME`和`PASSWORD`表示登录数据库的用户名和密码。在`getConnection()`方法中,我们使用`Class.forName()`方法加载JDBC驱动,然后通过`DriverManager.getConnection()`方法获取数据库连接。
3. 插入学生信息
下面是向数据库中插入学生信息的示例代码:
```java
public class InsertDemo {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DBUtil.getConnection();
String sql = "INSERT INTO student_info(name, age, gender) VALUES(?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "张三");
pstmt.setInt(2, 18);
pstmt.setString(3, "男");
int count = pstmt.executeUpdate();
System.out.println("插入了" + count + "条记录");
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt, null);
}
}
}
```
在该示例中,我们使用`PreparedStatement`对象来执行SQL语句,使用`setString()`和`setInt()`方法来设置参数值,最后调用`executeUpdate()`方法执行SQL语句并返回受影响的记录数。
4. 查询学生信息
下面是从数据库中查询学生信息的示例代码:
```java
public class QueryDemo {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM student_info";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
String gender = rs.getString("gender");
System.out.println("id=" + id + ", name=" + name + ", age=" + age + ", gender=" + gender);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt, rs);
}
}
}
```
在该示例中,我们使用`PreparedStatement`对象来执行SQL查询语句,使用`executeQuery()`方法执行SQL语句并返回一个结果集`ResultSet`,然后使用`ResultSet`对象遍历查询结果。
5. 更新学生信息
下面是更新学生信息的示例代码:
```java
public class UpdateDemo {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DBUtil.getConnection();
String sql = "UPDATE student_info SET age = ? WHERE name = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, 20);
pstmt.setString(2, "张三");
int count = pstmt.executeUpdate();
System.out.println("更新了" + count + "条记录");
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt, null);
}
}
}
```
在该示例中,我们使用`PreparedStatement`对象来执行SQL更新语句,使用`setInt()`和`setString()`方法来设置参数值,最后调用`executeUpdate()`方法执行SQL语句并返回受影响的记录数。
6. 删除学生信息
下面是删除学生信息的示例代码:
```java
public class DeleteDemo {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DBUtil.getConnection();
String sql = "DELETE FROM student_info WHERE name = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "张三");
int count = pstmt.executeUpdate();
System.out.println("删除了" + count + "条记录");
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, pstmt, null);
}
}
}
```
在该示例中,我们使用`PreparedStatement`对象来执行SQL删除语句,使用`setString()`方法来设置参数值,最后调用`executeUpdate()`方法执行SQL语句并返回受影响的记录数。
希望以上步骤可以帮助您完成Java加mysql的学生信息管理软件。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)