gpt4 book ai didi

mysql - 从表的每一行中检索每列的每个属性 mysql java

转载 作者:行者123 更新时间:2023-11-29 21:05:16 26 4
gpt4 key购买 nike

到目前为止,我知道可以使用以下代码从表中选择第 n 行
SELECT * FROM table ORDER BY column_name LIMIT n-1,1 ;
在下面的代码中,我尝试从名为 responsable 的表的每一行中检索每一列的每个属性,该表具有以下列:id、name、surname、mail、password。 id 是整数,其他列的类型是字符串

java.sql.Connection connection = null;
int rowCount;
try {connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name","root","");
Statement stmt = null;
ResultSet result = null;
ResultSet rs = null;
rs = stmt.executeQuery("SELECT COUNT(*) FROM responsable");
rowCount = rs.getInt(1);
if(rowCount!=0)
{
for(int i=0;i<rowCount;i++)
{result= stmt.executeQuery("SELECT * FROM responsable ORDER BY id LIMIT"+i+",1");
System.out.put(result.getInt(1));
System.out.put(result.getString(2));
System.out.put(result.getString(3));
System.out.put(result.getString(4));
System.out.put(result.getString(5));}
}
}catch (Exception ex) {
out.println("Unable to connect to database");
}


我正在尝试执行这段代码,但没有得到任何结果。任何帮助将不胜感激。

最佳答案

您的 stmt 变量为空。您需要先创建语句。

Statement stmt = connection.createStatement();

此外,您需要调用 next() 从结果集中检索下一行。例如

if (rs.next()) {
rowCount = rs.getInt(1);
}

...

if (result.next()) {
System.out.put(result.getInt(1));
System.out.put(result.getString(2));
System.out.put(result.getString(3));
System.out.put(result.getString(4));
System.out.put(result.getString(5));
}

关于mysql - 从表的每一行中检索每列的每个属性 mysql java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36875613/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com