Java MySQL ResultSet为空,但MySQL命令正确[关闭]

nkkqxpd9  于 2024-01-05  发布在  Mysql
关注(0)|答案(1)|浏览(108)

**已关闭。**此问题需要debugging details。目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
8天前关闭
Improve this question
我试图通过hwid值搜索从MySQL数据库中获取expire值。当我将MySQL命令写入控制台时,我得到了以下内容:

mysql> select expire from users where hwid = 0001;
+---------------+
| expire        |
+---------------+
| 20            |
+---------------+

字符串
但是在java上,使用相同的命令ResultSet会关闭,没有任何异常。MySQL.accessGet()的结果是ResultSet,带有Result set representing update count of -1消息。

String command = "SELECT expire FROM users WHERE hwid = " + hwid + ";";
System.out.println(command); // "SELECT expire FROM users WHERE hwid = 0001;"
ResultSet resultSet = MySQL.accessGet( command );
if (resultSet == null || resultSet.isClosed() || !resultSet.next()) return "OK\n\nfalse+unknown";

if (Long.parseLong(resultSet.getString( 0 )) > 10) return "OK\n\ntrue";
return "OK\n\nfalse+expired";


我试图改变命令,但只有当我不给给予任何WHERE选项时它才能工作。

afdcj2ne

afdcj2ne1#

尝试使用单引号将参数括起来,如

String command = "SELECT expire FROM users WHERE hwid = '" + hwid + "';";

字符串
除此之外,我建议您使用PreparedStatement而不是普通Statement来避免SQL注入攻击,例如

PreparedStatement pstmt = connection.prepareStatement("SELECT expire FROM users WHERE hwid = ?");
pstmt.setString(1, "0001");
ResultSet resultSet = pstmt.executeQuery();
if (resultSet == null || resultSet.isClosed() || !resultSet.next()) return "OK\n\nfalse+unknown";

if (Long.parseLong(resultSet.getString( 0 )) > 10) return "OK\n\ntrue";
return "OK\n\nfalse+expired";

相关问题