简体   繁体   中英

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

Use this to initialize your combos:

    cDay.removeAllItems();
    cMonth.removeAllItems();
    cYear.removeAllItems();

    cDay.addItem("Day");
    cMonth.addItem("Month");
    cYear.addItem("Year");

    for (int i = 1; i <= 31; i++) {
        cDay.addItem(i + "");
    }

    for (int i = 1; i <= 12; i++) {
        cMonth.addItem(i + "");
    }

    for (int i = 1975; i <= 2015; i++) {
        cYear.addItem(i + "");
    }

Then, to set the values:

updateTo("19#1#41"); // index of values in combos

Where

private void updateTo(String data) {
    String[] list = data.split("#");
    cDay.setSelectedIndex(Integer.parseInt(list[0]));
    cMonth.setSelectedIndex(Integer.parseInt(list[1]));
    cYear.setSelectedIndex(Integer.parseInt(list[2]));
}

to get the values:

private String getvalue() {
    return cDay.getSelectedIndex() + "#"
            + cMonth.getSelectedIndex() + "#"
            + cYear.getSelectedIndex();
}

Storing these data needs you to learn and work with mysql, or simply SQLite which it is easy to use.

This link is really useful: http://www.tutorialspoint.com/sqlite/sqlite_java.htm

Instead of Using JComboBox for selecting Dates , A better solution is to use JXDatePicker which is providing datepickers in Swing Programming

    import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jdesktop.swingx.JXDatePicker;

    @SuppressWarnings("serial")
    public class DatePickerExample extends JPanel {

        public static void main(String[] args) {
            JFrame frame = new JFrame("JXPicker Example");
            JPanel panel = new JPanel();

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setBounds(400, 400, 250, 100);


            JXDatePicker picker = new JXDatePicker();
            picker.setDate(Calendar.getInstance().getTime());
            picker.setFormats(new SimpleDateFormat("dd.MM.yyyy"));

            panel.add(picker);
            frame.getContentPane().add(panel);

            frame.setVisible(true);
        }
    }

No need to worry about selecting text from diffrent comboboxes , and will also give a better UI to Your Application

Note : Either use Maven Dependency for swingx jar or You can download it from here : http://www.java2s.com/Code/Jar/s/Downloadswingx094jar.htm

Edit : As Requested How to get the selected date from DatePicker

JFormattedTextField editor = picker.getEditor();
Date dateInDatePicker = (Date) editor.getValue();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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