【问题标题】:How to convert prepared statement into String [duplicate]如何将准备好的语句转换为字符串 [重复]
【发布时间】:2022-01-04 00:27:44
【问题描述】:

我正在尝试使用准备好的语句和结果集将字符串值设置到数据库中。但它会将此“com.mysql.cj.jdbc.result.ResultSetImpl@fcd6521”插入数据库行

PreparedStatement check = connection.prepareStatement(
        "SELECT buildingName FROM building GROUP BY buildingId HAVING buildingId = ?");
check.setInt(1, building_id);
ResultSet rs = check.executeQuery();
insert.setString(1, String.valueOf(rs));
insert.executeUpdate();

【问题讨论】:

  • 您希望从String.valueOf(rs) 得到什么?它不会给你数据。
  • 除此之外,如果可能的话,我会使用一条 SQL 语句:INSERT INTO ... (...) VALUES ( (SELECT buildingName FROM building GROUP BY buildingId HAVING buildingId = ?)...)

标签: java mysql jdbc


【解决方案1】:

我认为您需要阅读@user16320675 指示的教程 ResultSet 是一种集合。必须像这样访问它:

//if you expects receive only one buildName you use a simple if, otherwise you have to use a loop and a list or something like that
String text;
if(rs.next()){
    text = rs.getString(0);
}
//index it's related to buildingName

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-23
    • 2017-04-09
    • 2021-05-09
    • 2011-04-18
    • 2015-03-12
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多