본문 바로가기

개발하자/JAVA문제풀이

15.01.14 Lotto 프로그램(DB연동)

랜덤함수를 이용한 로또번호 생성&DB저장 프로그램입니다.

최초실행화면

로또번호생성을 클릭하면 숫자가 보이고 DB에 그 숫자들이 입력된다

반복적으로 생성을 클릭하면 계속 화면숫자가 바뀌고 DB에는 번호가 누적되어 저장된다.

초기화를 누르면 디비에 누적된 숫자가 초기화된다.

 빈도수 Max7순위를 누르면 DB에 저장된 로또생성 번호중 빈도수가 높은 순서대로 7위까지 보여주게 된다.

[소스보기]

package Lotto;

import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

class DB_Conn {
 static MyFrame frame;
 DB_Conn(MyFrame f) {
  this.frame=f;
 }
 int[] arr = new int[7];
 String[] random = new String[7];

 void Conn(int a, int b, int c, int d, int e, int f, int g, String s) {
  try {
   arr[0] = a;
   arr[1] = b;
   arr[2] = c;
   arr[3] = d;
   arr[4] = e;
   arr[5] = f;
   arr[6] = g;

   Class.forName("oracle.jdbc.OracleDriver");
   String url = "jdbc:oracle:thin:@//localhost:1521/xe";
   String id = "scott";
   String pw = "tiger";
   Connection conn = DriverManager.getConnection(url, id, pw);
   Statement stmt = conn.createStatement();
   if (s == "로또번호생성(DB저장)") {
    for (int array : arr) {
     int rs = stmt
       .executeUpdate("insert into lotto values(" + array
         + ")");
    }
   } else if (s == "초기화") {
    ResultSet rs = stmt.executeQuery("delete lotto");
   }else { 
    //System.out.println("빈도수쿼리문");
    ResultSet rs = stmt.executeQuery("select num from lotto group by num order by count(num) desc");
    rs.next();
    for(int w=0;w<7;w++){
    random[w]=rs.getString(1);
    rs.next();
    }
    frame.bindoNum(random[0],random[1],random[2],random[3],random[4],random[5],random[6]);
    }
    
  } catch (Exception ex) {
   System.out.println("DB연결 실패!!");
  }
 }
}
class Random {
 
 int a,b,c,d,e,f,g;
 
 Random(){
  
  a = (int)(Math.random()*45)+1;
  do{
  b = (int)(Math.random()*45)+1;
  }while(b==a);
  do{
  c = (int)(Math.random()*45)+1;
  }while(c==b||c==a);
  do{
  d = (int)(Math.random()*45)+1;
  }while(d==c||d==b||d==a);
  do{
  e = (int)(Math.random()*45)+1;
  }while(e==d||e==c||e==b||e==a);
  do{
  f = (int)(Math.random()*45)+1;
  }while(f==e||f==d||f==c||f==b||f==a);
  do{
  g = (int)(Math.random()*45)+1;
  }while(g==f||g==e||g==d||g==c||g==b||g==a);
  
 }
 
 
}

class Action implements ActionListener {
 Random ran;
 MyFrame f;
 DB_Conn db;

 Action(MyFrame f) {
  this.f = f; 
 }

 public void actionPerformed(ActionEvent e) {
  db = new DB_Conn(f);
  if(e.getActionCommand()=="로또번호생성(DB저장)"){
  ran = new Random();
  db.Conn(ran.a, ran.b, ran.c, ran.d, ran.e, ran.f, ran.g,e.getActionCommand());
  f.setNum(ran.a, ran.b, ran.c, ran.d, ran.e, ran.f, ran.g);
  }else if(e.getActionCommand()=="빈도수 Max7순위"){
  db.Conn(ran.a, ran.b, ran.c, ran.d, ran.e, ran.f, ran.g,e.getActionCommand());
  }else if(e.getActionCommand()=="초기화"){
   db.Conn(ran.a, ran.b, ran.c, ran.d, ran.e, ran.f, ran.g,e.getActionCommand());
   }
 }
}

class MyFrame extends Frame {
 Button b1;//로또번호생성
 Button b2;//Max빈도수 조회
 Button b3;//DB초기화
 Label la1, la2, la3, la4, la5, la6,la7,la8;
 Label la11, la22, la33, la44, la55, la66,la77,la88;
 Panel p,p1,p2;
 Font f1;
 Action act;
 MyFrame(){}
 MyFrame(String t) {
  setTitle(t);
  b1 = new Button();
  b2 = new Button();
  b3 = new Button();
  p = new Panel();
  p1 = new Panel();
  p2 = new Panel();
  act = new Action(this);
  

  f1 = new Font("굴림체", Font.BOLD, 17);
  b1.setLabel("로또번호생성(DB저장)");
  b2.setLabel("빈도수 Max7순위");
  b3.setLabel("초기화");
  b1.setFont(f1);
  b2.setFont(f1);
  b3.setFont(f1);

  la1 = new Label("?", Label.CENTER);
  la2 = new Label("?", Label.CENTER);
  la3 = new Label("?", Label.CENTER);
  la4 = new Label("?", Label.CENTER);
  la5 = new Label("?", Label.CENTER);
  la6 = new Label("?", Label.CENTER);
  la7 = new Label("+ 보너스번호", Label.CENTER);
  la8 = new Label("?", Label.CENTER);
  
  la11 = new Label("?", Label.CENTER);
  la22 = new Label("?", Label.CENTER);
  la33 = new Label("?", Label.CENTER);
  la44 = new Label("?", Label.CENTER);
  la55 = new Label("?", Label.CENTER);
  la66 = new Label("?", Label.CENTER);
  la77 = new Label("?", Label.CENTER);

  la1.setFont(f1);
  la2.setFont(f1);
  la3.setFont(f1);
  la4.setFont(f1);
  la5.setFont(f1);
  la6.setFont(f1);
  la7.setFont(f1);
  la8.setFont(f1);

  la1.setBackground(Color.red);
  la2.setBackground(Color.orange);
  la3.setBackground(Color.yellow);
  la4.setBackground(Color.green);
  la5.setBackground(Color.blue);
  la6.setBackground(Color.magenta);
  la8.setBackground(Color.CYAN);
  
  la11.setBackground(Color.lightGray);
  la22.setBackground(Color.lightGray);
  la33.setBackground(Color.lightGray);
  la44.setBackground(Color.lightGray);
  la55.setBackground(Color.lightGray);
  la66.setBackground(Color.lightGray);
  la77.setBackground(Color.lightGray);
  
 }
 void setNum(int num1,int num2,int num3,int num4,int num5,int num6,int num8){
  la1.setText(""+num1);
  la2.setText(""+num2);
  la3.setText(""+num3);
  la4.setText(""+num4);
  la5.setText(""+num5);
  la6.setText(""+num6);
  la8.setText(""+num8);
 }
 void bindoNum(String num11,String num22,String num33,String num44,String num55,String num66,String num77){
  la11.setText(num11);
  la22.setText(num22);
  la33.setText(num33);
  la44.setText(num44);
  la55.setText(num55);
  la66.setText(num66);
  la77.setText(num77);
 }
 void addComponent() {
  p2.add(b1);
  p2.add(b3);
  p.add(la1);
  p.add(la2);
  p.add(la3);
  p.add(la4);
  p.add(la5);
  p.add(la6);
  p.add(la7);
  p.add(la8);
  p1.add(b2);
  p1.add(la11);
  p1.add(la22);
  p1.add(la33);
  p1.add(la44);
  p1.add(la55);
  p1.add(la66);
  p1.add(la77);
  add(p2,"North");
  add(p,"Center");
  add(p1,"South");
 }

 void registerEvent() {
  b1.addActionListener(act);
  b2.addActionListener(act);
  b3.addActionListener(act);
  this.addWindowListener(new WindowClose());
 }
}
class WindowClose extends WindowAdapter {
 public void windowClosing(WindowEvent we) {
  System.exit(0);
 }
}
public class Main {
 public static void main(String areg[]) {
  MyFrame f = new MyFrame("Lotto");
  f.setVisible(true);
  f.setBounds(700, 300, 370, 150);
  f.addComponent();
  f.registerEvent();
 }
}