简体   繁体   English

带有JDBC的MySQL,没有while循环,select不起作用,with不需要下一个查询

[英]MySQL with JDBC, without while loop, select doesn't work, with while it doesn't take next query

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();
}

Without while loop, select doesn't work, with while it doesn't take next query. 如果没有while循环,则select不起作用,而while则不接受下一个查询。

The ResultSet is very general propose interface and it is cursor based . ResultSet是非常通用的提议接口,它基于游标

Here is quote from official documentation: 这是官方文档的报价:

A ResultSet object maintains a cursor pointing to its current row of data. ResultSet对象维护一个游标,该游标指向其当前数据行。 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方法将光标移动到下一行,并且由于当ResultSet对象中没有更多行时它返回false ,因此可以在while循环中使用它来迭代结果集。

This is means you have to perform next to move cursor. 这意味着您必须执行next才能移动光标。 Here is the graphical illustration. 是图形图示。

So why ResultSet is cursor based? 那么,为什么ResultSet是基于游标的呢?

To allow underlying implementation not load the potentially huge number of rows into memory. 为了允许基础实现不将潜在的大量行加载到内存中。 The cursor is the very common pattern to kind of scroll over the potentially big data. 光标是滚动潜在大数据的一种非常常见的模式。 For example the PgResultSet uses ResultCursor to do so. 例如, PgResultSet使用ResultCursor这样做。

PS Also please always perform ResultSet.close() to free up resources. PS另外,请始终执行ResultSet.close()以释放资源。

First, in using replaceAll you must escape all character, one time for Java, and second time for regex. 首先,在使用replaceAll您必须转义所有字符,对于Java是一次,对于regex是第二次。

So if you use a \\ in a replaceAll you must write like that \\\\\\\\ . 因此,如果在replaceAll使用\\ ,则必须这样写\\\\\\\\

Second, print with sysout the content of inst[i] before send it. 其次,在发送之前先用sysout打印inst[i]的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM