Java如何刷新jlist中的sql结果

Posted

技术标签:

【中文标题】Java如何刷新jlist中的sql结果【英文标题】:Java how to refresh sql outcome in jlist 【发布时间】:2013-05-04 08:56:39 【问题描述】:

我的 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;



图形用户界面截图:

http://img90.imageshack.us/img90/1676/schoolopdrachtgui.jpg

【问题讨论】:

从刷新按钮或数据库查询代码? 刷新按钮名为“opvraagKnop”(我们必须使用荷兰语名称进行编程),查询简单选择仍需交付的所有订单的产品。 我添加了查询,但不要介意混乱。我们必须这样做,因为我们得到的数据库有点搞砸了...... 如需尽快获得更好的帮助,请发帖SSCCE。 【参考方案1】:

使用JList.setModel 设置您自己的DefaultListModel 实例。它具有添加、更改和删除内容的方法,并且 JList 应该监视它并观察您的更改。

【讨论】:

您好,感谢您的回答,但我对 Java 还是很陌生。所以你介意解释一下吗?【参考方案2】:

看看你的 actionPerformed 方法

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

首先,从jPanel1 中删除所有活动组件,然后创建一个新的Barsdherm 窗口并使其可见。

应该发生的是您应该从数据库中加载您需要的所有值并刷新列表模型。

您的示例中没有足够的代码来 100% 确定确切的解决方案,但一种方法是将您需要的数据库中的所有值加载到新的 DefaultListModel 中,并将此模型应用于您的 JList

看看How to use lists的例子

【讨论】:

您想要更多代码来完成正在做的事情吗?因为我唯一的额外代码是来自 GUI 的代码。剩下的都在那里 事情就是这样。您显然已经从数据库中加载了一次数据。您需要做的只是在 actionPerformed 方法中复制流程

以上是关于Java如何刷新jlist中的sql结果的主要内容,如果未能解决你的问题,请参考以下文章

java和sql到JList

java将数组刷新到jList中

如何清除 Java 中的 JList?

如何在java中的某些事件上取消选择Jlist项目

java swing 中的列表框JList如何在程序中动态的添加和删除元素

将数据库表名添加到 java 中的 JList