简体   繁体   English

使用JComboBox从MySQL数据库动态选择信息

[英]Dynamically selecting information from a MySQL database using a JComboBox

It was a little difficult to explain the problem properly in the title, but this is what I am struggling with; 在标题中正确解释该问题有些困难,但这就是我正在努力的目标;

I am making a program that will hopefully predict the result between two football teams using their previous results. 我正在编写一个程序,希望可以使用他们以前的结果预测两个足球队之间的结果。 The results will be stored in a mysql database. 结果将存储在mysql数据库中。 There are 21 tables in the database, 1 is the results of all matches in the past 3 years, and the other 20 are for each team containing various information of that team. 数据库中有21张表,其中1张是过去3年中所有比赛的结果,另外20张是针对每个球队的,其中包含该球队的各种信息。

My problem is connecting to the table which contains results of all matches using two jcombobox and retrieving information for matches between two specific teams. 我的问题是连接到包含使用两个jcombobox的所有比赛的结果的表,并检索两个特定团队之间的比赛信息。

For example, this is the code for the two jcombobox. 例如,这是两个jcombobox的代码。

private String[] homeTeam = { "Arsenal", "Aston Villa", "Cardiff", "Chelsea", "Crystal Palace", "Everton", "Fulham",
"Hull", "Liverpool", "Manchester City", "Manchester United", "Newcastle", "Norwich", "Southampton", "Stoke", "Sunderland",
"Swansea", "Tottenham", "West Brom", "West Ham"};                                                             
private JComboBox box1 = new JComboBox(homeTeam);

private String[] awayTeam = { "Arsenal", "Aston Villa", "Cardiff", "Chelsea", "Crystal Palace", "Everton", "Fulham",
"Hull", "Liverpool", "Manchester City", "Manchester United", "Newcastle", "Norwich", "Southampton", "Stoke", "Sunderland",
"Swansea", "Tottenham", "West Brom", "West Ham" };                                                             
private JComboBox box2 = new JComboBox(awayTeam);

So, say I want to find the results if the home team was Cardiff, and the away team was Hull, I know what sql query I would need to write to do that. 因此,如果主队是加的夫,而客队是赫尔,我想找到结果,我知道我需要写什么样的SQL查询来做到这一点。 But my problem is there are so many different possible combinations of teams that I would end up needing to write hundreds of queries. 但是我的问题是团队的组合可能太多,最终我需要编写数百个查询。

Is it possible to do this dynamically, rather than writing a query for every possible combination of match? 是否可以动态地执行此操作,而不是针对匹配的每种可能组合编写查询?

Thanks 谢谢

Use a PreparedStatement. 使用PreparedStatement。 You have to write one Statement and the values can be set dynamically out of the JComboBoxes. 您必须编写一个Statement,并且可以在JComboBoxes中动态设置值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM