Search code examples
javamysqldatabaseswingjcombobox

How to insert selected items in multiple JCombobox into database mysql? (Java)


Date: (combobox for day) (combobox for month) (combobox for year)

I've got these three combo boxes and i want to get the selected item and store it into String date; to store into database mysql. I'm not very good at coding and i hope someone could help me.. Thanks! :-)

my code:

JPanel panelDay = new JPanel();
panelDay.setBounds(224, 149, 56, 30);
panelStep2.add(panelDay);
panelDay.setBackground(new Color(224, 255, 255));
panelDay.setLayout(new BoxLayout(panelDay, BoxLayout.X_AXIS));

String[] dayStrings = {"31", "30", "29", "28", "27", "26", "25", "24", 
    "23", "22", "21", "20", "19", "18", "17", "16", "15", "14", "13",
    "12", "11", "10", "9","8", "7", "6","5", "4", "3", "2", "1", "Day" };

final JComboBox dayList = new JComboBox(dayStrings);
panelDay.add(dayList);
dayList.setSelectedIndex(31);   
dayList.setPreferredSize(new Dimension(200,130));
dayList.setVisible(true);           

JPanel panelMonth = new JPanel();
panelMonth.setBounds(292, 149, 70, 30);
panelStep2.add(panelMonth);
panelMonth.setBackground(new Color(224, 255, 255));
panelMonth.setLayout(new BoxLayout(panelMonth, BoxLayout.X_AXIS));

String[] monthStrings = {"12", "11", "10", "09",
    "08", "07", "06", "05", "04", "03", "02", "01", "Month" };

final JComboBox monthList = new JComboBox(monthStrings);
panelMonth.add(monthList);
monthList.setSelectedIndex(12); 
monthList.setPreferredSize(new Dimension(200,130));
monthList.setVisible(true);

JPanel panelYear = new JPanel();
panelYear.setBounds(375, 149, 70, 30);
panelStep2.add(panelYear);
panelYear.setBackground(new Color(224, 255, 255));
panelYear.setLayout(new BoxLayout(panelYear, BoxLayout.X_AXIS));

String[] yearStrings = {"1975", "1976", "1977", "1978", 
                                    "1979", "1980", 
                                    "1981", "1982",
                                    "1983", "1984", 
                                    "1985", "1986", 
                                    "1987", "1988", 
                                    "1989", "1990", 
                                    "1991", "1992",
                                    "1993", "1994", 
                                    "1995", "1996", 
                                    "1997", "1998", 
                                    "1999", "2000",
                                    "2001", "2002",
                                    "2003", "2004",
                                    "2005", "2006", 
                                    "2007", "2008",
                                    "2009", "2010", 
                                    "2011", "2012",
                                    "2013", "2014", 
                                    "2015", "Year" };

final JComboBox yearList = new JComboBox(yearStrings);
panelYear.add(yearList);
yearList.setSelectedIndex(12);  
yearList.setPreferredSize(new Dimension(200,130));
yearList.setVisible(true);

Solution

  • String day = dayList.getSelectedItem().toString();
    String month = monthList.getSelectedItem().toString();
    String year = yearList.getSelectedItem().toString();
    
    if (!day.equals("Day") && !month.equals("Month") && !year.equals("Year")) {
         String dateAsString = day + "/" + month + "/" + year;
         SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
         try {
             Date date = sdf.parse(dateAsString);
         } catch (ParseException e1) {}
    }
    

    You now have the date in a string format or in a java.util.Date format