使用java+mysql写一个简单的在线投票系统
时间: 2024-01-14 07:05:07 浏览: 116
在线投票系统JavaWeb源代码+sql
好的,下面是一个简单的在线投票系统的Java代码,使用MySQL作为数据库:
首先,需要创建一个投票项目的实体类(VoteItem)和投票记录的实体类(VoteRecord):
```
public class VoteItem {
private int id;
private String name;
private int count;
// getter and setter methods
}
public class VoteRecord {
private int id;
private int itemId;
private String voterIp;
private Date voteTime;
// getter and setter methods
}
```
接下来,需要创建一个MySQL数据库,并创建两张表:vote_item用于存储投票项目,vote_record用于存储投票记录。下面是两张表的建表语句:
```
CREATE TABLE vote_item (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
count INT DEFAULT 0
);
CREATE TABLE vote_record (
id INT PRIMARY KEY AUTO_INCREMENT,
item_id INT NOT NULL,
voter_ip VARCHAR(50) NOT NULL,
vote_time DATETIME NOT NULL
);
```
然后,创建一个投票服务类(VoteService),实现投票功能:
```
public class VoteService {
private static final String DB_URL = "jdbc:mysql://localhost:3306/vote";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "root";
private static final String INSERT_VOTE_RECORD_SQL = "INSERT INTO vote_record (item_id, voter_ip, vote_time) VALUES (?, ?, ?)";
private static final String UPDATE_VOTE_ITEM_SQL = "UPDATE vote_item SET count = count + 1 WHERE id = ?";
private static final String GET_ALL_VOTE_ITEMS_SQL = "SELECT * FROM vote_item";
private static final String GET_VOTE_ITEM_SQL = "SELECT * FROM vote_item WHERE id = ?";
private Connection conn;
public VoteService() throws SQLException {
conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
}
public List<VoteItem> getAllVoteItems() throws SQLException {
List<VoteItem> items = new ArrayList<>();
try (PreparedStatement ps = conn.prepareStatement(GET_ALL_VOTE_ITEMS_SQL)) {
ResultSet rs = ps.executeQuery();
while (rs.next()) {
VoteItem item = new VoteItem();
item.setId(rs.getInt("id"));
item.setName(rs.getString("name"));
item.setCount(rs.getInt("count"));
items.add(item);
}
}
return items;
}
public VoteItem getVoteItem(int id) throws SQLException {
try (PreparedStatement ps = conn.prepareStatement(GET_VOTE_ITEM_SQL)) {
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
VoteItem item = new VoteItem();
item.setId(rs.getInt("id"));
item.setName(rs.getString("name"));
item.setCount(rs.getInt("count"));
return item;
} else {
return null;
}
}
}
public void vote(int itemId, String voterIp) throws SQLException {
Date voteTime = new Date();
try (PreparedStatement ps = conn.prepareStatement(INSERT_VOTE_RECORD_SQL)) {
ps.setInt(1, itemId);
ps.setString(2, voterIp);
ps.setTimestamp(3, new Timestamp(voteTime.getTime()));
ps.executeUpdate();
}
try (PreparedStatement ps = conn.prepareStatement(UPDATE_VOTE_ITEM_SQL)) {
ps.setInt(1, itemId);
ps.executeUpdate();
}
}
}
```
最后,创建一个投票应用程序(VoteApp),使用Spring Boot框架实现Web界面:
```
@RestController
public class VoteApp {
private VoteService voteService;
@Autowired
public VoteApp(VoteService voteService) {
this.voteService = voteService;
}
@GetMapping("/vote")
public ModelAndView vote() throws SQLException {
List<VoteItem> items = voteService.getAllVoteItems();
ModelAndView mv = new ModelAndView("vote");
mv.addObject("items", items);
return mv;
}
@PostMapping("/vote")
public String doVote(@RequestParam int itemId, HttpServletRequest request) throws SQLException {
String voterIp = request.getRemoteAddr();
voteService.vote(itemId, voterIp);
return "success";
}
@GetMapping("/vote/{id}")
public ModelAndView voteItem(@PathVariable int id) throws SQLException {
VoteItem item = voteService.getVoteItem(id);
if (item == null) {
return new ModelAndView("error");
} else {
ModelAndView mv = new ModelAndView("vote_item");
mv.addObject("item", item);
return mv;
}
}
public static void main(String[] args) {
SpringApplication.run(VoteApp.class, args);
}
}
```
在resources/templates目录下创建vote.html和vote_item.html两个模板文件,分别用于显示所有投票项目和单个投票项目的投票结果。
最后,启动投票应用程序,访问http://localhost:8080/vote即可进行投票。
阅读全文