从JComboBox获取输入并将其放入预备语句中

时间:2014-02-13 11:16:01

标签: java mysql prepared-statement jcombobox

我想要做的是使用jcombobox从mysql表中打印信息以选择信息。

以下是jcombobox的代码

private static String[] homeTeam = { "Arsenal", "Aston Villa", "Cardiff", "Chelsea"};                                            
private static JComboBox box1 = new JComboBox(homeTeam);
private static String[] awayTeam = { "Arsenal", "Aston Villa", "Cardiff", "Chelsea"};                                                             
private static JComboBox box2 = new JComboBox(awayTeam);

这是预备陈述的代码

try 
    {
        //Sets up the connedtion to the database and installs drivers which are required.

        Class.forName("com.mysql.jdbc.Driver");                                                                        

        con = DriverManager.getConnection("jdbc:mysql://localhost", "username", "password");        


        String BoxSelectedItem1 = box1.getSelectedItem().toString();
        String BoxSelectedItem2 = box2.getSelectedItem().toString();

        String SQL = "SELECT * FROM PreviousSeasons WHERE HomeTeam=? and AwayTeam=?";
        PreparedStatement prepst;            

        prepst =  con.prepareStatement(SQL);
        prepst.setString(1,BoxSelectedItem1);
        prepst.setString(2,BoxSelectedItem2);
        rs = prepst.executeQuery();

        while (rs.next()) 
        {

            hometeam = rs.getString("HomeTeam");
            awayteam = rs.getString("AwayTeam");                    
            result = rs.getString("Result");                    


            custs = (hometeam + "," + awayteam + "," + result);     
            SeasonResults add = SeasonResults.add(hometeam, awayteam, result);
        }

    }

    catch (Exception e) 
    {
        System.out.println("Error " +e);                                                                               
    }

这是jbutton的代码,我尝试打印出结果

public void actionPerformed(ActionEvent e)
{           
        if("Predict".equals(e.getActionCommand())) {

        JOptionPane.showMessageDialog(null,result);
            //System.out.println(result);

}
}

“预测”是jbutton的名称。我有另一个jbutton重置jcombobox,这是有效的,所以我知道jbuttons的actionlisteners没有任何问题。

当我按下按钮时,我没有收到回复。有谁知道问题是什么?

0 个答案:

没有答案
相关问题