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();
}
}
}