daZza
daZza

Reputation: 1689

Every String[] within an ArrayList has the same value

I am trying to create Oracle SQL scripts for creating/replacing several views by using information acquired from the data dictionary.

I already managed to set up a working database connection via JDBC drivers and a query to the data dictionary, which works as well and returns the correct values.

However upon storing the queried information in a String array - which is then added to an ArrayList of String[] - something seems to go wrong, as all arrays seem to have the same values in their respective index positions and I don't have a clue why that is.

Here's my code, I'd appreciate if someone can spot the error:

public ArrayList<String[]>  getDataDictionary(ArrayList<String> dbInfo, String table) throws SQLException {

ArrayList<String[]> result = new ArrayList<String[]>();
String[] resultTemp = new String[2];

... connection variables (URL, User, Pass)
... get connection, etc.

try {
            Statement statement = con.createStatement();
            ResultSet rs = statement.executeQuery("SELECT COLUMN_NAME,DATA_TYPE FROM USER_TAB_COLUMNS WHERE TABLE_NAME = '" + table + "'");

            while (rs.next()) {
                resultTemp[0] = rs.getString("COLUMN_NAME");
                resultTemp[1] = rs.getString("DATA_TYPE");

                // database values
                System.out.println(rs.getString("COLUMN_NAME"));
                System.out.println(rs.getString("DATA_TYPE"));
                // array values
                System.out.println(resultTemp[0]);
                System.out.println(resultTemp[1]);

                //The above sout's return the proper values for each pass of the loop
                //This is what feels the strangest to me. The values are correct here, but when queried later they are wrong

                result.add(resultTemp);
            }

            String[] test = new String[2];

            // sout's return wrong values now, i.e. the value returned is always the same for all arrays queried in the ArrayList
            //I don't understand how that can be, because the correct values were added to the ArrayList a few lines above and now they are wrong with no changes made
            test = result.get(0);
            System.out.println(test[0]);
            System.out.println(test[1]);

            test = result.get(1);
            System.out.println(test[0]);
            System.out.println(test[1]);

            test = result.get(2);
            System.out.println(test[0]);
            System.out.println(test[1]);

            rs.close();
            statement.close();
            con.close();

            return result;  
} catch(Exception e)
        {
            e.printStackTrace();
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Error!");
            alert.setHeaderText("Invalid SQL!");
            alert.setContentText("Please verify the information you provided!");
            alert.showAndWait();
            return null;
        }

Upvotes: 0

Views: 126

Answers (3)

Jens
Jens

Reputation: 69450

You store a reference to the same object in every loop.

You have to create in every lopp a new Array:

 while (rs.next()) {
      resultTemp = new String[2];

                resultTemp[0] = rs.getString("COLUMN_NAME");
                resultTemp[1] = rs.getString("DATA_TYPE");

                // database values
                System.out.println(rs.getString("COLUMN_NAME"));
                System.out.println(rs.getString("DATA_TYPE"));
                // array values
                System.out.println(resultTemp[0]);
                System.out.println(resultTemp[1]);

                //The above sout's return the proper values for each pass of the loop
                //This is what feels the strangest to me. The values are correct here, but when queried later they are wrong

                result.add(resultTemp);
            }

Upvotes: 3

Santhosh
Santhosh

Reputation: 8197

You are overriding the same array , String[] resultTemp = new String[2];

while (rs.next()) {
            String[] resultTemp = new String[2];
            resultTemp[0] = rs.getString("COLUMN_NAME");
            resultTemp[1] = rs.getString("DATA_TYPE");

Intialise it inside the while loop. So that when you add

result.add(resultTemp);

result will hold the list of resultTemp[] objects inside it .

Upvotes: 2

Eran
Eran

Reputation: 393841

You should create the array instance inside the loop.

       while (rs.next()) {
            String[] resultTemp = new String[2];
            resultTemp[0] = rs.getString("COLUMN_NAME");
            resultTemp[1] = rs.getString("DATA_TYPE");
            ....

Failing to do so causes the same array to be added to result multiple times.

Upvotes: 3

Related Questions