java - 将嵌入式 Derby 数据库整个表放入字符串数组中

标签 java sql derby

我是 SQL 的初学者,尽管我了解得足够多了。这些语句并不是让我困惑的,而是与 Java 的结合让我困惑。

本质上,我正在尝试显示一个显示数据库中数据的JTable。我的思路是简单地将所有数据放入字符串数组中,并使用for循环添加到我的JTable的模型中

数据库表只有 2 列:varchar(30)date。尽管使用 Derby,但我完全迷失了方向,而且在线文档对我来说是拉丁文。我已经能够使用 ConnectionStatement 对象来执行 SQL 查询,毫无问题地设置数据库。

我认为我的答案位于ResultSet中的某个地方,因为我可以使用SELECT * FROM table_name查询整个表,只是不这样做知道如何将该对象放入字符串数组中。

对于这个杂乱的问题,我深表歉意,因为工作太累了。

TL;DR:从 Derby 数据库 将整个表放入 字符串数组 中。

最佳答案

您实际上并不需要获取字符串数组。您可以使用 next() 循环遍历 ResultSet

来自docs :

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.

类似这样的事情:

ResultSet rs = ...
while (rs.next()) {
    String yourVarCharColumn = rs.getNString("varchar column name");
    Date yourDateColumn = rs.getDate("date column name");
    // Add to your JTable
}

关于java - 将嵌入式 Derby 数据库整个表放入字符串数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23532066/

相关文章:

Java:旋转图像使其指向鼠标光标

java - 收到 Java 无法访问代码

java.sql.SQLIntegrityConstraintViolationException异常

java - 了解释放资源

java - StringBuilder 如何比普通 "+"更高效?

java - 如何优化Jooq中的SQL查询以返回表对象

sql - Oracle如何将多行合并为单行

php - Laravel 插入嵌套 Eloquent ORM 模型

java - Apache Derby : SQLSyntaxErrorException

hibernate - Spring Boot : Apache derby pool empty. 无法在 30 秒内获取连接