ResultSet不适用于INSERT查询?错误消息:类型不匹配:无法从int转换为String [英] ResultSet is not for INSERT query? Error message: Type mismatch: cannot convert from int to String

查看:166
本文介绍了ResultSet不适用于INSERT查询?错误消息:类型不匹配:无法从int转换为String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我在'结果集'中弄错了。我插入数据的代码是:

I think I got it wrong at the 'resultset'. My code for inserting data is :

ResultSet columns = statement.executeUpdate("INSERT INTO Feedback (Username, Feedbacks) VALUES( '" + user + "','"+ msg +"')");

编译错误是:


类型不匹配:无法从int转换为String

Type mismatch: cannot convert from int to String

你能帮助我吗?

推荐答案

语句#cuteUpdate() 不返回 ResultSet ,但是 int 表示受影响的行。修复如下。

Statement#executeUpdate() doesn't return a ResultSet, but an int representing the affected rows. Fix it as follows.

int affectedRows = statement.executeUpdate(sql);

或者,如果您对受影响的行数不感兴趣,请忽略它。

Or if you aren't interested in the amount of affected rows, just ignore it.

statement.executeUpdate(sql);

顺便说一句,编译错误本身就很奇怪。你不是在看错线吗?该错误告诉您正在尝试将 int 结果分配给 String ,但您的代码显示您'重新尝试将 int 结果分配给 ResultSet

By the way, the compilation error at its own is weird. Aren't you looking at the wrong line? The error tells that you're trying to assign an int result to a String, but your code shows that you're trying to assign int result to a ResultSet.

如果用户 msg 是用户控制输入的一部分,那么这段代码很容易 SQL注入 攻击。我强烈建议用 Statement rel =nofollow> PreparedStatement 然后。另请参见本教程如何使用预准备语句。

That said and unrelated to your current problem, if user and msg are parts of user-controlled input, then this piece of code is prone to SQL injection attacks. I strongly recommend to replace Statement by PreparedStatement then. See also this tutorial how to use prepared statement.

这篇关于ResultSet不适用于INSERT查询?错误消息:类型不匹配:无法从int转换为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆