【发布时间】: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