0

I'm trying to take in values for 6 variable from a user, and then store those inputs in a database. How do I code it to set the user input as the variable?

So I want to set the variables num1, etc as the users inputs.

My code is =>

public class LottoJDBC {

private Connection connection;

public LottoJDBC() throws Exception

//CONNECT TO DATABASE

{
    Class.forName("com.mysql.lottoPro.Driver").newInstance();
    connection = DriverManager.getConnection("lottoPro:mysql://localhost:3306/lottProDB"+"user=root&password=root");

    //SELECT DATA FROM DATABASE
    Statement select = connection.createStatement();
    ResultSet rs = select.executeQuery("SELECT * FROM userChoice");
    while (rs.next()) {
    System.out.println("These numbers are already taken by the previous user: "+rs.getNString(1) + " " +rs.getNString(2) + " " +rs.getNString(3) + " " +rs.getNString(4) + " " +rs.getNString(5) + " " +rs.getNString(6) + ".");

    }


    //INSERTTING DATA
    try (PreparedStatement createUser = connection.prepareStatement("INSERT into userChoice (num1, num2, num3, num4, num5, num6)" + " VALUES ( ?, ?, ?, ?, ?, ?)")) {
        int num1, num2, num3, num4, num5, num6;
        createUser.setInt(1, num1);
        createUser.setInt(2, num2);
        createUser.setInt(3, num3);
        createUser.setInt(4, num4);
        createUser.setInt(5, num5);
        createUser.setInt(6, num6);
        int rowsUpdated = createUser.executeUpdate();
        createUser.close();
    }

    connection.close();
}

    public static void main(String[] args){
        try {
            new LottoJDBC();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

2 Answers2

0

The simplest way is to use the Scanner class, as stated there, and probably at tons of other places.

As soon as the program runs the "reader.nextInt();" line, your program will stop and prompt the user for an entry in the console. That being said, you will want to check the datas entered by the user, as someone might want to enter invalid entries such as letters to see what happens (and in this case, it will throw an exception).

Community
  • 1
  • 1
Kishlin
  • 373
  • 2
  • 15
0

Not the most ideal way but would definitely give you an idea for your issue regarding getting input from user.

public class LottoJDBC {
        private static int num1;
        private static int num2;
        private static int num3;
        private static int num4;
        private static int num5;
        private static int num6;

        public LottoJDBC() throws Exception

        {
            System.out.println(num1);
            System.out.println(num2);
            System.out.println(num3);
            System.out.println(num4);
            System.out.println(num5);
            System.out.println(num6);
        }

        public static void main(String[] args){
            try {
                Scanner scanner = new Scanner(System.in);

                num1 = scanner.nextInt();
                num2 = scanner.nextInt();
                num3 = scanner.nextInt();
                num4 = scanner.nextInt();
                num5 = scanner.nextInt();
                num6 = scanner.nextInt();

                new LottoJDBC();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
Nick Div
  • 5,338
  • 12
  • 65
  • 127