我的 Java 应用程序有一点问题。我想在左侧 Jlist 中显示餐厅的订单,在右侧 Jlist 中显示所选订单的详细信息。订单通过 MySQL 查询读入,然后打印到 Jlist 中。但是,我们想要创建一个刷新按钮,该按钮必须再次执行查询,然后打印到 Jlist。但是,我无法使刷新按钮起作用。它确实刷新了内容,但会打开一个新的 Jfram 窗口......
来自 GUI 类:
public class Barscherm extends javax.swing.JFrame {
private BedieningsManager manager;
/**
* Creates new form Barscherm
*/
public Barscherm() {
manager = new BedieningsManager();
initComponents();
}
private void opvraagKnopActionPerformed(java.awt.event.ActionEvent evt) { // This is the refresh button
jPanel1.removeAll();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Barscherm().setVisible(true);
}
});
}
从经理类:
public class BedieningsManager {
private ArrayList<Barbestelling> bestellingen;
private int tafelNummer,bestelNummer, aantal;
Database db1 = new Database();
public BedieningsManager(){
this.bestellingen = new ArrayList<Barbestelling>();
maakBestellingen();
public void maakBestellingen(){
aantal = db1.getAantalRijenBarBestelling();
Barbestelling[] bss = new Barbestelling[aantal];
for (int i = 0; i < aantal; i++){
tafelNummer = db1.getTafelnummer(i);
bestelNummer = db1.getBestelNummer(i);
bss[i] = new Barbestelling(bestelNummer,tafelNummer,"Geplaatst");
bestellingen.add(bss[i]);
}
}
public ArrayList<String> toonBarbestellingen()
{
ArrayList<String> tafelNummers = new ArrayList<String>();
for(Barbestelling i : bestellingen)
{
tafelNummers.add(i.getTafelNummer());
}
return tafelNummers;
}
public ArrayList<Barbestelling> getBestellingen()
{
return bestellingen;
}
public String getDetails(String barbestelling){
String details = "";
boolean found = false;
int count = 0;
while(!found && count < bestellingen.size())
{
if(bestellingen.get(count).getTafelNummer().equals(barbestelling))
{
found = true;
details = bestellingen.get(count).getDetails();
}
else
{
count ++;
}
}
if(!found)
{
System.out.println("Bestelling met Tafelnummer: " + barbestelling + " bestaat niet!");
}
return details;
}
}
数据库查询的:
public class Database {
private Connection con;
private int bestelNummer;
public Database() {
String connectiestring;
connectiestring = "jdbc:mysql://localhost:3306/hartigehap";
try {
con = DriverManager.getConnection(connectiestring,"root", "");
}
catch(Exception e) {
System.err.println("Melding: " + e.getMessage());
}
}
public int getAantalRijenBestelRegel(int bestelNummer){
Statement stmt;
ResultSet rs;
String query;
int i = 0;
try {
stmt = con.createStatement();
query = "SELECT COUNT(ItemItemID) FROM bestelregel WHERE barbestellingbestellingnummer = '"+bestelNummer+"'";
rs = stmt.executeQuery(query);
while (rs.next()){
i = rs.getInt("COUNT(ItemItemID)");
}
}
catch(Exception e) {
System.err.println("Melding1: " + e.getMessage());
}
return i ;
}
public int getAantalRijenBarBestelling(){
Statement stmt;
ResultSet rs;
String query;
int i = 0;
try {
stmt = con.createStatement();
query = "SELECT BestellingNummer FROM barbestelling WHERE Status = 'Geplaatst'";
rs = stmt.executeQuery(query);
while ( rs.next() ) {
i++;
}
stmt.close();
}
catch(Exception e) {
System.err.println("Melding1: " + e.getMessage());
}
return i ;
}
public String getProduct(int bestelNummer){
Statement stmt;
ResultSet rs;
String query;
String productNaam = "";
try {
stmt = con.createStatement();
query = "SELECT Naam FROM item, bestelregel WHERE bestelregel.barbestellingbestellingnummer = "+bestelNummer+" AND item.ItemID = bestelregel.ItemItemID";
rs = stmt.executeQuery(query);
rs.absolute(bestelNummer);
productNaam = rs.getString("Naam");
}
catch(Exception e) {
System.err.println("Melding1: " + e.getMessage());
}
return productNaam;
}
public int getTafelnummer(int bestelNummer){
Statement stmt;
ResultSet rs;
String query;
int tafelNummer = 0;
bestelNummer++;
try {
stmt = con.createStatement();
query = "SELECT barbestelling.TafelNummer FROM barbestelling WHERE barbestelling.BestellingNummer = "+bestelNummer+"";
rs = stmt.executeQuery(query);
rs.absolute(1);
tafelNummer = rs.getInt("Tafelnummer");
}
catch(Exception e) {
System.err.println("Melding1: " + e.getMessage());
}
return tafelNummer;
}
public int getAantal(int bestelNummer){
Statement stmt;
ResultSet rs;
String query;
int aantal = 0;
try {
stmt = con.createStatement();
query = "SELECT Aantal FROM bestelregel WHERE barbestellingbestellingnummer = '"+bestelNummer+"'";
rs = stmt.executeQuery(query);
rs.absolute(bestelNummer);
aantal = rs.getInt("Aantal");
}
catch(Exception e) {
System.err.println("Melding1: " + e.getMessage());
}
return aantal;
}
public String getBestelRegel(int bestelNummer) {
Statement stmt;
ResultSet rs;
String query, querytekst1;
String details = "";
try {
stmt = con.createStatement();
query = "SELECT bestelregel.Aantal, item.Naam FROM bestelregel, item WHERE bestelregel.barbestellingbestellingnummer = " + bestelNummer + " AND item.ItemID = bestelregel.ItemItemID";
rs = stmt.executeQuery(query);
while(rs.next()){
String aantal = rs.getString("Aantal");
String naam = rs.getString("Naam");
details += aantal + " " + naam + "\n";
}
}
catch(Exception e) {
System.err.println("Melding1: " + e.getMessage());
}
return details;
}
public int getBestelNummer(int bestelNummer) {
Statement stmt;
ResultSet rs;
String query;
bestelNummer++;
try {
stmt = con.createStatement();
query = "SELECT BestellingNummer FROM barbestelling WHERE BestellingNummer = '"+bestelNummer+"' AND Status = 'Geplaatst'";
rs = stmt.executeQuery(query);
rs.absolute(1);
bestelNummer = rs.getInt("BestellingNummer");
}
catch(Exception e) {
System.err.println("Melding1: " + e.getMessage());
}
return bestelNummer;
}
private boolean sluitConnectie() {
boolean x = false;
try {
if(con != null) {
con.close();
x = true;
}
} catch(SQLException e) {
x = false;
}
return x;
}
}
图形用户界面截图: