表未更新[关闭]

问题描述 投票:-1回答:1

编辑:此问题已解决!

我正在更新表上的数据。这是代码:

@Override
public void UpdateQnsAns(Exam exam, String subject) {
    try {
        String sql = "update table " + subject + "_tbl set question=?, option1=?, option2=?,"
                + " option3=?, option4=?, answers=? where id=?";
        PreparedStatement pst = new DBConnection().getConnection().prepareStatement(sql);
        pst.setString(1, exam.getQuestion());
        pst.setString(2, exam.getOption1());
        pst.setString(3, exam.getOption2());
        pst.setString(4, exam.getOption3());
        pst.setString(5, exam.getOption4());
        pst.setString(6, exam.getCorrectOption());
        pst.setInt(7, exam.getId());
        pst.executeUpdate();
    } catch (SQLException ex) {
        Logger.getLogger(ExamDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
}

[我猜这是根据glassfish服务器在pst.executeUpdate()处显示的错误,因为下面的错误的第三行(com.edu.daoImpl.ExamDaoImpl.UpdateQnsAns(ExamDaoImpl.java:120))将我链接到pst.executeUpdate行。错误显示如下:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'table java_tbl set question='What is the default value of Short variable?', opti' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2794)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359)
at com.edu.daoImpl.ExamDaoImpl.UpdateQnsAns(ExamDaoImpl.java:120)
at com.edu.defaultServlet.ExamServlet.processRequest(ExamServlet.java:221)
at com.edu.defaultServlet.ExamServlet.doPost(ExamServlet.java:257)
java mysql sql jsp jdbc
1个回答
1
投票

更新表中行的语法是

UPDATE tablename ...
不是
UPDATE TABLE tablename ...
© www.soinside.com 2019 - 2024. All rights reserved.