votebean.java

来自「一个用JAVA实现的彩票20选5的小程序,大家可以看看买彩票中的机率哦,仅供参考」· Java 代码 · 共 155 行

JAVA
155
字号
package cn.vote.lucky;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class VoteBean {
	int one, two, three, four, five;

	// 生成随机号,并存入数据库
	public void generateVote() {
		System.out.println("生成备选号码.....");
		Random r = new Random();
		try {
			for (int i = 0; i < 5; i++) {
				StringBuffer sb = new StringBuffer();
				one = r.nextInt(20) + 1;
				two = r.nextInt(20) + 1;
				three = r.nextInt(20) + 1;
				four = r.nextInt(20) + 1;
				five = r.nextInt(20) + 1;
				if (one != two && one != three && one != four && one != five
						&& two != three && two != four && two != five
						&& three != four && three != five && four != five) {
					sb.append(one + " " + two + " " + three + " " + four + " "
							+ five);
					PreparedStatement pstmt = ConnDb
							.pstmt("insert into vote(one,two,three,four,five) values(?,?,?,?,?)");
					pstmt.setInt(1, one);
					pstmt.setInt(2, two);
					pstmt.setInt(3, three);
					pstmt.setInt(4, four);
					pstmt.setInt(5, five);
					pstmt.execute();
				}
				System.out.println("生成备选号码成功.....");
			}
		} catch (SQLException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (Exception e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
	}

	// 显示列表
	public List getVoteList() {
		List list = new ArrayList();
		try {
			ResultSet rs = ConnDb.executeSelect("select * from vote");
			while (rs.next()) {
				Vote v = new Vote();
				v.setOne(rs.getInt("one"));
				// System.out.println(v.getOne());
				v.setTwo(rs.getInt("two"));
				v.setThree(rs.getInt("three"));
				v.setFour(rs.getInt("four"));
				v.setFive(rs.getInt("five"));
				list.add(v);
			}
		} catch (SQLException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (Exception e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return list;
	}
	//获取推荐号码
	public static List getVote(){
		List list = new ArrayList();
		try {
			PreparedStatement pstmt = ConnDb.pstmt("select top 7 * from voteresult where result>0 order by result desc ");
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				list.add(new Integer(rs.getInt("vid")));
			}
		} catch (Exception e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return list;
	}
	// 删除所有备选号码
	public void delAllVote() {
		try {
			System.out.println("开始删除数据...");
			PreparedStatement pstmt = ConnDb.pstmt("delete from vote");
			pstmt.execute();
			PreparedStatement stmt = ConnDb.pstmt("update voteresult set result = 0");
			stmt.execute();
			System.out.println("删除所有数据成功....");
		} catch (SQLException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (Exception e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
	}

	// 统计号码产生的次数
	public List count() {
		List list = new ArrayList();
		try {
			for (int num = 1; num < 21; num++) {
				ResultSet rs = ConnDb
						.executeSelect("select count(sid) from vote where one = "
								+ num
								+ " or two = "
								+ num
								+ " or three = "
								+ num
								+ " or four = "
								+ num
								+ " or five ="
								+ num);
				if (rs.next()) {
					list.add(new Integer(rs.getInt(1)));
					PreparedStatement pstmt = ConnDb
							.pstmt("update voteresult set result = ? where vid = ?");
					pstmt.setInt(1, rs.getInt(1));
					pstmt.setInt(2,num);
					if (pstmt.execute()) {
						System.out.println("插入出现次数总数成功");
					}
					// System.out.println(rs.getInt(1));
				}
			}
		} catch (Exception e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}

		return list;
	}

	public static void main(String[] args) {
		List list = new VoteBean().count();
		System.out.println(list.get(0));
		// for(int i=0;i<4;i++){
		// Vote v = (Vote)list.get(i);
		//			
		// System.out.println(v.getOne()+" "+v.getTwo());
		// }

	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?