gpt4 book ai didi

java - 带有 JDBC 的 MySQL,没有 while 循环,select 不起作用,while 它不接受下一个查询

转载 作者:行者123 更新时间:2023-11-30 21:43:33 24 4
gpt4 key购买 nike

FileReader fr = new FileReader(new File("D:\\folder\\mySQLFile1.txt"));
BufferedReader br = new BufferedReader(fr);

while((s = br.readLine()) != null) {
sb.append(s);
}
br.close();

String sbq=sb.toString();
String S1 = sbq.replaceAll("(?:/\\*(?:[^*]|(?:\\*+[^*/]))*\\*+/)|(?://.*)","");
String[] inst = S1.split(";|/");

for (int i = 0; i<inst.length; i++) {
if (!inst[i].trim().equals("")) {
ResultSet resultSet = stmt.executeQuery(inst[i]);
ResultSetMetaData rsmd = resultSet.getMetaData();
int columnsNumber = rsmd.getColumnCount();
// while (resultSet.next()) {
for (int x = 1; x <= columnsNumber; x++) {
String columnValue = resultSet.getString(x);
System.out.print(columnValue + " " );
}
System.out.println("");
// }
}
System.out.println();
}

没有 while 循环,select 不起作用,while 则不会进行下一个查询。

最佳答案

ResultSet 是非常通用的建议接口(interface),它是基于游标的

引用官方文档:

A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.

这意味着您必须执行next 来移动光标。 Here是图形说明。

那么为什么 ResultSet 是基于游标的呢?

允许底层实现不将潜在的大量行加载到内存中。游标是一种非常常见的模式,可以在潜在的大数据上滚动。例如 PgResultSet使用 ResultCursor这样做。

附言此外,请始终执行 ResultSet.close() 以释放资源。

关于java - 带有 JDBC 的 MySQL,没有 while 循环,select 不起作用,while 它不接受下一个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50528446/

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