gpt4 book ai didi

java - 为什么我会收到此错误 : E/ERROR: The executeQuery method must return a result set. ?

转载 作者:行者123 更新时间:2023-12-02 09:54:46 25 4
gpt4 key购买 nike

我正在尝试将我的项目与 SQL-Server 数据库连接。但我总是收到此错误 E/ERROR:executeQuery 方法必须返回结果集。

Class.forName("net.sourceforge.jtds.jdbc.Driver");
String username = "un";
String password = "pass";

conn = DriverManager.getConnection("jdbc:jtds:sqlserver://ip/db;user=" + username + ";password=" + password);

Log.w("Connection","open");

String sql = "INSERT INTO TABLE" +
"(Cliente, NomePessoa, Email, NivelSatisfacao, Nota) " +
"VALUES ('" + informacao.getNomeCliente() + "', '" + informacao.getNome() + "', '" + informacao.getEmail() + "', '" + informacao.getSatisfacao() + "', '" + informacao.getNota() + "') ";
Statement stmt = conn.createStatement();
ResultSet rSet = stmt.executeQuery(sql); // error here

我尝试将 stmt.executeQuery 更改为 stmt.executeUpdate,但它用红色下划线表示,并表示输出为 int,所以它不兼容。

最佳答案

使用PreparedStatement更安全。

Class.forName("net.sourceforge.jtds.jdbc.Driver");
String username = "un";
String password = "pass";

conn = DriverManager.getConnection("jdbc:jtds:sqlserver://ip/db;user=" + username + ";password=" + password);

Log.w("Connection","open");

String sql = "INSERT INTO TABLE" +
"(Cliente, NomePessoa, Email, NivelSatisfacao, Nota) " +
"VALUES (?, ?, ?, ?, ?)";

try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, informacao.getNomeCliente())
pstmt.setString(2, informacao.getNome())
pstmt.setString(3, informacao.getEmail())
pstmt.setString(4, informacao.getSatisfacao())
pstmt.setString(5, informacao.getNota())

int result = pstmt.executeUpdate();

} catch (SQLException e) {
e.printStackTrace();
}

关于java - 为什么我会收到此错误 : E/ERROR: The executeQuery method must return a result set. ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56078968/

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