自我覆盖对象选项卡

问题描述 投票:0回答:1

我想请你帮忙。根据我目前的Java编程技巧,这个问题相当基本,这个问题对我来说很神奇。让我先解释一下我的意图:我想将数据从数据库加载到名为airportList的对象选项卡,这是在这里初始化的Airport对象的选项卡:

 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[];

好的,正如你所看到的,我有2x表,机场对象的airportList和String类型的airportList2。在下一段代码中,我将数据从数据库同时加载到这两个选项卡中:

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();
        } 
       }
     }


}

这就是机场类的样子:

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;

}

并使用2x FOR来获取控制台中两个选项卡的内容:

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]);
    }

结果是:

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

如果你看看这个,String选项卡是OK,机场选项卡不是因为它只加载了一个 - 数据库中的最后一个记录,并且两个选项卡都加载在旁边的两行代码中彼此,我没有任何代码可以以某种方式更改第一个选项卡的值。要测试是否正确创建了Airport对象,我添加了带有创建对象名称的System.out.println,并且您可以在结果中看到正确的值。我将补充说,我正在运行它作为小程序,init()可以建议。

我将代码更改为英文名称,但数据库值和表名称是波兰语,就像在控制台中你可以看到“Port lotniczy”是什么意思机场。

java mysql object applet
1个回答
2
投票

您的所有字段都是静态的,而不是实例字段。不要让它们变得静止,对于每个类的实例,它们应该是不同的。

也不建议使用公共字段。使用数组而不是集合也是一个坏主意。在使用Swing和JDBC之前,您应该了解有关static fieldscollections和封装的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.