Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将MySQL数据库中的值存储到变量中?_Java_Sql_Return - Fatal编程技术网

Java 如何将MySQL数据库中的值存储到变量中?

Java 如何将MySQL数据库中的值存储到变量中?,java,sql,return,Java,Sql,Return,这个问题可能很简单,但我不太确定如何做到这一点。 我想将SQL语句的返回值存储在变量中 int i; String name = "Testuser"; 首先我创建我的var。现在我想将值存储在数据库(命名数据)中名为“money”的字段中,并将键“Testuser”存储到我的变量i中 现在您已经有了语句的实例,您可以这样做了 int i; ResultSet resultat = null; resultat = statement.executeQuery("SELECT mo

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

int i;
String name = "Testuser";

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

现在您已经有了语句的实例,您可以这样做了

   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
}

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