一会再试试~~ 编写java代码作业:在数据库中新建一张教师信息表 Teachlnfo,字段有教师编号TeachID、姓名TName、办公室Toffice。编写教师信息的增、改、查功能,并在导航页面中设置相关的导航项。
时间: 2024-02-06 19:04:00 浏览: 62
好的,以下是Java代码实现:
首先,先创建一个教师信息的实体类 TeachInfo,包含教师编号、姓名、办公室三个属性:
```java
public class TeachInfo {
private String teachID;
private String tName;
private String toffice;
// 构造函数
public TeachInfo(String teachID, String tName, String toffice) {
this.teachID = teachID;
this.tName = tName;
this.toffice = toffice;
}
// getter和setter方法
public String getTeachID() {
return teachID;
}
public void setTeachID(String teachID) {
this.teachID = teachID;
}
public String gettName() {
return tName;
}
public void settName(String tName) {
this.tName = tName;
}
public String getToffice() {
return toffice;
}
public void setToffice(String toffice) {
this.toffice = toffice;
}
}
```
然后,在数据库中新建一张教师信息表,包含三个字段:教师编号、姓名、办公室。
```sql
CREATE TABLE TeachInfo (
TeachID VARCHAR(20) PRIMARY KEY,
TName VARCHAR(20) NOT NULL,
Toffice VARCHAR(20) NOT NULL
);
```
接下来,实现教师信息的增、改、查功能:
```java
import java.sql.*;
public class TeachInfoDao {
// 数据库连接信息
private final String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
private final String username = "root";
private final String password = "123456";
// 添加教师信息
public void addTeachInfo(TeachInfo teachInfo) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 加载数据库驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 获取数据库连接
conn = DriverManager.getConnection(url, username, password);
// SQL语句
String sql = "INSERT INTO TeachInfo(TeachID, TName, Toffice) VALUES(?, ?, ?)";
// 创建PreparedStatement对象
pstmt = conn.prepareStatement(sql);
// 给占位符赋值
pstmt.setString(1, teachInfo.getTeachID());
pstmt.setString(2, teachInfo.gettName());
pstmt.setString(3, teachInfo.getToffice());
// 执行插入操作
pstmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 修改教师信息
public void updateTeachInfo(TeachInfo teachInfo) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 加载数据库驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 获取数据库连接
conn = DriverManager.getConnection(url, username, password);
// SQL语句
String sql = "UPDATE TeachInfo SET TName = ?, Toffice = ? WHERE TeachID = ?";
// 创建PreparedStatement对象
pstmt = conn.prepareStatement(sql);
// 给占位符赋值
pstmt.setString(1, teachInfo.gettName());
pstmt.setString(2, teachInfo.getToffice());
pstmt.setString(3, teachInfo.getTeachID());
// 执行更新操作
pstmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 查询教师信息
public TeachInfo queryTeachInfo(String teachID) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
TeachInfo teachInfo = null;
try {
// 加载数据库驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 获取数据库连接
conn = DriverManager.getConnection(url, username, password);
// SQL语句
String sql = "SELECT * FROM TeachInfo WHERE TeachID = ?";
// 创建PreparedStatement对象
pstmt = conn.prepareStatement(sql);
// 给占位符赋值
pstmt.setString(1, teachID);
// 执行查询操作
rs = pstmt.executeQuery();
// 处理结果集
if (rs.next()) {
teachInfo = new TeachInfo(rs.getString("TeachID"), rs.getString("TName"), rs.getString("Toffice"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return teachInfo;
}
}
```
最后,在导航页面中设置相关的导航项,如下所示:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>教师信息管理系统</title>
</head>
<body>
<h3>导航菜单</h3>
<ul>
<li><a href="addTeachInfo.jsp">添加教师信息</a></li>
<li><a href="updateTeachInfo.jsp">修改教师信息</a></li>
<li><a href="queryTeachInfo.jsp">查询教师信息</a></li>
</ul>
</body>
</html>
```
其中,addTeachInfo.jsp、updateTeachInfo.jsp、queryTeachInfo.jsp分别对应添加、修改、查询教师信息的页面。
阅读全文