Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
 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();
            
        }
    }
}


What I have tried:

 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);
    }
}
Posted
Updated 8-May-22 21:38pm
v2
Comments
CPallini 9-May-22 3:43am    
Just code dump?
Could you please provide also a description of your problem?
OriginalGriff 9-May-22 3:43am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are?

That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!

Use the "Improve question" widget to edit your question and provide better information.
Patrice T 9-May-22 4:25am    
And you have a question ?
Rajeev Jayaram 9-May-22 12:32pm    
What is your question?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900