This question already has an answer here:
这个问题在这里已有答案:
- How to perform an SQL insert in Java 1 answer
如何在Java 1答案中执行SQL插入
I am writing java application using sqllite
我正在使用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;
}
My product table have (ID,Name) column, where Id is auto generated. What all java changes are required so that preStatement can insert auto generated id in db.
我的产品表有(ID,Name)列,其中Id是自动生成的。所有java更改都是必需的,以便preStatement可以在db中插入自动生成的id。
2 个解决方案
#1
String query = "INSERT into tbl_Product values (?) ";
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();
#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();
use execute update instead of execute query.
使用执行更新而不是执行查询。
executeQuery()
:
Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query.
在此PreparedStatement对象中执行SQL查询,并返回查询生成的ResultSet对象。
executeUpdate()
:
Executes the SQL statement in this PreparedStatement object, which must be an SQL INSERT, UPDATE or DELETE statement; or an SQL statement that returns nothing, such as a DDL statement.
执行此PreparedStatement对象中的SQL语句,该对象必须是SQL INSERT,UPDATE或DELETE语句;或者不返回任何内容的SQL语句,例如DDL语句。
#1
String query = "INSERT into tbl_Product values (?) ";
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();
#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();
use execute update instead of execute query.
使用执行更新而不是执行查询。
executeQuery()
:
Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query.
在此PreparedStatement对象中执行SQL查询,并返回查询生成的ResultSet对象。
executeUpdate()
:
Executes the SQL statement in this PreparedStatement object, which must be an SQL INSERT, UPDATE or DELETE statement; or an SQL statement that returns nothing, such as a DDL statement.
执行此PreparedStatement对象中的SQL语句,该对象必须是SQL INSERT,UPDATE或DELETE语句;或者不返回任何内容的SQL语句,例如DDL语句。