Java - returns an array from a method

I created this snippet:

public static String[] get_data() { conn = getInstance(); String[] data_array = null; if(conn != null) { Statement query; try { query = conn.createStatement(); String sql = "SELECT data_x FROM table_x"; ResultSet result = query.executeQuery(sql); result.next(); int count = result.getInt("data_x"); result.close(); data_x_array = new String[count]; for (int x = 1; x <= count; x++) { String data_x = result.getString(x); data_x_array[x] = data_x; } } catch (SQLException e) { e.printStackTrace(); } } return data_x_array; } 

I just created a class in which data from a database is collected in an array.

Now I just want to return an array from this method.

But I get:

 data_array cannot be resolved to a variable 

Can anybody help me?

Hello!

UPDATE:

i changed the fragment to:

 public static String[] get_data() { conn = getInstance(); String[] data_array = null; if(conn != null) { Statement query; try { query = conn.createStatement(); String sql = "SELECT data_x FROM table_x"; ResultSet result = query.executeQuery(sql); result.next(); int count = result.getInt("data_x"); result.close(); data_array = new String[count]; for (int x = 1; x <= count; x++) { String data_x = result.getString(x); data_x_array[x] = data_x; } } catch (SQLException e) { e.printStackTrace(); } } return data_x_array; } 

When I just compile:

 Invalid value for getInt() - 'value_in_table' 

Does anyone know this?

Hello!

+6
source share
5 answers

Another thing everyone forgot to mention

 String[] data_array = new String[999]; for (int x = 0; x <= 999; x++){} 

will throw an ArrayIndexOutOfBoundsException. Possible Solution

 String[] data_array = new String[999]; for (int x = 0; x < 999; x++){} 
+6
source

You defined your variable inside the while , that is, it does not appear in the return . In addition, you defined your method as static void , which means that the expected return value is not expected. Use static String [] instead.

+3
source

Your String[] declaration is not in the same scope as the return statement.

You need to declare it at the beginning of the field.

And you need to change the function title:

 public static String[] get_data() 
+2
source

Since it is not available outside the while loop, compilation complains about the same thing. Try the following:

 public static String[] get_data() { conn = getInstance(); String[] data_array = null; if(conn != null) { Statement query; try { query = conn.createStatement(); String sql = "SELECT data_x FROM table_x"; ResultSet result = query.executeQuery(sql); while (result.next()) { String data_x = result.getString("data_x"); data_array = new String[999]; for (int x = 0; x <= 999; x++) { data_array[x] = data_x; } } } catch (SQLException e) { e.printStackTrace(); } } return data_array; } 
+2
source

Two things can be fixed here:

 public static void get_data() 

This method is declared as returnable. Change it to:

 public static String[] get_data() 

Your String[] data_array declared in a while loop, so it is only known there. Your return statement is outside of this loop, so it does not have access to it.

Move the variable outside the loop:

 String sql = "SELECT data_x FROM table_x"; ResultSet result = query.executeQuery(sql); String[] data_array = new String[999]; 

Remember that you need to move the declaration and initialization outside the while , otherwise you will overwrite the previously saved data of this array, initializing it again. And also remember that your for loop will overwrite the current data anyway ... you should consider storing the string data in another array so that it is lost.

+1
source

Source: https://habr.com/ru/post/976944/


All Articles