Using ResultSet, converting String to Array

0

I'm trying to convert some data that comes from DB into an Array, using ResultSet to select DB data. In the code below it does not give any error though. If I try to use one:

  

System.out.println (x [0]);

It does not show the first data, so it looks like the conversion was not done, I wanted to know if that's the way it converts, or if it can not be done within the while.

public static void main(String[] args) {

    connection = new Conexao().getConnection();
    String sql = "SELECT * FROM figura";
    try {
        PreparedStatement ps = connection.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();

        while(rs.next()){
            String x = rs.getString(2);
            String y = rs.getString(3);
            x.toCharArray();
            y.toCharArray();
            System.out.println(x);
        }

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
asked by anonymous 23.04.2015 / 21:51

1 answer

0

Are you willing to mount a Array with 2 values, or 2 Array with 1 value each? Depending on the case, you can create a model and create a ArrayList of its type.

You're using toCharArray() , so you want an array of char with the String captured from Bank? If not, you do not need to use this function. As for the code, have you tried anything like this?

public static void main(String[] args) {
    connection = new Conexao().getConnection();
    String sql = "SELECT * FROM figura";
    List<String> x = new ArrayList<String>();
    List<String> y = new ArrayList<String>();

    try {
        PreparedStatement ps = connection.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();

        while ( rs.next() ) {
            x.add( rs.getString(2) );
            y.add( rs.getString(3) );
        }

        for (String value : x) {
            System.out.println(x);
        }
        for (String value : y) {
            System.out.println(y);
        }

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
    
23.04.2015 / 23:01