I want to store a sql column into an array using a method in java
my code:
public static String[] getAirports() {
String airport[];
try {
String sql = "SELECT airportname FROM luggageApp.airport";
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DatabaseManager.openConnection();
Statement statement = connection.createStatement();
PreparedStatement pst = connection.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
ResultSetMetaData rsmetadata = rs.getMetaData();
int columns = rsmetadata.getColumnCount();
airport[] = new String[columns];
while (rs.next()) {
for (int j = 1; j < columns; j++) {
airport[j] = rs.getString(j);
}
}
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
return airport;
}
What I want: In a for loop I would like to add all the airport names 1 by 1 into the array so I can later on call this method to fill in a jCombobox.
columnvariable should be first defined than it should be used as it will give compile time error.