这是Java向数据库中插入值的问题。

我想将数据插入数据库,但是出现一个异常,指出“查询不返回结果”。我究竟做错了什么?我应该退还什么?

这是我的功能代码:

public void commitToDB(String fName) throws SQLException {
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    String query = "INSERT INTO users (firstname)" + " VALUES (?)";
    try {
        preparedStatement = connection.prepareStatement(query);
        preparedStatement.setString(1, fName);

        // execute the preparedstatement
        resultSet = preparedStatement.executeQuery();
    }
    catch(Exception e) {
        System.out.println("Got an exception");
        System.out.println(e.getMessage());
    }
    finally {
        preparedStatement.close();
        resultSet.close();
    }
}

最佳答案

您想执行一个更新语句(不返回ResultSet的操作),但是您试图将其作为查询执行。第一次使用Java数据库驱动程序时,我也犯了这个错误。

您要做的就是更改:

resultSet = preparedStatement.executeQuery();


对此:

preparedStatement.execute();

关于java - 将Java值插入sqlite数据库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43207181/

10-12 02:50