BatchUpdateException:where子句中的未知列

时间:2014-09-26 17:12:52

标签: java mysql sql

写入mySQL数据库时,出现以下错误:

java.sql.BatchUpdateException: Unknown column 'ALFA' in 'where clause'

这是我的java代码:

public void pushWinner(String game, String teamW) throws SQLException{   
    String[] t1 = game.split("-");
    String statement = "update games set winner=(?) where team1 = "+t1[0]+" AND team2 = "+t1[1];
    try (PreparedStatement pstmt = conn.prepareStatement(statement)) {                 
            pstmt.setString(1, teamW);
            pstmt.addBatch();
        pstmt.executeBatch();
        pstmt.close();
    } catch (SQLException ex) {
        System.out.println(ex);
    }
}

我真的不知道where子句有什么问题......

修改

查看我的评论,忘了提及' ALFA'是

team1和team2的数据类型都是VARCHAR(45)。

3 个答案:

答案 0 :(得分:0)

试试这个:,因为column team1team2的数据类型为VARCHAR,所以请single quote进行比较。< / p>

 queryString= "update games set winner=(?) where team1 = '"+t1[0]+"' AND team2 = '"+t1[1]+"'";

答案 1 :(得分:0)

你在制作像这样的sql时遇到麻烦。使用带有单独参数的预准备语句而不是内联参数构建。将停止这样的问题并处理参数转义。那么用吗?在主sql中去除parm的位置并使用.setString(1,value);首先设置(是的,基于1)。或者setInt(1,intValue)......取决于数据类型。对于日期使用java.sql.Timestamp - 可以将日历转换为日期,将java.util.Date转换为sql时间戳或新的javax.time或joda。但是不要使用内联。

http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

为什么

  1. 易于编程和
  2. https://www.owasp.org/index.php/SQL_Injection
  3. 从java教程复制:

        String updateString =
            "update " + dbName + ".COFFEES " +
            "set SALES = ? where COF_NAME = ?";
    
        String updateStatement =
            "update " + dbName + ".COFFEES " +
            "set TOTAL = TOTAL + ? " +
            "where COF_NAME = ?";
    
        try {
            con.setAutoCommit(false);
            updateSales = con.prepareStatement(updateString);
            updateTotal = con.prepareStatement(updateStatement);
    
            for (Map.Entry<String, Integer> e : salesForWeek.entrySet()) {
                updateSales.setInt(1, e.getValue().intValue());
                updateSales.setString(2, e.getKey());
                updateSales.executeUpdate();
                updateTotal.setInt(1, e.getValue().intValue());
                updateTotal.setString(2, e.getKey());
                updateTotal.executeUpdate();
                con.commit();
            }
        } catch (SQLException e ) {
            JDBCTutorialUtilities.printSQLException(e);
            if (con != null) {
                try {
                    System.err.print("Transaction is being rolled back");
                    con.rollback();
                } catch(SQLException excep) {
                    JDBCTutorialUtilities.printSQLException(excep);
                }
            }
        } finally {
            if (updateSales != null) {
                updateSales.close();
            }
            if (updateTotal != null) {
                updateTotal.close();
            }
            con.setAutoCommit(true);
        }
    }
    

答案 2 :(得分:0)

如果您的数据类型是字符串,那么您传递的变量值

周围需要Single Quote 像这样......

String statement = "update games set winner=(?) where team1 = '"+t1[0]+"' AND team2 = ' "+ t1[1] + "'";