简体   繁体   中英

Error in java servlet on stmt.executeUpdate

This is my code:

String[] teams = {"Blue Team", "Red Team", "Yellow Team", "Green Team", "Orange Team"};
            int count = 0, team = 0;
            String selectSQL = "select * from students"; // rand and distinct
            Statement stmt = conn.createStatement();
            ResultSet rs1 = stmt.executeQuery(selectSQL);
            // Retrieve the results
            while(rs1.next()){
                if(count==3){
                    team++;
                    count = 0;
                }
            stmt.executeUpdate("Insert into "teams[team]" values ('"+rs1.getString("studentno")+"', '"+rs1.getString("firstname")+"', '"+rs1.getString("surname")+"', '"+rs1.getString("degreecode")+"', '"+rs1.getString("dob")+"')");
            }

I get an error on stmt.executeUpdate. This is the error:

java.lang.Error: Unresolved compilation problems: 
    Syntax error, insert ")" to complete MethodInvocation
    Syntax error, insert ";" to complete Statement
    Syntax error on token "]", AssignmentOperator expected after this token
    Syntax error on token ")", delete this token

    CreateTheTeams.doGet(CreateTheTeams.java:69)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

How is possible to fix the error?

You have a syntax error in your Java source file, this has nothing to do with Servlet directly.

Notice in this part of your code:

stmt.executeUpdate("Insert into "teams[team]" values ('"+ // more code here

There is no + concatenanting the "Insert into " , teams[team] and " values ('" . To begin fixing your problems, change that part into:

stmt.executeUpdate("Insert into "+teams[team]+" values ('"+ // more code here
//                               ^           ^

From the error message, you can figure that out by yourself:

java.lang.Error: Unresolved compilation problems: states that your file couldn't even be compiled.

But the best part:

CreateTheTeams.doGet(CreateTheTeams.java:69)

Tells you where it is finding the error: at line 69 of the CreateTheTeams.java file. Next time you see something like that, get to that line and reread carefully, this may save you some time.

You have two errors:

  1. Compilation error, since you have an invalid String .
  2. Using a same Statement for two different operations. When performing the second operation, the ResultSet of the already executed operation will be lost.

To solve each one:

  1. This is not a valid String:

     "Insert into "teams[team]" values ('"+rs1.getString("studentno")+"', '"+rs1.getString("firstname")+"', '"+rs1.getString("surname")+"', '"+rs1.getString("degreecode")+"', '"+rs1.getString("dob")+"')"

    You need to concatenate the String s accordingly:

     //note the usage of + signs between teams[team] "Insert into " + teams[team] + " values ('" + rs1.getString("studentno") + "', '"+ rs1.getString("firstname") + "', '" + rs1.getString("surname")+ "', '" + rs1.getString("degreecode") + "', '" + rs1.getString("dob")+ "')"
  2. It would be better using a new Statement for this:

     String insertSql = "Insert into " + teams[team] + " values ('" + rs1.getString("studentno") + "', '"+ rs1.getString("firstname") + "', '" + rs1.getString("surname")+ "', '" + rs1.getString("degreecode") + "', '" + rs1.getString("dob")+ "')"; Statement stmtInsert = connection.createStatement(insertSql); stmtInsert.executeUpdate();

    But this will be even better if you use a PreparedStatement to avoid SQL Injection. To do this, change the code to:

     String insertSql = "Insert into " + teams[team] + " values (?, ?, ?, ?, ?)"; PreparedStatement pstmtInsert = connection.prepareStatement(insertSql); pstmt.setParameter(1, rs1.getString("studentno")); pstmt.setParameter(2, rs1.getString("firstname")); pstmt.setParameter(3, rs1.getString("surname")); pstmt.setParameter(4, rs1.getString("degreecode")); pstmt.setParameter(5, rs1.getString("dob")); pstmtInsert.executeUpdate(); pstmtInsert.close();

Also, not an error but seems that you never update count variable, so if(count==3) will never occur. To fix it, add a count++ after executing your DML insert operation.

The line

    stmt.executeUpdate("Insert into "teams[team]" values ('"+rs1.getString("studentno")+"', '"+rs1.getString("firstname")+"', '"+rs1.getString("surname")+"', '"+rs1.getString("degreecode")+"', '"+rs1.getString("dob")+"')");

has a compilation error. You need to add + to concatenate the strings in the statement:

        stmt.executeUpdate("Insert into "+teams[team]+" values ('"+rs1.getString("studentno")+"', '"+rs1.getString("firstname")+"', '"+rs1.getString("surname")+"', '"+rs1.getString("degreecode")+"', '"+rs1.getString("dob")+"')");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM