生成一个javaweb的CoolMetting会议管理系统并连接数据库的代码
时间: 2024-03-01 17:55:14 浏览: 126
基于javaweb的毕业设计会议管理系统源码+数据库.zip
5星 · 资源好评率100%
以下是一个简单的JavaWeb的CoolMeeting会议管理系统的代码,包括了连接MySQL数据库的代码:
Meeting.java
```
package CoolMeeting;
import java.util.Date;
public class Meeting {
private int id;
private String name;
private Date startTime;
private Date endTime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
}
```
MeetingDAO.java
```
package CoolMeeting;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class MeetingDAO {
private String driver = "com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://localhost:3306/CoolMeeting";
private String username = "root";
private String password = "123456";
public Connection getConnection() {
Connection connection = null;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return connection;
}
public List<Meeting> getAllMeetings() {
List<Meeting> meetings = new ArrayList<>();
String sql = "SELECT * FROM meetings";
try (Connection connection = getConnection();
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
Meeting meeting = new Meeting();
meeting.setId(resultSet.getInt("id"));
meeting.setName(resultSet.getString("name"));
meeting.setStartTime(resultSet.getTimestamp("start_time"));
meeting.setEndTime(resultSet.getTimestamp("end_time"));
meetings.add(meeting);
}
} catch (SQLException e) {
e.printStackTrace();
}
return meetings;
}
public void addMeeting(Meeting meeting) {
String sql = "INSERT INTO meetings (name, start_time, end_time) VALUES (?, ?, ?)";
try (Connection connection = getConnection();
PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setString(1, meeting.getName());
statement.setTimestamp(2, new Timestamp(meeting.getStartTime().getTime()));
statement.setTimestamp(3, new Timestamp(meeting.getEndTime().getTime()));
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteMeeting(int id) {
String sql = "DELETE FROM meetings WHERE id = ?";
try (Connection connection = getConnection();
PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setInt(1, id);
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateMeeting(Meeting meeting) {
String sql = "UPDATE meetings SET name = ?, start_time = ?, end_time = ? WHERE id = ?";
try (Connection connection = getConnection();
PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setString(1, meeting.getName());
statement.setTimestamp(2, new Timestamp(meeting.getStartTime().getTime()));
statement.setTimestamp(3, new Timestamp(meeting.getEndTime().getTime()));
statement.setInt(4, meeting.getId());
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
index.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CoolMeeting</title>
</head>
<body>
<h1>CoolMeeting</h1>
<table>
<tr>
<td>ID</td>
<td>Name</td>
<td>Start Time</td>
<td>End Time</td>
<td></td>
<td></td>
</tr>
<c:forEach var="meeting" items="${meetings}">
<tr>
<td>${meeting.id}</td>
<td>${meeting.name}</td>
<td>${meeting.startTime}</td>
<td>${meeting.endTime}</td>
<td><a href="edit.jsp?id=${meeting.id}">Edit</a></td>
<td><a href="delete.jsp?id=${meeting.id}">Delete</a></td>
</tr>
</c:forEach>
</table>
<h2>Add Meeting</h2>
<form action="add.jsp" method="post">
<label>Name:</label>
<input type="text" name="name"><br>
<label>Start Time:</label>
<input type="datetime-local" name="startTime"><br>
<label>End Time:</label>
<input type="datetime-local" name="endTime"><br>
<button type="submit">Add</button>
</form>
</body>
</html>
```
在这个简单的示例中,我们使用MySQL数据库来存储会议数据。MeetingDAO类负责连接数据库并提供基本的数据访问功能,而index.jsp页面则显示会议列表和提供添加、删除和更新会议的功能。
阅读全文