机选福利彩票程序V2.0-批量插入,比V1.0效率高

今天上班没什么事情干,很无聊,于是拿出中午吃饭时买的彩票,开始研究,然后打开双色球走势图网页,感觉双色球的号码,就是一组随机数,无规律可循,靠的都是运气,于是想通过自己写的程序给自己选四组号码,看看能不能给自己带来好运!本程序按照双色球的出球顺序依次产生号码。并对产生的号码进行了排序展示
V1.0:

package com.jincm.wflt.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class LuckyNumber {

public static void main(String[] args) {
LuckyNumber luckyNum=new LuckyNumber();
/**
* 机选4注
*/
for(int i=0;i<1;i++){
System.out.println("-----------第"+(i+1)+"注----------");
luckyNum.getLuckyNumber();
}
}
public void getLuckyNumber(){
Map luckyMap=new HashMap();
StringBuffer luckySb=new StringBuffer();
/**
* 避免重复的号码产生
*/
for(int i=0;i<6;i++){
int luckyRed=(int)(Math.random()*33+1);
if(luckyMap.containsKey(luckyRed)){
i--;
}else{
luckyMap.put(luckyRed, luckyRed);
luckySb.append(luckyRed+" ");
}
}
int luckyBlue=(int)(Math.random()*16+1);
luckySb.append(luckyBlue);
System.out.println(luckySb.toString());//按彩票产生的顺序输出
String[] luckyStr=luckySb.toString().split(" ");
List luckyList=new ArrayList();
for(int i=0;i if(i!=6){
luckyList.add(Integer.parseInt(luckyStr[i]));
}
}
Collections.sort(luckyList);//对产生的彩票号码进行排序
StringBuffer luckyBuff=new StringBuffer();
for(int i=0;i luckyBuff.append(luckyList.get(i)+" ");
}
luckyBuff.append(luckyStr[6]);
System.out.println(luckyBuff.toString());//将排序后的彩票输出
insert(luckyBuff.toString(),luckyBlue);
}
/**
*
* @return
*/
public static Connection getConn(){
Connection conn=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORACL", "wflt", "wflt");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}

public void insert(String luckyNum,Integer blue){
String[] red=luckyNum.split(" ");
Connection conn;
PreparedStatement ps;
PreparedStatement ps2;
ResultSet rs;
try {
conn = getConn();
ps2=conn.prepareStatement("select jincm_wflt_seq.nextval from dual");
rs=ps2.executeQuery();
Long id=0L;
while(rs.next()){
id=rs.getLong(1);
}
ps=conn.prepareStatement("insert into jincm_wflt(id_,red1_,red2_,red3_,red4_,red5_,red6_,blue_,createtime_,createby_) values(?,?,?,?,?,?,?,?,sysdate,'金聪敏')");
ps.setLong(1, id);
ps.setInt(2, Integer.parseInt(red[0]));
ps.setInt(3, Integer.parseInt(red[1]));
ps.setInt(4, Integer.parseInt(red[2]));
ps.setInt(5, Integer.parseInt(red[3]));
ps.setInt(6, Integer.parseInt(red[4]));
ps.setInt(7, Integer.parseInt(red[5]));
ps.setInt(8, blue);
ps.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
}

V2.0:

package com.jincm.wflt.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class LuckyNumber {

public static void main(String[] args) {
LuckyNumber luckyNum=new LuckyNumber();
/**
* 机选4注
*/
List luckyList=new ArrayList();
for(int i=0;i<1000;i++){
System.out.println("-----------第"+(i+1)+"注----------");
String luckyStr=luckyNum.getLuckyNumber();
String[] lucky=luckyStr.split(" ");
luckyList.add(lucky);
}
luckyNum.insertBatch(luckyList);

}
public String getLuckyNumber(){
Map luckyMap=new HashMap();
StringBuffer luckySb=new StringBuffer();
/**
* 避免重复的号码产生
*/
for(int i=0;i<6;i++){
int luckyRed=(int)(Math.random()*33+1);
if(luckyMap.containsKey(luckyRed)){
i--;
}else{
luckyMap.put(luckyRed, luckyRed);
luckySb.append(luckyRed+" ");
}
}
int luckyBlue=(int)(Math.random()*16+1);
luckySb.append(luckyBlue);
System.out.println(luckySb.toString());//按彩票产生的顺序输出
String[] luckyStr=luckySb.toString().split(" ");
List luckyList=new ArrayList();
for(int i=0;i if(i!=6){
luckyList.add(Integer.parseInt(luckyStr[i]));
}
}
Collections.sort(luckyList);//对产生的彩票号码进行排序
StringBuffer luckyBuff=new StringBuffer();
for(int i=0;i luckyBuff.append(luckyList.get(i)+" ");
}
luckyBuff.append(luckyStr[6]);
System.out.println(luckyBuff.toString());//将排序后的彩票输出
//insert(luckyBuff.toString(),luckyBlue);
String returnLucky=luckyBuff.toString()+" "+luckyBlue;
return returnLucky;
}
/**
*
* @return
*/
public static Connection getConn(){
Connection conn=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORACL", "wflt", "wflt");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}

public void insert(String luckyNum,Integer blue){
String[] red=luckyNum.split(" ");
Connection conn;
PreparedStatement ps;
PreparedStatement ps2;
ResultSet rs;
try {
conn = getConn();
ps2=conn.prepareStatement("select jincm_wflt_seq.nextval from dual");
rs=ps2.executeQuery();
Long id=0L;
while(rs.next()){
id=rs.getLong(1);
}
ps=conn.prepareStatement("insert into jincm_wflt(id_,red1_,red2_,red3_,red4_,red5_,red6_,blue_,createtime_,createby_) values(?,?,?,?,?,?,?,?,sysdate,'金聪敏')");
ps.setLong(1, id);
ps.setInt(2, Integer.parseInt(red[0]));
ps.setInt(3, Integer.parseInt(red[1]));
ps.setInt(4, Integer.parseInt(red[2]));
ps.setInt(5, Integer.parseInt(red[3]));
ps.setInt(6, Integer.parseInt(red[4]));
ps.setInt(7, Integer.parseInt(red[5]));
ps.setInt(8, blue);
ps.execute();
} catch (Exception e) {
e.printStackTrace();
}
}

public void insertBatch(List luckyList){
Connection conn;
PreparedStatement ps;
PreparedStatement ps2;
ResultSet rs;
try {
conn = getConn();
conn.setAutoCommit(false);
ps2=conn.prepareStatement("select id_ from (select id_,rownum rn from jincm_wflt where rownum<=1 order by id_ desc) where rn>0");
rs=ps2.executeQuery();
Long id=0L;
while(rs.next()){
id=rs.getLong(1);
}
ps=conn.prepareStatement("insert into jincm_wflt(id_,red1_,red2_,red3_,red4_,red5_,red6_,blue_,createtime_,createby_) values(?,?,?,?,?,?,?,?,sysdate,'金聪敏')");
for(int i=0;i String[] lucky=luckyList.get(i);
id++;
ps.setLong(1, id);
ps.setInt(2, Integer.parseInt(lucky[0]));
ps.setInt(3, Integer.parseInt(lucky[1]));
ps.setInt(4, Integer.parseInt(lucky[2]));
ps.setInt(5, Integer.parseInt(lucky[3]));
ps.setInt(6, Integer.parseInt(lucky[4]));
ps.setInt(7, Integer.parseInt(lucky[5]));
ps.setInt(8, Integer.parseInt(lucky[6]));
ps.addBatch();
}
ps.executeBatch();
conn.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
}



package com.jincm.wflt.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LuckyNumber implements Runnable {
private int count;

public LuckyNumber() {
}

public LuckyNumber(int count) {
this.count = count;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

public static void main(String[] args) {
for (int i = 0; i < 30; i++) {
Thread t = new Thread(new LuckyNumber(i + 1));
t.start();
}

}

public String getLuckyNumber() {
Map luckyMap = new HashMap();
StringBuffer luckySb = new StringBuffer();
/**
* 避免重复的号码产生
*/
for (int i = 0; i < 6; i++) {
int luckyRed = (int) (Math.random() * 33 + 1);
if (luckyMap.containsKey(luckyRed)) {
i--;
} else {
luckyMap.put(luckyRed, luckyRed);
luckySb.append(luckyRed + " ");
}
}
int luckyBlue = (int) (Math.random() * 16 + 1);
luckySb.append(luckyBlue);
System.out.println(luckySb.toString());// 按彩票产生的顺序输出
String[] luckyStr = luckySb.toString().split(" ");
List luckyList = new ArrayList();
for (int i = 0; i < luckyStr.length; i++) {
if (i != 6) {
luckyList.add(Integer.parseInt(luckyStr[i]));
}
}
Collections.sort(luckyList);// 对产生的彩票号码进行排序
StringBuffer luckyBuff = new StringBuffer();
for (int i = 0; i < luckyList.size(); i++) {
luckyBuff.append(luckyList.get(i) + " ");
}
luckyBuff.append(luckyStr[6]);
System.out.println(luckyBuff.toString());// 将排序后的彩票输出
// insert(luckyBuff.toString(),luckyBlue);
String returnLucky = luckyBuff.toString() + " " + luckyBlue;
return returnLucky;
}

/**
*
* @return
*/
public static Connection getConn() {
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:ORACL", "wflt", "wflt");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}

public void insert(String luckyNum, Integer blue) {
String[] red = luckyNum.split(" ");
Connection conn;
PreparedStatement ps;
PreparedStatement ps2;
ResultSet rs;
try {
conn = getConn();
ps2 = conn
.prepareStatement("select jincm_wflt_seq.nextval from dual");
rs = ps2.executeQuery();
Long id = 0L;
while (rs.next()) {
id = rs.getLong(1);
}
ps = conn
.prepareStatement("insert into jincm_wflt(id_,red1_,red2_,red3_,red4_,red5_,red6_,blue_,createtime_,createby_) values(?,?,?,?,?,?,?,?,sysdate,'金聪敏')");
ps.setLong(1, id);
ps.setInt(2, Integer.parseInt(red[0]));
ps.setInt(3, Integer.parseInt(red[1]));
ps.setInt(4, Integer.parseInt(red[2]));
ps.setInt(5, Integer.parseInt(red[3]));
ps.setInt(6, Integer.parseInt(red[4]));
ps.setInt(7, Integer.parseInt(red[5]));
ps.setInt(8, blue);
ps.execute();
} catch (Exception e) {
e.printStackTrace();
}
}

public void insertBatch(List luckyList) {
Connection conn;
PreparedStatement ps;
PreparedStatement ps2;
ResultSet rs;
try {
conn = getConn();
conn.setAutoCommit(false);
// ps2 = conn
// .prepareStatement("select id_ from (select id_,rownum rn from jincm_wflt where rownum<=1 order by id_ desc) where rn>0");
// rs = ps2.executeQuery();
// Long id = 0L;
// while (rs.next()) {
// id = rs.getLong(1);
// }
// id+=(getCount())*luckyList.size();
ps = conn
.prepareStatement("insert into jincm_wflt(id_,red1_,red2_,red3_,red4_,red5_,red6_,blue_,createtime_,createby_) values(jincm_wflt_seq.nextval,?,?,?,?,?,?,?,sysdate,'金聪敏')");
if (luckyList != null && luckyList.size() > 0) {
for (int i = 0; i < luckyList.size(); i++) {
String[] lucky = luckyList.get(i);
// id++;
// ps.setLong(1, id);
ps.setInt(1, Integer.parseInt(lucky[0]));
ps.setInt(2, Integer.parseInt(lucky[1]));
ps.setInt(3, Integer.parseInt(lucky[2]));
ps.setInt(4, Integer.parseInt(lucky[3]));
ps.setInt(5, Integer.parseInt(lucky[4]));
ps.setInt(6, Integer.parseInt(lucky[5]));
ps.setInt(7, Integer.parseInt(lucky[6]));
// ps.setLong(1, id);
// ps.setInt(2, Integer.parseInt(lucky[0]));
// ps.setInt(3, Integer.parseInt(lucky[1]));
// ps.setInt(4, Integer.parseInt(lucky[2]));
// ps.setInt(5, Integer.parseInt(lucky[3]));
// ps.setInt(6, Integer.parseInt(lucky[4]));
// ps.setInt(7, Integer.parseInt(lucky[5]));
// ps.setInt(8, Integer.parseInt(lucky[6]));
ps.addBatch();
}
}
ps.executeBatch();
conn.commit();
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void run() {
LuckyNumber luckyNum = new LuckyNumber();
/**
* 机选4注
*/
List luckyList = new ArrayList();
for (int i = 0; i < 5000; i++) {
System.out.println("线程" + count +"-----------第" + (i + 1) + "注----------");
String luckyStr = luckyNum.getLuckyNumber();
String[] lucky = luckyStr.split(" ");
luckyList.add(lucky);
}
luckyNum.insertBatch(luckyList);
System.out.println("线程" + count + "数据插入成功!");
}

}

你可能感兴趣的:(通用技术)