java - SQL select 命令只返回列名
问题描述
在 MySQL 上的 Java JDBC 中使用 select 查询时,它似乎只返回列的名称,而不是与它们关联的值。例如,Content
即使这是列的名称而不是它的值,以下方法也会返回。
public <T> T select(String column, @Nullable List<DBCondition> conditions) {
try (Statement statement = MySQL.createStatement()) {
ResultSet rs = statement.executeQuery("select '" + column + "' from " + name + (conditions == null || conditions.isEmpty() ? "" : " " + MySQL.conditionsListToString(conditions)) + ";");
rs.next();
return (T) rs.getObject(column);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
在调试创建的查询时,最终select Content from testtable where Name='Main_test';
返回的是 Content 而不是{"Test":"Workss"}
预期的。这是意料之中的,因为在 MySQL“调试器”中运行完全相同的查询会返回
+-------------------+
| Content |
+-------------------+
| {"Test":"Workss"} |
+-------------------+
1 row in set (0.00 sec)
我做错了什么,我该如何解决?
解决方案
不要使用'。利用" + column + "
推荐阅读
- lxc - 无法设置 lxc.apparmor.profile
- amazon-web-services - API 网关中缺少身份验证令牌错误
- c# - Azure-Speaker Recognition API 扬声器识别 - 获取操作状态:消息字段中出现错误,出现此错误“IncompleteEnrollment”
- c++ - C ++将结构传递给函数以访问嵌套结构
- javascript - 当字符串包含特殊字符时,正则表达式查询缓慢
- visual-studio - 有没有办法在 Azure devops tfs 冲刺板上显示代码审查请求/响应工作项
- haproxy - 为什么通过自身代理时 haproxy tcp check 不起作用
- drools - 显示为新的业务中心工作台测试场景触发的审计日志/规则?
- java - 如何在另一个类的方法中使用一个类的字段名称,这样如果名称更改会出现编译器问题
- c# - 如何将datagrid数据发送到xml文件