java mysql始终指定列错误

java mysql始终指定列错误,java,mysql,Java,Mysql,我需要在更新sql时指定列名。(使用?=?) 始终获取以下错误信息: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''d11'=1 where cityi

我需要在更新sql时指定列名。(使用?=?) 始终获取以下错误信息:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL 
syntax; check the manual that corresponds to your MySQL 
server version for the right syntax to use near ''d11'=1 where cityid=11143'
at line 1
代码片段

conn.setAutoCommit(false);
    pstmt  = conn.prepareStatement("update usercityday set ?=? where cityid=?");
    for (int i = 1; i < 91727; i++ ) {
        for (int j = 1; j < 32; j++ ){
            if (map[i][j] != 0){
                pstmt.clearParameters();

                pstmt.setString(1, "d" + String.valueOf(j));
                pstmt.setInt(2, map[i][j]);
                pstmt.setInt(3, i);
                pstmt.executeUpdate();
                ic++;
                if (ic %1000 == 0) {
                    conn.commit();
                }
            }
        }
    }
conn.setAutoCommit(假);
pstmt=conn.prepareStatement(“更新用户cityDay集?=?其中cityid=?”;
对于(int i=1;i<91727;i++){
对于(int j=1;j<32;j++){
如果(map[i][j]!=0){
pstmt.clearParameters();
pstmt.设置字符串(1,“d”+字符串值(j));
pstmt.setInt(2,map[i][j]);
pstmt.setInt(3,i);
pstmt.executeUpdate();
ic++;
如果(ic%1000==0){
conn.commit();
}
}
}
}

你的第一次?在哪?不是字符串值。它是SQL代码的一部分。您正在生成这样的代码:

update usercityday set 'd11'=11143 where cityid=2
这不是有效的SQL

一个简单的解决方案是在循环中指定语句,并对SQL查询执行如下字符串concat:

conn.setAutoCommit(false);
for (int i = 1; i < 91727; i++ ) {
    for (int j = 1; j < 32; j++ ){
        if (map[i][j] != 0){
            pstmt = conn.prepareStatement("update usercityday set d"+String.valueOf(j)+"=? where cityid=?");
            pstmt.setInt(1, map[i][j]);
            pstmt.setInt(2, i);
            pstmt.executeUpdate();
            ic++;
            if (ic %1000 == 0) {
                conn.commit();
            }
        }
    }
}
conn.setAutoCommit(假);
对于(int i=1;i<91727;i++){
对于(int j=1;j<32;j++){
如果(map[i][j]!=0){
pstmt=conn.prepareStatement(“更新用户cityDay集d”+String.valueOf(j)+“=?其中cityid=?”;
pstmt.setInt(1,map[i][j]);
pstmt.setInt(2,i);
pstmt.executeUpdate();
ic++;
如果(ic%1000==0){
conn.commit();
}
}
}
}

我认为您不能设置后缀的列名,因为SQL是在使用PreparedStatements时预编译的。您可以使用语句(可能导致SQL注入错误),也可以在创建语句之前设置列名