简体   繁体   English

如何使用 SQL 'select *' 查询的 jdbc 结果集创建和填充二维数组作为真值表

[英]How to use jdbc ResultSet of a SQL 'select *' query to create and populate a 2 dimensional array as a truth table

I am trying to select the entire contents of a MSSQL Server table populated with int values to serve as a 'truth table'.我正在尝试 select 用 int 值填充的 MSSQL Server 表的全部内容作为“真值表”。 The goal is to select the entire SQL table, and populate it in Java as a 2 dimensional array.目标是 select 整个 SQL 表,并将其作为二维数组填充到 Java 中。 Here is a token source table I am working with (with the table name SANITIZED): SQLServerTable I may have a variable number of rows, but the number of columns will be fixed (so the array will not be a jagged array).这是我正在使用的令牌源表(表名为 SANITIZED): SQLServerTable我可能有可变数量的行,但列数将是固定的(因此数组不会是锯齿状数组)。 The table is seeded with token values to represent row-then-column for ease of troubleshooting (13 being row 1, column 3, 24 being row 2, column 4...)该表使用标记值来表示先行后列,以便于进行故障排除(13 是第 1 行,第 3 列,24 是第 2 行,第 4 列...)

Here is the code I have tried (SANITIZED for anonymity):这是我试过的代码(为匿名而消毒):

public class SANITIZED {

private static String url = "jdbc:sqlserver://SANITIZED:SANITIZED;DatabaseName=SANITIZED;encrypt=true;trustServerCertificate=true";
    
public static void main(String[] args) {

int numRows = 0;
                
try {
Connection conn = DriverManager.getConnection(url, "SANITIZED", "SANITIZED");
//  Establish Connection Object
Statement statement = conn.createStatement();
//  Create a SQL statement object to send to the database
ResultSet resultSet = statement.executeQuery("select count (*) from SANITIZED");  //  Execute the statement object
//  Process the result
if (resultSet.next()) { // just in case
numRows = resultSet.getInt(1); // note that indexes are one-based
}
} catch (SQLException e) {
e.printStackTrace();
}

int[][] truthTable = new int[numRows][27];
        
try {
Connection conn = DriverManager.getConnection(url, "SANITIZED", "SANITIZED");  //  Establish Connection Object
Statement statement = conn.createStatement();
//  Create a SQL statement object to send to the database
ResultSet resultSet = statement.executeQuery("select * from SANITIZED");  //  Execute the statement object
//  Process the result
while(resultSet.next()) {

System.out.println("resultSet: " + (resultSet.getInt(1)));
System.out.println("resultSet: " + (resultSet.getInt(2)));
System.out.println("resultSet: " + (resultSet.getInt(3)));
System.out.println("resultSet: " + (resultSet.getInt(4)));
            
truthTable[numRows - 2][0] = resultSet.getInt(1);
truthTable[numRows - 2][1] = resultSet.getInt(2);
truthTable[numRows - 2][2] = resultSet.getInt(3);
truthTable[numRows - 2][3] = resultSet.getInt(4);
}

The output from the println statements is: resultSet: 11 resultSet: 12 resultSet: 13 resultSet: 14 resultSet: 21 resultSet: 22 resultSet: 23 resultSet: 24 println 语句中的 output 是: resultSet: 11 resultSet: 12 resultSet: 13 resultSet: 14 resultSet: 21 resultSet: 22 resultSet: 23 resultSet: 24

But when I try to populate a 2 dimensional array, the Eclipse debugger is showing the SECOND row of the SQL table populating into the FIRST row of the array despite the columnIndex of the getInt() method being passed as 1. This is true even if I comment the println statements out.但是,当我尝试填充二维数组时,Eclipse 调试器显示 SQL 表的第二行填充到数组的第一行,尽管 getInt() 方法的 columnIndex 被传递为 1。即使我把 println 语句注释掉了。 Here is the debugger output: truthTable (id=24) [0] (id=27) [0] 21 [1] 22 [2] 23 [3] 24 [4] 0这是调试器 output: truthTable (id=24) [0] (id=27) [0] 21 [1] 22 [2] 23 [3] 24 [4] 0

The code above hard codes the row of the array ([numRows - 2]) for simplicity - but ideally, the goal is to iterate over the ResultSet as necessary in hopes that a single SQL 'select *' query that returns only int values from the DB (having a specific number of columns but a variable number of rows) can be used to create and populate an array of a fixed number of columns with a variable number of rows.为简单起见,上面的代码对数组的行 ([numRows - 2]) 进行硬编码 - 但理想情况下,目标是根据需要迭代 ResultSet,希望单个 SQL 'select *' 查询仅返回 int 值数据库(具有特定数量的列但可变数量的行)可用于创建和填充具有可变行数的固定数量的列的数组。 My thanks to the community for any assistance in understanding how this may be accomplished.我感谢社区在理解如何实现这一点方面提供的任何帮助。

The println statements for a given index prints values from the first row of the SQL table.给定索引的 println 语句打印 SQL 表第一行的值。
The attempt to use the same indexes to populate an array (in the absence of the println statements) populates the values from the second row of the array.尝试使用相同的索引填充数组(在没有 println 语句的情况下)填充数组第二行的值。 Why would it print the first DB row, but store the second DB row when the same index is being used?为什么它会打印第一个数据库行,但在使用相同索引时存储第二个数据库行?

Try the following.尝试以下操作。 You may also have introduced an issue with resultSet.getIndex() indexing from 1 rather that 0.您可能还引入了 resultSet.getIndex() 从 1 而不是 0 开始索引的问题。

    List<int[]> orderedTruthTableList = new ArrayList<>();
    try {
        Connection conn = DriverManager.getConnection(url, "SANITIZED", "SANITIZED"); // Establish Connection Object
        Statement statement = conn.createStatement();
        // Create a SQL statement object to send to the database
        ResultSet resultSet = statement.executeQuery("select * from SANITIZED"); // Execute the statement object

        // Process the result
        while (resultSet.next()) {

            orderedTruthTableList.add(new int[] {resultSet.getInt(1), resultSet.getInt(2), resultSet.getInt(3), resultSet.getInt(4)});

        }
    } finally {

    }

    int[][] truthTable = orderedTruthTableList.toArray(new int[0][0]);

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

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