首页>代码>Java+Servlet投票系统>/vote/src/com/vote/service/VoteService.java
package com.vote.service;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.ResultSet;
import java.sql.SQLException;

public class VoteService {

	public String showUser() throws SQLException {
		String sql = "select * from vote_user";
		StringBuffer result = new StringBuffer();
		ResultSet rs = DBService.getConnection().createStatement().executeQuery(sql);
		while(rs.next()) {
			result.append("{\"uid\":\"").append(rs.getString("uid")).append("\",");
			result.append("\"username\":\"").append(rs.getString("username")).append("\"},");
		}
		return result.deleteCharAt(result.length() - 1).toString();
	}
	
	public boolean vote(String uid) throws UnknownHostException, SQLException {
		
		InetAddress addr = InetAddress.getLocalHost();
		String ip=addr.getHostAddress().toString();	//获得本机IP
		
		String findSql = "select ip from vote_vote where ip = '" + ip + "'";
		ResultSet rs = DBService.getConnection().createStatement().executeQuery(findSql);
		if(rs.next()) {
			return false;
		} else {
			String insertSql = "insert into vote_vote(uid, ip, votedate) values ('" + uid + "','" + ip + "','" + new java.sql.Date(System.currentTimeMillis()) + "')";
			int res = DBService.getConnection().createStatement().executeUpdate(insertSql);
			if(res > 0) 
				return true;
		}
		return false;
	}
}
最近下载更多
masterpieceaaa  LV1 2024年11月5日
AIzhai  LV1 2024年6月4日
梦幻瞳  LV1 2022年11月24日
hhhccc  LV1 2022年11月6日
wubinbin  LV11 2022年10月24日
lhlh123456  LV1 2022年6月17日
744083587  LV1 2022年6月13日
我是阿拉伯王子  LV1 2022年4月20日
起个名字好难起  LV1 2021年12月20日
3089559272  LV11 2021年12月17日
最近浏览更多
xiquyiyuan  LV10 4月12日
zolscy  LV24 2024年12月9日
masterpieceaaa  LV1 2024年11月5日
张泽帅  LV6 2024年10月21日
暂无贡献等级
taoshen95  LV16 2024年7月12日
泓鼎168  LV20 2024年6月5日
AIzhai  LV1 2024年6月4日
2411307984  LV1 2024年5月12日
wananall  LV13 2024年3月13日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友