gpt4 book ai didi

java - 如何从 CSV 读取 H2 结果集,并重置光标以进行第二遍

转载 作者:行者123 更新时间:2023-11-30 04:09:53 28 4
gpt4 key购买 nike

我正在编写一个 Java 库,用于从 CSV 文件导入数据,并根据一些正则表达式测试的结果以 INTEGER、DECIMAL、VARCHAR 和其他列类型返回结果。

因此,我需要读取 Csv.read() 返回的 ResultSet 两次;一次确定列类型,然后再次填充新的 SimpleResultSet (或者最好将原始 ResultSet 包装在 SimpleRowSource 中,所以我不'不必存储表数据)。我尝试调用 ResultSet.beforeFirst(),但第二遍没有可用行。

// Create an input stream with test data.
String data =
"name,value\n" +
"one,1\n" +
"two,2\n" +
"three,3\n";
InputStream stream = new ByteArrayInputStream(data.getBytes());
Reader reader = new InputStreamReader(stream);

// Read the test data into a new ResultSet.
Csv csv = new Csv();
ResultSet resultSet = csv.read(reader, null);

// Iterate over the ResultSet on first pass.
int firstPass = 0;
while (resultSet.next())
{
// Test all values for integer, decimal etc.
++firstPass;
}
System.out.println("Read " + firstPass + " row(s) on first pass");

// Move the cursor to before the first row; this doesn't work!
resultSet.beforeFirst();

// Check if the cursor is before the first row; throws exception!
/* resultSet.isBeforeFirst(); */

// Iterate over the ResultSet on second pass.
int secondPass = 0;
while (resultSet.next())
{
// Add all values to new SimpleResultSet, or wrap this code
// in an H2 SimpleRowSource, and pass to the constructor of
// a new SimpleResultSet.
++secondPass;
}
System.out.println("Read " + secondPass + " row(s) on second pass");

这会产生以下输出:

Read 3 row(s) on first pass
Read 0 row(s) on second pass

我注意到 beforeFirst() 不会抛出异常,但 isBeforeFirst() 会抛出异常:

org.h2.jdbc.JdbcSQLException: Feature not supported: null [50100-172]

如果有任何关于为什么这行不通的建议,或者其他方法的建议,我将不胜感激!

更新

由于 H2 Csv.read() 函数生成一个无法回滚的 ResultSet,因此我最终复制了源 InputStream,因为它是在第一遍读取,然后在第二遍重播生成的 byte[]。这样,我就可以确保导入相同的数据。这是我为此编写的辅助类:

public class CopyStream extends InputStream
{
private final InputStream mInputStream;
private final ByteArrayOutputStream mOutputStream;

public CopyStream(InputStream inputStream)
{
// Initialise the class.
mInputStream = inputStream;
mOutputStream = new ByteArrayOutputStream();
}

public InputStream copy()
{
// Return a new input stream based on the copied data.
byte[] buffer = mOutputStream.toByteArray();
return new ByteArrayInputStream(buffer);
}

@Override
public int read() throws IOException
{
// Read a byte from the input stream.
int b = mInputStream.read();
if (b >= 0)
{
// Copy the byte to the output stream.
mOutputStream.write(b);
}
return b;
}

@Override
public int read(byte[] buffer) throws IOException
{
// Read some bytes from the input stream.
int length = mInputStream.read(buffer);
if (length > 0)
{
// Copy the bytes to the output stream.
mOutputStream.write(buffer, 0, length);
}
return length;
}

@Override
public int read(byte[] buffer, int offset, int length) throws IOException
{
// Read some bytes from the input stream.
length = mInputStream.read(buffer, offset, length);
if (length > 0)
{
// Copy the bytes to the output stream.
mOutputStream.write(buffer, offset, length);
}
return length;
}
}

其用法如下:

Csv csv = new Csv();
CopyStream copyStream = new CopyStream(inputStream);
ResultSet resultSet1 = csv.read(new InputStreamReader(copyStream), null);
ResultSet resultSet2 = csv.read(new InputStreamReader(copyStream.copy()), null);

最佳答案

您不需要读取 ResultSet 两次。 您可以使用非标准 H2 命令 CSVREAD 从文件填充表格。

或者, 您可以再次创建 Csv 对象。

关于java - 如何从 CSV 读取 H2 结果集,并重置光标以进行第二遍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19890680/

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