0

这是我的代码:

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")+"')");
            }

我在 stmt.executeUpdate 上收到错误消息。这是错误:

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)

如何修复错误?

4

3 回答 3

1

您的 Java 源文件中有语法错误,这与 Servlet 没有直接关系。

请注意这部分代码:

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

没有+连接"Insert into ",teams[team]" values ('"。要开始解决您的问题,请将该部分更改为:

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

从错误消息中,您可以自己弄清楚:

java.lang.Error: Unresolved compilation problems:表示您的文件甚至无法编译。

但最好的部分:

CreateTheTeams.doGet(CreateTheTeams.java:69)

告诉您它在哪里找到错误:在CreateTheTeams.java文件的第69行。下次您看到类似的内容时,请转到该行并仔细阅读,这可能会节省您一些时间。

于 2013-11-06T20:44:29.760 回答
1

你有两个错误:

  1. 编译错误,因为你有一个无效的String.
  2. Statement对两个不同的操作使用相同的。执行第二个操作时,ResultSet已经执行的操作会丢失。

解决每一个问题:

  1. 这不是一个有效的字符串:

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

    您需要String相应地连接 s :

    //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. 最好Statement为此使用新的:

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

    PreparedStatement但是,如果您使用 a来避免 SQL 注入,这会更好。为此,请将代码更改为:

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

此外,这不是错误,但似乎您永远不会更新count变量,因此if(count==3)永远不会发生。要修复它,count++请在执行 DML 插入操作后添加。

于 2013-11-06T20:46:23.063 回答
0

线

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

有编译错误。您需要添加 + 来连接语句中的字符串:

        stmt.executeUpdate("Insert into "+teams[team]+" values ('"+rs1.getString("studentno")+"', '"+rs1.getString("firstname")+"', '"+rs1.getString("surname")+"', '"+rs1.getString("degreecode")+"', '"+rs1.getString("dob")+"')");
于 2013-11-06T20:43:09.243 回答