简体   繁体   English

Java / MySql-获取一行并将其转换为arraylist <String>

[英]Java/MySql - getting a row and convert it to a arraylist<String>

I am trying to get a row and convert it to a array I alredy tried to convert the resultset.getArray() to a ArrayList but for some reason it returned nothing somebody help me with this 我试图获取一行并将其转换为数组,但是我尝试将resultset.getArray()转换为ArrayList,但是由于某种原因,它没有返回任何帮助

code now: 现在的代码:

public ArrayList<String> getReport(String name) {
    try {
        Statement sql =  mySql.getConnection().createStatement();
        ResultSet resultSet = sql.executeQuery("SELECT * FROM `reports` WHERE `reportedplayer`='" + name + "';");
        if(!resultSet.next()) {
            sql.close();
            resultSet.close();
            return null;
        }
        ArrayList<String> rowValues = new ArrayList<String>();
        while (resultSet.next()) {
            rowValues.add(resultSet.getString("reason"));
        }
        sql.close();
        resultSet.close();
        return rowValues;
    }catch (SQLException e) {
        e.printStackTrace();
        return null;
    }
}

You should read column names from resultSet metadata and then iterate over the names for each row. 您应该从resultSet元数据中读取列名称,然后遍历每行的名称。 Maybe better idea will be to store the parameters as ArrayList<HashMap<String,String>> than into ArrayList<String> , since select * doesn't say anything about order of the fileds you read. 也许更好的主意是将参数存储为ArrayList<HashMap<String,String>>不是存储到ArrayList<String> ,因为select *不会说明您读取的文件的顺序。 But if you know for sure you have just one row, and you know the order, or maybe the order doesn't matter, you could use something like this: 但是,如果您确定只知道一行,并且知道顺序,或者顺序无关紧要,则可以使用以下内容:

public ArrayList<String> getReport(String name) {
    try {
        Statement sql =  mySql.getConnection().createStatement();
        ResultSet resultSet = sql.executeQuery("SELECT * FROM `reports` WHERE `reportedplayer`='" + name + "';");
        if(!resultSet.next()) {
            sql.close();
            resultSet.close();
            return null;
        }
        ArrayList<String> rowValues = new ArrayList<String>();
        int columnCount = resultSet.getMetaData().getColumnCount();
        String[] columnNames = new String[columnCount];
        for (int idx=0; idx<columnCount; idx++) {
            columnNames[idx] = resultSet.getMetaData().getColumnName(idx);
        }

        while (resultSet.next()) {
            for (String columnName: columnNames) {
                rowValues.add(resultSet.getString(columnName));
            }
        }
        sql.close();
        resultSet.close();
        return rowValues;
    }catch (SQLException e) {
        e.printStackTrace();
        return null;
    }
}

Your line: 您的电话:

if (!resultSet.next())

Is moving to the second row without even looking at the first row. 甚至不看第一行就移到第二行。 You should also have a finally block to handle closing to ensure that it is always carried out properly. 您还应该有一个finally块来处理关闭操作,以确保始终正确执行关闭操作。

try {
    Statement sql =  mySql.getConnection().createStatement();
    ResultSet resultSet = sql.executeQuery("SELECT * FROM `reports` WHERE `reportedplayer`='" + name + "';");
    List<String> rowValues = new ArrayList<String>();
    while (resultSet.next()) {
        rowValues.add(resultSet.getString("reason"));
    }
    return rowValues.isEmpty() ? null : rowValues;
} catch (SQLException e) {
    e.printStackTrace();
    return null;
} finally {
    sql.close();
    resultSet.close();
}

resultSet.next() will move the cursor to the next row. resultSet.next()将光标移动到下一行。 1st time when you call resultSet.next() in the if condition it will point to 1st row and when you again call resultSet.next() in the while condition it will point to the 2nd row. 第一次在if条件中调用resultSet.next()时,它将指向第一行;而在while条件中再次调用resultSet.next()时,它将指向第二行。

since you are using the resultSet.next() twice you will be getting the 2nd row onwards into the list. 由于您两次使用resultSet.next(),因此将第二行向前放入列表。 If you have only one row in the table you will not get anything in the list. 如果表中只有一行,则列表中将不会有任何内容。

You can close the connection in the finally block as shown below. 您可以在finally块中关闭连接,如下所示。

    finally
    {
        try {              
        sql.close();
        resultSet.close();
        } catch (SQLException e) {
        e.printStackTrace();
        }
    }

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

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