1

我在使用 Java 和 JDBC 在 Oracle 中插入时遇到问题。得到的错误是:

java.sql.SQLException: ORA-00917: 缺少逗号

插入的数据取自字符串等形式,并被解析为适当的数据类型,然后保存在名为 edicio 的对象中。没关系。然后,我的意图是使用该对象的数据在数据库中插入。

这是 DAO 的代码,我在其中进行插入:

public Edicio insertarCurs(Connection con, Edicio ed) throws SQLException {
    PreparedStatement stm = null;
    ResultSet rst = null;

    // Insert
    StringBuffer sql = new StringBuffer();
    sql.append("INSERT INTO curs (id, nom, idarea, area, programa, datainici)");
    sql.append(" VALUES (?, ?, ?, ?, ?, ?");
    logger.info("Building insert works fine.");

    try {
        stm = con.prepareStatement(sql.toString());
        // params
        stm.setLong(1, ed.getIdEdicio());
        stm.setString(2, ed.getNomEdicio());
        stm.setLong(3, ed.getIdArea());
        stm.setString(4, ed.getArea());
        stm.setString(5, ed.getPrograma());
        // Conversion from Java Date to SQL Date
        java.sql.Date sqlDate = new java.sql.Date(ed.getDataInici().getTime());
        logger.info("sqlDate before the insert is: "+ sqlDate); //0011-12-02
        stm.setDate(6, sqlDate);

                // Data and results commented
        logger.info("Id edicio: "+ ed.getIdEdicio()); //6
        logger.info("Nom edicio: "+ ed.getNomEdicio()); //test
        logger.info("Id area: "+ ed.getIdArea()); //0
        logger.info("Nom area: "+ ed.getArea()); //test
        logger.info("Programa: "+ ed.getPrograma()); //test
        logger.info("Data inici: "+ sqlDate); //2011-06-06

        // We are going to execute the insert
        int numRows = stm.executeUpdate();
        // The program never reaches this point, fails doing the executeUpdate()
                logger.info("Rows created: "+ numFiles);
                ...

变量类型有:

idEdicio = long  
nomEdicio = String  
idArea = long  
area = String  
programa = String  
dataInici = Date  

有人能帮我吗?先感谢您 :)

4

2 回答 2

6

失踪)

sql.append(" VALUES (?, ?, ?, ?, ?, ?");

应该

sql.append(" VALUES (?, ?, ?, ?, ?, ?)");
于 2011-03-31T08:28:23.403 回答
5
sql.append(" VALUES (?, ?, ?, ?, ?, ?)");
                                     ^--- missing parenthesis
于 2011-03-31T08:28:36.930 回答