如何将JComboBox事件处理程序的String放入变量中进行查询



在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的GetRoute()。

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

此处:

String go = Cashier.GetRoute();

这个方法不是静态的,不能用这种方式调用它。无论如何,这是一个糟糕的设计选择。考虑为DBaccess类提供所需路由的setter。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。请参阅多个JFrame的使用,良好/不良做法?

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

  • 您尝试执行的查询易受SQL注入攻击。请查看PreparedStatement以避免出现这种情况。

  • 如果DBaccess类将使用Swing组件显示查询结果,那么您可能需要查看SwingWorker类,以便在后台线程中执行数据库调用,并在事件调度线程中更新Swing组件。查看Swing trail中的并发以了解更多详细信息。

将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 将使用这种方法

public字符串GetRoute(){

return d;                    
}
//More Objects...

}}

最新更新