简体   繁体   English

JDBC PreparedStatement.setString()在SQLite中不能正常工作吗?

[英]JDBC PreparedStatement.setString() not working properly with SQLite?

I'm using Xerials SQLite JDBC driver and want to do the simplest thing you can imagine: 我正在使用Xerials SQLite JDBC驱动程序,并想做您可以想象的最简单的事情:

I have created the following table: 我已经创建了下表:

CREATE TABLE IF NOT EXISTS households (hostname_id_pk INTEGER PRIMARY KEY, hostname TEXT UNIQUE, vm TEXT);

Now, as I want a parametrized insertion statment I use PreparedStatment as follows: 现在,由于需要参数化的插入语句,因此按如下方式使用PreparedStatment:

public static void main(String[] args) throws ClassNotFoundException {
    Class.forName("org.sqlite.JDBC");
    Connection con = null;
    PreparedStatement updateHouseholdStmt = null;

    try {
        con = DriverManager.getConnection(DB_IDENTIFIER);           

        String getHouseholdString = "SELECT hostname_id_pk FROM households WHERE hostname = ?";
        String updateHouseholdString = "INSERT INTO households (hostname, vm) values(?, 'VM1')";

        getHouseholdStmt = con.prepareStatement(getHouseholdString);
        getHouseholdStmt.setString(1, "test1");

        updateHouseholdStmt = con.prepareStatement(updateHouseholdString);
        getHouseholdStmt.setString(1, "test1");

        ResultSet hhRS = getHouseholdStmt.executeQuery();
        int hhId = -1;

        if(hhRS.next()){
            hhId = hhRS.getInt(1);
            System.out.println(hhId);
        } else {
            System.out.println(updateHouseholdStmt.executeUpdate());
        }

    } catch (SQLException e) {
        System.err.println(e.getMessage());
    } finally {
        try {
            if (con != null) {
                con.close();
            }
            if (getHouseholdStmt != null){
                getHouseholdStmt.close();
            }
            if (updateHouseholdStmt != null) {
                updateHouseholdStmt.close();
            }
        } catch (SQLException e) {
            System.err.println(e);
        }
    }

}

In this case, the only thing I can find in the DB is a new entry with (1, , VM1) meaning that the entry is created, but the string parameter is missing for some reason. 在这种情况下,我在数据库中唯一能找到的是带有(1, , VM1)的新条目,这意味着该条目已创建,但是由于某种原因缺少了字符串参数。

When I'm replacing the "?" 当我替换“?”时 in the updateHousholdStatement() with a sample value and remove the .setString(...) method line everything works fine. 在带有示例值的updateHousholdStatement()并删除.setString(...)方法行,一切正常。

What the heck am I missing?! 我到底想念什么? I already checked this a thousand times this morning. 今天早上我已经检查了一千遍了。

Thx in advance. 提前谢谢。

Please correct the following lines: 请更正以下几行:

updateHouseholdStmt = con.prepareStatement(updateHouseholdString);
getHouseholdStmt.setString(1, "test1");

to

updateHouseholdStmt = con.prepareStatement(updateHouseholdString);
updateHouseholdStmt.setString(1, "test1");

When updating, you're setting and executing the wrong variable: 更新时,您正在设置和执行错误的变量:

getHouseholdStmt = con.prepareStatement(getHouseholdString);
getHouseholdStmt.setString(1, "test1");

updateHouseholdStmt = con.prepareStatement(updateHouseholdString);
getHouseholdStmt.setString(1, "test1"); //updating the select query
ResultSet hhRS = getHouseholdStmt.executeQuery(); //you're executing the select query again!

It should be: 它应该是:

getHouseholdStmt = con.prepareStatement(getHouseholdString);
getHouseholdStmt.setString(1, "test1");

updateHouseholdStmt = con.prepareStatement(updateHouseholdString);
updateHouseholdStmt .setString(1, "test1"); //update stmt 
ResultSet hhRS = updateHouseholdStmt .executeQuery(); //update stmt

First, getHouseholdStmt Statement isn't declared as a PreparedStatement. 首先,未将getHouseholdStmt语句声明为PreparedStatement。 Second, you need to executeUpdate() your PreparedStatement for updateHouseholdStmt(updateHouseholdStatement.executeUpdate();) 其次,您需要为updateHouseholdStmt(updateHouseholdStatement.executeUpdate();)执行PreparedStatement。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JDBC PreparedStatement.setString 抛出 Out Of Bounds 异常 - JDBC PreparedStatement.setString throws Out Of Bounds exception PostgreSQL JDBC 中 int/numeric 列上的 PreparedStatement.setString - PreparedStatement.setString on int/numeric columns in PostgreSQL JDBC PreparedStatement.setString()调用行为异常 - PreparedStatement.setString() call behaving oddly 循环内的 PreparedStatement.setString 是如何工作的? - how PreparedStatement.setString inside of the loop works? prepareStatement.setString(1,“ null”)被解释为null而不是字符串(在prepareStatement.addBatch()之后) - preparedStatement.setString(1,“null”), is being interpreted as null instead of a string (after preparedStatement.addBatch()) 如何在PreparedStatement.setString中修复“预期的声明,最终变量或有效地最终变量” - How to fix “Declaration, final or effectively final variable expected” in a PreparedStatement.setString 为什么准备好的语句在 jdbc 中不起作用? - Why the preparedStatement is not working in jdbc? 具有PreparedStatement的JDBC批处理在MySQL中不起作用 - JDBC batch with PreparedStatement not working in MySQL setString()中未替换JDBC和c3p0 PreparedStatement中的问号占位符 - Question Mark placeholder in JDBC and c3p0 PreparedStatement not being replaced in setString() SQLite JDBC的Connection.preparedStatement中的NullPointerException - NullPointerException from Connection.preparedStatement for SQLite JDBC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM