如何将多个JCombobox中的选定项插入数据库mysql? (JAVA)

时间:2015-01-19 11:37:48

标签: java mysql database swing jcombobox

日期:(组合框一天)(组合框一个月)(组合框一年)

我有这三个组合框,我想获取所选项目并将其存储到字符串日期;存储到数据库mysql中。 我不太擅长编码,我希望有人可以帮助我.. 谢谢! : - )

我的代码:

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

3 个答案:

答案 0 :(得分:1)

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

现在您拥有字符串格式或java.util.Date格式的日期

答案 1 :(得分:0)

用它来初始化你的组合:

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

然后,设置值:

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

其中

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

获取值:

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

存储这些数据需要您学习和使用mysql,或者只是简单易用的SQLite。

此链接非常有用:http://www.tutorialspoint.com/sqlite/sqlite_java.htm

答案 2 :(得分:0)

使用 JXDatePicker 代替使用JComboBox选择日期,而不是在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);
        }
    }

无需担心从不同的组合框中选择文本,也可以为您的应用程序提供更好的UI

注意:使用 swingx jar 的Maven依赖项 或者你可以从这里下载: http://www.java2s.com/Code/Jar/s/Downloadswingx094jar.htm

修改:按要求如何从DatePicker获取所选日期

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