I'd like to ask you for help. At my current Java programming skills, that are rather basic this problem is magic for me. Let me explain first what was my intention: I wanted to load data from database to object tab called airportList, and this is tab of Airport objects initialized here:
public void init()
{
System.out.println("Applet inicialization ...");
mysqllink = new MySQLlink();
try {
mysqllink.getConnection();
} catch (SQLException e) {
System.out.println("Not connected!");
e.printStackTrace();
}
allAirportAmount = mysqllink.getNumerOfRows(tabela_lotnisk);
allAirlineAmount = mysqllink.getNumerOfRows(tabela_linii);
allAirplanesAmount = mysqllink.getNumerOfRows(tabela_samolotow);
allConnectionsAmount = mysqllink.getNumerOfRows(tabela_tras);
airportList = new Airport[allAirportAmount]; //Airport object tab
airportList2 = new String[allAirportAmount]; //as a test i put only Airport name from database over here to String tab
airlineList = new String[allAirlineAmount];
airplaneList = new String[allAirplanesAmount];
FlightConnectionFrame frame = new FlightConnectionFrame();
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(frameWidth,frameHeight);
frame.setVisible(true);
add(frame);
}
public static MySQLlink mysqllink = new MySQLlink();
public static int allAirportAmount;
public static int allAirlineAmount;
public static int allAirplanesAmount;
public static int allConnectionsAmount;
public static Airport airportList[]; // declaration over here
public static String airportList2[]; // and here as a test
public static String airlineList[];
public static String airplaneList[];
Ok, and as you can see I have 2x tables, the airportList of Airport objects and airportList2 of String type. In the next piece of code I load data from database into those two tabs at the same time:
public void viewAirportList()
{
String query = "select id_lotniska, nazwa, lokalizacja, oplaty_lotniskowe from " + databaseName + "." + tabela_lotnisk;
Statement stmt = null;
try {
stmt = databaseConnection.createStatement();
ResultSet rs = stmt.executeQuery(query);
int j = 0;
while (rs.next()) {
int id_l = rs.getInt("id_lotniska");
String name = rs.getString("nazwa");
String loc = rs.getString("lokalizacja");
double tax = rs.getDouble("oplaty_lotniskowe");
flight_connections.FCApletCore.airportList[j] = new Airport(id_l, name, loc, tax); // this is where the Airport object is created and put into the AirportList tab
flight_connections.FCApletCore.airportList2[j] = name; // here, as a test - i put only String value of name to AirportList2 tab
j++;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (stmt != null) { try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
This is how Airport class looks like:
public class Airport {
public Airport(int i, String n, String l, double o)
{
airport_id = i;
airport_name = n;
airport_localization = l;
airport_tax = o;
System.out.println(" > new object created:" + airport_name);
}
public int getAirportId(){return airport_id;}
public String getAirportName(){return airport_name;}
public String getAirportLocalization(){return airport_localization;}
public double getAirportTax(){return airport_tax;}
public static int airport_id;
public static String airport_name;
public static String airport_localization;
public static double airport_tax;
}
And using 2x FOR's to get what's inside of both tabs in console:
System.out.println("*** Airport objects tab: airportList ***");
for (int i=0; i<flight_connections.FCApletCore.allAirportAmount; i++)
{
System.out.println("airportList[" + i + "]: " + flight_connections.FCApletCore.airportList[i].getAirportName());
}
System.out.println("*** String values tab: airportList2 ***");
for (int i=0; i<flight_connections.FCApletCore.allAirportAmount; i++)
{
System.out.println("airportList2[" + i + "]: " + flight_connections.FCApletCore.airportList2[i]);
}
result is:
Applet inicialization ...
Connecting database...
Database connected!
loading number of records from database table: lotniska ...
loading number of records from database table: linie ...
loading number of records from database table: samoloty ...
loading number of records from database table: trasy ...
> new object created:Port Lotniczy Balice
> new object created:Port lotniczy Paryż Charles de Gaulle
> new object created:Port Lotniczy Heathrow
> new object created:Port Lotniczy O'Hare
> new object created:Port Lotniczy Dubaj
> new object created:Port lotniczy Berlin-Brandenburg
> new object created:Port lotniczy San Francisco
> new object created:Port lotniczy Tokio-Haneda
*** Airport objects tab: airportList ***
airportList[0]: Port lotniczy Tokio-Haneda
airportList[1]: Port lotniczy Tokio-Haneda
airportList[2]: Port lotniczy Tokio-Haneda
airportList[3]: Port lotniczy Tokio-Haneda
airportList[4]: Port lotniczy Tokio-Haneda
airportList[5]: Port lotniczy Tokio-Haneda
airportList[6]: Port lotniczy Tokio-Haneda
airportList[7]: Port lotniczy Tokio-Haneda
*** String values tab: airportList2 ***
airportList2[0]: Port Lotniczy Balice
airportList2[1]: Port lotniczy Paryż Charles de Gaulle
airportList2[2]: Port Lotniczy Heathrow
airportList2[3]: Port Lotniczy O'Hare
airportList2[4]: Port Lotniczy Dubaj
airportList2[5]: Port lotniczy Berlin-Brandenburg
airportList2[6]: Port lotniczy San Francisco
airportList2[7]: Port lotniczy Tokio-Haneda
And if you take a look at this, the String tab is OK, and the Airport tab is not since it's loaded only with one - the last of the records from database and both of the tabs are loaded in the two lines of code next to each other, I do not have any code that can somehow change the values of the first tab. To test if the Airport objects are created correctly I added System.out.println
with name of object created, and that you can see in results as a correct values. I'll add that I'm running this as the applet, what init()
can suggest.
I changed code a little bit to English names but the database values and tables names are Polish, like in the console you can see "Port lotniczy" what means Airport.
All your fields are static, instead of being instance fields. Don't make them static, sice they should be different for each instances of the class.
Using public fields is also not advised. Using arrays instead of collections is also a bad idea. You should learn more about static fields, collections and encapsulation before using Swing and JDBC.