不允许使用HSQLDB参数标记

时间:2017-04-28 16:59:00

标签: java jdbc prepared-statement hsqldb

使用hsqldb和preparedStatement给出了这个错误:

java.sql.SQLSyntaxErrorException: parameter marker not allowed
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.executeUpdate(Unknown Source)

我试过了:

String sql = "INSERT INTO Emergency (Id, status, typed, typeb, floor, 
locationX, locationY) Values(CAST(? AS INT), CAST(? AS INT), CAST(? AS 
VARCHAR(50)), CAST(? AS VARCHAR(50)), CAST(? AS INTEGER), CAST(? AS 
INTEGER), CAST(? AS INTEGER))";

String sql = "INSERT INTO Emergency (Id, status, typed, typeb, floor, 
locationX, locationY) Values(?,?,?,?,?,?,?)";

同样的错误。

编辑:更多代码

    try {
        con = DriverManager.getConnection(
                "jdbc:hsqldb:file:hsqldb; shutdown=true", "root", "");
        Statement stmt = con.createStatement();

        // Alle Kunden ausgeben
        //double help = ((number * pow(2.0, j)) - 1);
        String sql = "INSERT INTO Emergency (Id, status, typed, typeb, floor, locationX, locationY) Values(CAST(? AS INT), CAST(? AS INT), CAST(? AS VARCHAR(50)), CAST(? AS VARCHAR(50)), CAST(? AS INTEGER), CAST(? AS INTEGER), CAST(? AS INTEGER))";
        //Id INTEGER, status INTEGER, typeD VARCHAR(50) , typeB VARCHAR(50), floor INTEGER, locationX INTEGER, locationY INTEGER

        PreparedStatement pstmt = con.prepareStatement(sql);
        pstmt.setInt(1, Counter.emergencyID);
        pstmt.setInt(2, emergency.status);
        pstmt.setString(3, emergency.typeD);
        pstmt.setString(4, emergency.typeB);
        pstmt.setInt(5, emergency.floorID);
        pstmt.setInt(6, emergency.locationXY[0]);
        pstmt.setInt(7, emergency.locationXY[1]);
        Statement st = null;

        st = con.createStatement();    // statements

        int i = st.executeUpdate(sql);    // run the query

        Counter.emergencyID++;

        if (i == -1) {
            System.out.println("db error : " + sql);
        }

        st.close();
        System.out.println("Eintrag in DB");

    } catch (SQLException e) {
        e.printStackTrace();
    } finally {

        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

我正在添加所有值,然后执行它。

有人可以解释一下这里的错误是什么吗? 没找到任何有用的搜索。

1 个答案:

答案 0 :(得分:0)

  1. 您不需要投射您的类型,您只需要了解PreparedStatement的工作原理。

  2. 您正在使用Statement和Prepared Statement,但是您不需要Statement,您只需要执行Prepared Statement,所以请使用它:

  3.     String sql = "INSERT INTO Emergency (Id, status, typed, typeb, floor, 
                      locationX, locationY) Values(?,?,?,?,?,?,?)";
    
        PreparedStatement pstmt = connection.prepareStatement(sql);
    
            pstmt.setInt(1, Counter.emergencyID);
            pstmt.setInt(2, emergency.status);
            pstmt.setString(3, emergency.typeD);
            pstmt.setString(4, emergency.typeB);
            pstmt.setInt(5, emergency.floorID);
            pstmt.setInt(6, emergency.locationXY[0]);
            pstmt.setInt(7, emergency.locationXY[1]);
    
            int i = pstmt.executeUpdate();//execute pstmt no need to st