I created a simple registration form. I use struts2 and I want that when the user clicks on submit -> the data is displayed in the mysql database.
So, I'm trying to write data to database:
public void registerUser(){ user.add(username); user.add(password); user.add(email); user.add(picture); //connect to database try { Class.forName("com.mysql.jdbc.Driver"); try { connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/myFacebook?" + "user=root&password=root"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } //Insert Data into database try(PreparedStatement createUser = connection.prepareStatement("Insert into user(username, password, email, picture)" + "VALUES (?, ?, ?, ?)")){ for(int i = 0; i < 1000; i++) { createUser.setInt(0, i+1); } createUser.setString(1, user.get(0)); createUser.setString(2, user.get(1)); createUser.setString(3, user.get(2)); createUser.setString(4, user.get(3)); int rowsUpdated = createUser.executeUpdate(); createUser.close(); }catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } I decided to create an ArrayList in the User class, because it seems to me easier to write data.
ArrayList<String> user = new ArrayList<String>(); I run all my system through apache tomcat. Everything starts and does not give any errors. All methods and files are correctly connected to each other. But when I check data in the console, the data is not displayed and my user table is empty.
I just can not understand what the error is. Either the data from the form is somehow incorrectly called, or I incorrectly write it to the data.
Please help me figure it out. Thank you in advance!
UPD:
//Insert Data into database try(PreparedStatement createUser = connection.prepareStatement("Insert into user(user_id, username, password, email, picture)" + "VALUES (?, ?, ?, ?, ?)")){ createUser.setInt(0, userid); createUser.setString(1, user.get(0)); createUser.setString(2, user.get(1)); createUser.setString(3, user.get(2)); createUser.setString(4, user.get(3)); int rowsUpdated = createUser.executeUpdate(); createUser.close();