I am using Java to connect to a MySQL database. I am trying to insert or update data into the database.
我使用Java连接到一个MySQL数据库。我正在尝试将数据插入或更新到数据库中。
Even though I am quite sure the insert was successful, it returns false.
尽管我很确定插入成功,但它返回false。
According to the "execute" API, the return value is "true if the first result is a ResultSet object; false if it is an update count or there are no results".
根据“execute”API,如果第一个结果是ResultSet对象,则返回值为true;如果是更新计数或没有结果,则为false。
How can I determine whether or not my insert or update was successful?
如何确定插入或更新是否成功?
public boolean insertSelections(String selection, String name){
String sql ="INSERT INTO WORKREPORT VALUES (?,?,?,?,?)";
boolean action = false;
try {
PreparedStatement stmt = conn.prepareStatement(sql);
SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("yyyy:MM:dd hh:mm:ss");
String formatDate = dateFormat.format(new java.util.Date(System.currentTimeMillis()));
java.util.Date mDate = dateFormat.parse(formatDate);
java.sql.Timestamp timeStamp = new java.sql.Timestamp(System.currentTimeMillis());
// Date time= new Date(mDate.getTime());
stmt.setInt(1, Integer.parseInt(getNumberByName(name).trim()));
stmt.setString(2, name);
// stmt.setDate(3, time);
stmt.setTimestamp(3, timeStamp);
stmt.setString(4, selection);
stmt.setString(5, "N/A");
action = stmt.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return action;
}
4 个解决方案
#1
20
Since you are using PreparedStatement
you can call executeUpdate()
-
由于使用的是PreparedStatement,所以可以调用executeUpdate() -
int count = stmt.executeUpdate();
action = (count > 0); // <-- something like this.
From the Javadoc (Returns) link above, emphasis added,
从上面的Javadoc(返回)链接中,强调添加,
either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing.
或者(1)对SQL数据操作语言(DML)语句的行计数,或者(2)对于返回无任何值的SQL语句的行数。
If you want to insert a large number of entries, I would prefer addBatch()
and executeBatch()
.
如果您想插入大量的条目,我更喜欢addBatch()和executeBatch()。
#2
9
First of all this you should know :
首先,你应该知道:
boolean execute() Executes the SQL statement in this PreparedStatement object, which may be any kind of SQL statement.
boolean execute()在这个PreparedStatement对象中执行SQL语句,该对象可以是任何类型的SQL语句。
ResultSet executeQuery() Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query.
ResultSet executeQuery()在这个PreparedStatement对象中执行SQL查询并返回查询生成的ResultSet对象。
int 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.
int executeUpdate()在这个PreparedStatement对象中执行SQL语句,它必须是SQL INSERT、UPDATE或DELETE语句;或者不返回任何内容的SQL语句,比如DDL语句。
int i=stmt.executeUpdate();
if(i>0)
{
System.out.println("success");
}
else
{
System.out.println("stuck somewhere");
}
Try this and check it out whether insert is happening or not
试试这个,看看插入是否发生
#3
0
Try this, whether you want to know whether the data is inserted or not , if the record is inserted it return true or else false.
尝试此操作,无论您是否想知道数据是否被插入,如果插入记录,它将返回true或else false。
if(action > 0){
return true;
}else{
return false;
}
#4
-1
If you don't get a exception I think query is went ok. Or, you might be able to use executeUpdate() (http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#executeUpdate() )
如果你没有一个例外,我认为查询没问题。或者,您可以使用executeUpdate() (http://docs.oracle.com/javase/doc7/s/api/java/sql/preparedstatement.html #executeUpdate())
You can do a select count(*) do validate number of records if you want.
如果需要,可以执行select count(*)验证记录的数量。
#1
20
Since you are using PreparedStatement
you can call executeUpdate()
-
由于使用的是PreparedStatement,所以可以调用executeUpdate() -
int count = stmt.executeUpdate();
action = (count > 0); // <-- something like this.
From the Javadoc (Returns) link above, emphasis added,
从上面的Javadoc(返回)链接中,强调添加,
either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing.
或者(1)对SQL数据操作语言(DML)语句的行计数,或者(2)对于返回无任何值的SQL语句的行数。
If you want to insert a large number of entries, I would prefer addBatch()
and executeBatch()
.
如果您想插入大量的条目,我更喜欢addBatch()和executeBatch()。
#2
9
First of all this you should know :
首先,你应该知道:
boolean execute() Executes the SQL statement in this PreparedStatement object, which may be any kind of SQL statement.
boolean execute()在这个PreparedStatement对象中执行SQL语句,该对象可以是任何类型的SQL语句。
ResultSet executeQuery() Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query.
ResultSet executeQuery()在这个PreparedStatement对象中执行SQL查询并返回查询生成的ResultSet对象。
int 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.
int executeUpdate()在这个PreparedStatement对象中执行SQL语句,它必须是SQL INSERT、UPDATE或DELETE语句;或者不返回任何内容的SQL语句,比如DDL语句。
int i=stmt.executeUpdate();
if(i>0)
{
System.out.println("success");
}
else
{
System.out.println("stuck somewhere");
}
Try this and check it out whether insert is happening or not
试试这个,看看插入是否发生
#3
0
Try this, whether you want to know whether the data is inserted or not , if the record is inserted it return true or else false.
尝试此操作,无论您是否想知道数据是否被插入,如果插入记录,它将返回true或else false。
if(action > 0){
return true;
}else{
return false;
}
#4
-1
If you don't get a exception I think query is went ok. Or, you might be able to use executeUpdate() (http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#executeUpdate() )
如果你没有一个例外,我认为查询没问题。或者,您可以使用executeUpdate() (http://docs.oracle.com/javase/doc7/s/api/java/sql/preparedstatement.html #executeUpdate())
You can do a select count(*) do validate number of records if you want.
如果需要,可以执行select count(*)验证记录的数量。