【问题标题】:How do I store a value from MySQL Database into variable?如何将 MySQL 数据库中的值存储到变量中?
【发布时间】:2020-06-14 11:39:38
【问题描述】:

这个问题可能很简单,但我不太确定该怎么做。 我想将我的 SQL 语句的返回值存储在一个变量中。

int i;
String name = "Testuser";

首先我创建我的 var。现在我想将值存储在我的数据库 (named Data) 中名为 "money" 的字段中,并将键“Testuser”存储到我的变量 i .

【问题讨论】:

  • 编辑:我忘了说,我的键是一个字符串字段。请检查我编辑的帖子
  • 当它回答您的问题时,不要忘记接受答案

标签: java sql return


【解决方案1】:

现在你已经有了语句的实例,你可以做类似的事情

   int i;
ResultSet resultat = null;
   resultat = statement.executeQuery("SELECT money FROM data WHERE id=1;");//for example the value of the field money in the row who has id=1
   if(resultat.next()){ // you check if the query returned a value
 i=resultat.getInt("money"); // the name of the column
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    相关资源
    最近更新 更多