如何使用 Java 和 MySQL 确定插入或更新是否成功?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24378270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you determine if an insert or update was successful using Java and MySQL?
提问by user3239558
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".
根据“执行”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;
}
采纳答案by Elliott Frisch
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 ( Returns) 链接中,强调了添加,
either (1) the row countfor SQL Data Manipulation Language(DML) statements or (2) 0 for SQL statements that return nothing.
(1) SQL 数据操作语言(DML) 语句的行数或 (2) 0 对于不返回任何内容的 SQL 语句。
If you want to insert a large number of entries, I would prefer addBatch()
and executeBatch()
.
如果要插入大量条目,我更喜欢addBatch()
和executeBatch()
。
回答by Viraj
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/7/docs/api/java/sql/PreparedStatement.html#executeUpdate())
You can do a select count(*) do validate number of records if you want.
如果需要,您可以执行 select count(*) 验证记录数。
回答by SparkOn
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
试试这个并检查插入是否发生
回答by Pradeep Ramisetti
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.
试试这个,无论你想知道数据是否被插入,如果记录被插入,它返回真或假。
if(action > 0){
return true;
}else{
return false;
}