简体   繁体   English

如何从java中的executeUpdate或更新获取值?

[英]How to get a value from an executeUpdate OR update in java?

Im trying to get some returning data from an postgresql update. 我试图从PostgreSQL更新中获取一些返回数据。 The "update" works well in the DB script, but in java I'm having some issues: the code stucks while executing the query. “更新”在数据库脚本中运行良好,但是在Java中,我遇到了一些问题:执行查询时代码卡住了。

I'm trying with "PreparedStatament ps", "ResultSet rs=ps.executeQuery" and "if(rs.next)" to get the data from the "returning" in the query, but it appears that the code didnt get in there, because it stuck exactly in the "ps.executeQuery". 我正在尝试使用“ PreparedStatament ps”,“ ResultSet rs = ps.executeQuery”和“ if(rs.next)”从查询中的“ returning”中获取数据,但是似乎代码没有到达那里,因为它正好卡在“ ps.executeQuery”中。

Here's more or less what I'm trying to do: 这或多或少是我想要做的:

String cVal="";
ps=myConn.prepareStatement("UPDATE someTable SET aValue=? WHERE bValue=? 
returning Cvalue");
ps.setInt(1,"aNewValue");
ps.setInt(2,"aID");
rs=ps.executeQuery();
if (rs.next()){
  cVal=rs.GetString("Cvalue");
  return true;
}else{
  return false;
}

I'm trying to set the values of "RETURNING" to some variables. 我正在尝试将“ RETURNING”的值设置为某些变量。 There are 4 results that I want to set. 我要设置4个结果。

Since you are trying to update, in case of PreparedStatement, you have to use executeUpdate() method. 由于您尝试更新,因此在PreparedStatement的情况下,必须使用executeUpdate()方法。

To know more details about methods in Statement and PreparedStatment, check below the 要了解有关Statement和PreparedStatment中方法的更多详细信息,请检查以下内容

https://docs.oracle.com/javase/8/docs/api/java/sql/PreparedStatement.html#executeUpdate-- https://docs.oracle.com/javase/8/docs/api/java/sql/PreparedStatement.html#executeUpdate--

You can refer below an example. 您可以在下面参考一个示例。 https://www.mkyong.com/jdbc/jdbc-preparestatement-example-update-a-record/ https://www.mkyong.com/jdbc/jdbc-preparestatement-example-update-a-record/

First execute the update statement with ps.executeUpdate () , which returns the integer 0 or 1 . 首先使用ps.executeUpdate ()执行更新语句,该语句返回整数01 The value indicates whether the update statement has executed or not. 该值指示更新语句是否已执行。 After that, execute the select query to get the updated data. 之后,执行选择查询以获取更新的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM