Insert a row to MySQL table with jsp

14,094

since ID is auto-incremented, get rid of it from the list, preparedstatement is looking for index 1 on its parameters.

String queryString = "INSERT INTO users(login,password,full_name,ulevel,team_id) VALUES (?, ?, ?, ?, ?)";
pstatement = connection.prepareStatement(queryString);
pstatement.setString(1, login);
pstatement.setString(2, password);
pstatement.setString(3, full_name);
pstatement.setString(4, ulevel);
pstatement.setString(5, team_id);

this one below is not tested

just start you parameter with 1 because there are only 5 parameters to be defined.

String queryString = "INSERT INTO users(user_id,login,password,full_name,ulevel,team_id) VALUES (null, ?, ?, ?, ?, ?)";
pstatement = connection.prepareStatement(queryString);
pstatement.setString(1, login);
pstatement.setString(2, password);
pstatement.setString(3, full_name);
pstatement.setString(4, ulevel);
pstatement.setString(5, team_id);
Share:
14,094
steven967
Author by

steven967

Updated on June 04, 2022

Comments

  • steven967
    steven967 almost 2 years

    I want to insert rows to mysql database with an jsp code, but I can´t. Here is my jsp code without the html form:

    <%
    String login = request.getParameter("login");
    String password = request.getParameter("password");
    String full_name = request.getParameter("full_name");
    String ulevel = request.getParameter("ulevel");
    String team_id = request.getParameter("team_id");
    
    Connection connection = null;
    PreparedStatement pstatement = null;
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    int updateQuery = 0;
    if(login!=null && password!=null && full_name!=null && ulevel!=null && team_id!=null){
    if(login!="" && password!="" && full_name!="" && ulevel!="" && team_id!="") {
        try {
    connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/android","root","root");
    String queryString = "INSERT INTO users(user_id,login,password,full_name,ulevel,team_id) VALUES (null, ?, ?, ?, ?, ?)";
    pstatement = connection.prepareStatement(queryString);
    pstatement.setString(2, login);
    pstatement.setString(3, password);
    pstatement.setString(4, full_name);
    pstatement.setString(5, ulevel);
    pstatement.setString(6, team_id);
    updateQuery = pstatement.executeUpdate();
    if (updateQuery != 0) { %> 
    

    When I press SUBMIT, the webpage shows me this:

    type Status report message descriptionThe requested resource () is not available.

    p.s.: column user_id is set to autoincrement in mysql table, so I use null.

    But I dont know, that´s the right way...

    I run the code from netbeans 7.0.1