gpt4 book ai didi

java - java中的ExecuteUpdate成功将记录添加到PostgreSQL但表不会更新

转载 作者:行者123 更新时间:2023-12-02 11:19:39 25 4
gpt4 key购买 nike

我目前正在根据 Spring Boot 应用程序客户端的输入通过 executeUpdate 将数据添加到 PostgreSQL 数据库。我要添加评论经度纬度地址用户名在 java 方法中使用以下代码到 Review 表。

我使用此行在 Controller 类的方法中传递信息:

addReviewMethod.addReview( comment,  longitude,  latitude,  address,  username);

这会将数据传递给另一个 java 类并将其插入到表中,如下所示:

public void addReview(String comment, double longitude, double latitude, String address, String username) {

Connection c = null;
Statement stmt = null;

try {
Class.forName("org.postgresql.Driver");
c = DriverManager
.getConnection("jdbc:postgresql://localhost:5432/arearatingpgdb",
"postgres", "root");
c.setAutoCommit(false);

stmt = c.createStatement();
int rs = stmt.executeUpdate( "INSERT INTO reviews VALUES (nextval('reviews_id_seq'::regclass), '" +comment +" '," +longitude+"," +latitude+", '"+address+"', '"+username+"');" );

System.out.println("Successfully added: " + comment + longitude + latitude + address + username);
/* while ( rs.next() ) {
Review review = rs.getDouble("price");
System.out.println();
housePrices.add(price);
}*/
//rs.close();
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
}

我在 Controller 类和执行插入的类中都包含了打印调试器,它们打印出正确的信息,显示一切都应该正常工作。

我遇到的问题是,当我查看表的内容时,即 pgAdmin4 中的 select * from Reviews 时,表中唯一的行是我硬编码的行。更令人困惑的是当我通过查询工具将另一行硬编码到表中时,id 列反射(reflect)这些行实际上是从 java 端添加的,但它们没有显示在表中。例如,我硬编码为 2 行,然后我尝试运行这些方法 4 次,然后我硬编码为另外 2 行,数据库如下所示。

 id | val 
----+-----
1 | foo
2 | bar
6 | foo
7 | bar

我在这里做错了什么吗?

最佳答案

您必须提交这些更改。

stmt.close() 之前执行 c.commit() 或将 c.setAutoCommit(false); 更改为 c.setAutoCommit(true);

关于java - java中的ExecuteUpdate成功将记录添加到PostgreSQL但表不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50026311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com