【问题标题】:Inserting auto generated id in sqllite using java [duplicate]使用java在sqlite中插入自动生成的ID [重复]
【发布时间】:2015-07-24 13:16:07
【问题描述】:

我正在使用 sqllite 编写 java 应用程序

public Product createProduct(String productName) throws SQLException {
    String query = "INSERT into tbl_Product (name) values (?) ";
    PreparedStatement preparedStatement = null;
    Connection connection = null;
    ResultSet rs = null;
    Product product = null;
    try {
        connection = ConnectionFactory.getConnection();
        preparedStatement = connection.prepareStatement(query);
        //preStatement.setInt(1, 0);
        preparedStatement.setString(1, productName);
        preparedStatement.executeUpdate();

    } finally {

        //preparedStatement.close();
        DbUtil.close(rs);
        DbUtil.close(preparedStatement);
        DbUtil.close(connection);
    }
    return product;

}

我的产品表有 (ID,Name) 列,其中 Id 是自动生成的。需要什么所有 java 更改,以便 preStatement 可以在 db 中插入自动生成的 id。

【问题讨论】:

  • setString(1,productName) 因为你只有 1 个参数
  • 这是错误我得到 e = (java.sql.SQLException) java.sql.SQLException: [SQLITE_ERROR] SQL 错误或缺少数据库(表 tbl_Product 有 2 列但提供了 1 个值)这个案例。
  • 您需要明确指定要插入的列,例如INSERT into tbl_Product(name) values (?)
  • 我认为它有效并转到下一条语句,但是当我执行 preStatement.executeQuery();或 preStatement.executeUpdate();我收到以下异常,目标 VM 中发生异常:SQLite JDBC 驱动程序未实现 java.sql.SQLException:SQLite JDBC 驱动程序未实现

标签: java database sqlite jdbc auto-generate


【解决方案1】:

String query = "INSERT into tbl_Product values (?)";

ResultSet rs = null;
Product product = null;
try {
    connection = ConnectionFactory.getConnection();
    preStatement = (PreparedStatement) connection.prepareStatement(query);
    preStatement.setString(1, productName);
    rs = preStatement.executeQuery();

【讨论】:

  • 不,我有 Column(Id,Name)。如果我给 preStatement.setString(1, productName);,它抱怨只插入一列。我的自动生成的 ID 应该如何插入到 preStatement 中?
  • 在执行前添加 prep.addBatch(); prep.executeBatch();
  • HIString sqlQuery = "INSERT into tbl_Product values (?)"; PreparedStatement stmt = conn.prepareStatement(sqlQuery); stmt.setString(1, "val"); //nameText 变量包含 jtextfield 的文本) stmt.executeUpdate();它的工作正常......:)
【解决方案2】:
String sqlQuery = "INSERT into tbl_Product  values (?)                 
PreparedStatement stmt = conn.prepareStatement(sqlQuery);  
stmt.setString( 1, "val" ); //nameText variable contains the text of the jtextfield) 
stmt.executeUpdate();

使用执行更新而不是执行查询。

executeQuery():

在此 PreparedStatement 对象中执行 SQL 查询,并返回查询生成的 ResultSet 对象。

executeUpdate():

执行此 PreparedStatement 对象中的 SQL 语句,该语句必须是 SQL INSERT、UPDATE 或 DELETE 语句;或不返回任何内容的 SQL 语句,例如 DDL 语句。

【讨论】:

  • 我仍然得到相同的错误 e = (java.sql.SQLException) java.sql.SQLException: [SQLITE_ERROR] SQL 错误或缺少数据库(表 tbl_Product 有 2 列但提供了 1 个值)。你在用 sqllite 吗?
  • 请检查您是否将 Autoincrement 设置为 id
  • 请检查您创建的表是这样的。CREATE TABLE tbl_Product (id integer primary key auto increment, name varchar(20));否则删除表创建使用此查询然后尝试它。
  • 这是我的表 CREATE TABLE tbl_Product (Id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT NOT NULL);
  • 请手动执行并更新我。 INSERT INTO tbl_Product (Name)VALUES ('java');
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-03
  • 1970-01-01
  • 2019-12-10
  • 2012-10-24
  • 1970-01-01
相关资源
最近更新 更多