import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class ConnectDatabase { private Connection conn; public ConnectDatabase(){ conn = null; connectToDatabase(); } private void connectToDatabase(){ String url = "jdbc:mariadb://localhost:3306/lotto"; try{ conn = DriverManager.getConnection(url,"root",""); }catch(SQLException ex){ ex.printStackTrace(); } } public void closeConnect(){ try{ if(conn != null){ conn.close(); } }catch(SQLException ex){ ex.printStackTrace(); } } public Connection getConnection(){ return conn; } } 1.Reader import java.util.Scanner; import java.io.FileReader; import java.io.FileNotFoundException; import java.util.Vector; public class Reader { public Reader(){ } public Vector<String>readFile(){ Vector<String> numbers = new Vector<>(); try { FileReader fRead = new FileReader("szamok.txt"); Scanner scan = new Scanner(fRead); while(scan.hasNext()){ String row = scan.nextLine(); if(row.matches("[0-9:]+")){ numbers.add(row); } } scan.close(); } catch (FileNotFoundException ex) { ex.printStackTrace(); } return numbers; } } 1. Main import java.util.Vector; import java.sql.Connection; import java.sql.Statement; import java.sql.SQLException; public class Lotto { private static Vector<String> numbers; public static void main(String[] args) throws Exception { numbers = new Vector<>(); Reader reader = new Reader(); numbers = reader.readFile(); ConnectDatabase connDb = new ConnectDatabase(); Connection conn = connDb.getConnection(); numbers.forEach(rows -> rowsToDatabase(conn,rows)); } private static void rowsToDatabase(Connection conn, String rows){ String sql = "INSERT INTO drawed ( draw ) VALUES ('" + rows + "');"; Statement stmt = null; try { stmt = conn.createStatement(); stmt.executeQuery(sql); } catch (SQLException ex) { ex.printStackTrace(); } } }
2.main import java.sql.Connection; public class Lotto { public static void main(String[] args) throws Exception { ConnectDatabase connDb = new ConnectDatabase(); LottoController lottoCtr = new LottoController(connDb); } } 2. controller import java.util.Vector; import java.util.Random; import javax.swing.JCheckBox; import javax.swing.JCheckBoxMenuItem; import java.sql.Connection; import java.sql.SQLDataException; import java.sql.SQLException; import java.sql.Statement; public class LottoController { private ConnectDatabase connDb; private Vector<Integer> numberList; private Vector<Integer> drawedList; private Vector<Integer> choosenList; private Lottoform lottoFrm; private int counter = 0; public LottoController(ConnectDatabase connDb){ choosenList = new Vector<>(); drawedList = new Vector<>(); numberList = new Vector<>(); this.connDb = connDb; lottoFrm = new Lottoform(); lottoFrm.exitBtn.addActionListener(event -> exit()); lottoFrm.drwaBtn.addActionListener(event -> drawing()); fillNumberList(); numbercheckBoxes(); lottoFrm.setVisible(true); } private void fillNumberList(){ for(int i = 1; i < 91; i++ ){ numberList.add(i); } } private void numbercheckBoxes(){ for(Integer i = 1; i < 91; i++ ){ JCheckBox box = new JCheckBox(); box.setText(i.toString()); lottoFrm.centerPnl.add(box); box.addItemListener(event -> { JCheckBox check = (JCheckBox) event.getSource(); choosenList.add(Integer.parseInt(check.getText())); counter ++; if(counter == 5){ lottoFrm.drwaBtn.setEnabled(true); }else{ lottoFrm.drwaBtn.setEnabled(false); } }); } } private void drawing(){ int numbers = 90; Random rand = new Random(); for(int i = 0; i < 5; i++){ int number = rand.nextInt(numbers) + 1; numberList.remove(number - 1); numbers --; drawedList.add(number); } showResult(); numberstoDatabase(); } private void showResult(){ Integer result = 0; for(int i = 0;i < choosenList.size(); i++){ for(int j = 0; j < drawedList.size(); j++){ if(choosenList.get(i) == drawedList.get(j)){ result ++; } } } String resultValue = lottoFrm.resultLbl.getText(); lottoFrm.resultLbl.setText(resultValue + result.toString()); for(int i = 0;i < drawedList.size(); i++){ String drawValue = lottoFrm.drawLbl.getText(); String number = String.valueOf(drawedList.get(i)); lottoFrm.drawLbl.setText(drawValue + number + " "); } } private void numberstoDatabase(){ Connection conn = connDb.getConnection(); Statement stmt = null; String sqlData = ""; for(int i = 0; i < drawedList.size(); i++){ if(i < (drawedList.size() - 1)){ sqlData += String.valueOf(drawedList.get(i)) + ":"; }else{ sqlData += String.valueOf(drawedList.get(i)); } } System.out.println(sqlData); String sql = "INSERT INTO drawed(draw) VALUES ('"+ sqlData +"');"; try { stmt = conn.createStatement(); stmt.execute(sql); } catch (SQLException ex) { ex.printStackTrace(); } } private void exit(){ System.exit(0); } } 2. Form import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JLabel; import java.awt.BorderLayout; import java.awt.GridLayout; import java.util.concurrent.Flow; import java.awt.FlowLayout; public class Lottoform extends JFrame{ public JPanel mainPnl = new JPanel(); public JPanel northPnl = new JPanel(); public JPanel eastPnl = new JPanel(); public JPanel westPnl = new JPanel(); public JPanel southPnl = new JPanel(); public JPanel centerPnl = new JPanel(); public JPanel drawPnl = new JPanel(); public JPanel buttonPnl = new JPanel(); public JLabel drawLbl = new JLabel("Számok: "); public JLabel resultLbl = new JLabel("Találatok: "); public JButton exitBtn = new JButton("Kilépés"); public JButton drwaBtn = new JButton("Húzás"); public Lottoform(){ initComponents(); this.setVisible(true); } private void initComponents(){ this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(450,400); this.setLayout(new GridLayout(1,1)); mainPnl.setLayout(new BorderLayout()); this.add(mainPnl); northPnl.setLayout(new FlowLayout(FlowLayout.CENTER)); northPnl.add(resultLbl); mainPnl.add(northPnl,BorderLayout.NORTH); eastPnl.setSize(10,400); mainPnl.add(eastPnl,BorderLayout.EAST); southPnl.setLayout(new GridLayout(1,2)); drawPnl.setLayout(new FlowLayout(FlowLayout.CENTER)); drawPnl.add(drawLbl); buttonPnl.setLayout(new FlowLayout(FlowLayout.RIGHT)); drwaBtn.setEnabled(false); buttonPnl.add(drwaBtn); buttonPnl.add(exitBtn); southPnl.add(drawPnl); southPnl.add(buttonPnl); mainPnl.add(southPnl,BorderLayout.SOUTH); westPnl.setSize(10,400); mainPnl.add(westPnl); centerPnl.setLayout(new GridLayout(10,9)); mainPnl.add(centerPnl,BorderLayout.CENTER); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)