【问题标题】:How to take String of a JComboBox Event handler into a variable for query?如何将 JComboBox 事件处理程序的字符串放入变量中进行查询?
【发布时间】:2014-01-11 18:43:27
【问题描述】:

在 JCombobox 中有可能的火车路线。我想根据事件处理程序获取 JCombobox 的值,然后在使用数据库的类中使用它。这个值将是一个Mysql查询的参数,我已经获取成功了,但是不能使用。我在java方面不是很有经验,我做错了什么。我在网站上搜索过类似的问题,看到了但没有清楚地理解它们。我错过了什么?

//imports...

public class Cashier extends JFrame{

      //here is main...
 public Cashier(){

      //some Window code...

     final  String[] Routes = {"--Select--", "value1",....."valueN" };
     final JComboBox comboBox = new JComboBox(Routes);

     comboBox.addActionListener(new ActionListener() {
       /*-->*/ public String d; 
            public void WhereTo(String dest){

                 this.d=dest;                 
                   System.out.println(d); 

          // comes out correct!           
      /*I want d for use in DBaccess class as query parameter, by invoking 
        GetRoute()*/         
}            

         public void actionPerformed(ActionEvent e) {
                 int val = comboBox.getSelectedIndex();
                         this.d= Routes[val];    
              WhereTo(d);

        }         
     });
            comboBox.setBounds(165, 124, 130, 23);     
    contentPane.add(comboBox);

   //this method will be used by DBaccess

   public String GetRoute(){

     return d;                    
    }

    //More Objects...
  }
}

这是我的 DBaccess 类,我想在其中使用字符串 d,可能是通过调用 Cashier 的 Get Route()。

 public DBaccess extends Cashier{

  //connection code....

 // Executing the query
  System.out.println("Creating statement...");
  stmt = conn.createStatement();
  String sql; 

 //probably like this...   
 String go = Cashier.GetRoute();

  sql = "SELECT FROM reservations WHERE destination='"+go+"'";
  ResultSet rs = stmt.executeQuery(sql);
  }

【问题讨论】:

  • 请尝试描述您的确切问题。你的代码编译了吗?它运行吗?它会产生结果吗?此结果与您的预期不同吗?
  • neoprez ---我如你所说的那样声明,但也必须使其成为静态,包括 GetRoute。是的,它现在编译!但是在 DBaccess 中该值为 null 。无法查询!
  • 总而言之,我正在尝试将 d 传递给 DBaccess 以进行查询
  • Java GUI 可能必须在多个平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于组件的精确放置。要为强大的 GUI 组织组件,请改用布局管理器或 combinations of them,以及 white space 的布局填充和边框。

标签: java swing jdbc jcombobox


【解决方案1】:

这里:

String go = Cashier.GetRoute();

此方法不是静态的,不能以这种方式调用。无论如何,这是一个糟糕的设计选择。考虑为DBaccess 类提供所需路由的设置器。 actionPerformed() 实现应该如下所示:

@override
public void actionPerformed(ActionEvent e) {
    JComboBox comboBox = (JComboBox)e.getSource();
    String selectedRoute = (String)comboBox.getSelectedItem();
    DBaccess dbAccess = new DBaccess();
    dbAccess.setRoute(selectedRoute);
    dbAccess.setVisible(true);
}

一些可以帮助您的提示:

  • Cashier extends JFrame:如果您不添加一些 Swing 相关功能,请不要从 Swing 组件扩展。您可以改用简单的变量。

  • DBaccess extends Cashier(从 JFrame 扩展而来):典型的 Swing 应用程序应该只有一个 JFrame。您应该改用JDialog。见The Use of Multiple JFrames, Good/Bad Practice?

  • DBaccess 类提供一个方法来从另一个类(Cashier)设置所需的路由。

  • 您尝试执行的查询容易受到SQL Injection 攻击。请查看PreparedStatement 以避免这种情况。

  • 如果 DBaccess 类将使用 Swing 组件显示查询结果,那么您可能需要查看 SwingWorker 类在后台线程中进行数据库调用并更新 @987654327 @。查看Concurrency in Swing trail 了解更多详情。

【讨论】:

    【解决方案2】:

    使 d 成为全局类变量。这么说我的意思是,在类名之后声明它。

    public class Cashier extends JFrame{
    private String d;
      //here is main...
     public Cashier(){
    //some Window code...
    
     final  String[] Routes = {"--Select--", "value1",....."valueN" };
     final JComboBox comboBox = new JComboBox(Routes);
    
     comboBox.addActionListener(new ActionListener() {
       /*-->*/ public String d; 
            public void WhereTo(String dest){
    
                 Cashier.this.d=dest;   //to change d value              
                   System.out.println(d); 
    
          // comes out correct!           
      /*I want d for use in DBaccess class as query parameter, by invoking 
        GetRoute()*/         
    

    }

         public void actionPerformed(ActionEvent e) {
                 int val = comboBox.getSelectedIndex();
                         Cashier.this.d= Routes[val];   //to change d value 
              WhereTo(d);
    
        }         
     });
            comboBox.setBounds(165, 124, 130, 23);     
    contentPane.add(comboBox);
    

    //这个方法会被DBaccess使用

    公共字符串GetRoute(){

     return d;                    
    }
    
    //More Objects...
    

    } }

    【讨论】:

    • 必须将其更改为 (private static d) 并将方法更改为 (private static String GetRoute()) ,好的。但是后来我在 ActionListener 中声明了 d 和静态的,必须以某种方式将它们连接起来......
    • 我正在尝试找到将 d 从一个类传递到另一个类的方法,将 Cashier 传递给 DBaccess 以进行查询。
    猜你喜欢
    • 1970-01-01
    • 2018-06-16
    • 2013-06-01
    • 2019-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    相关资源
    最近更新 更多