【问题标题】:How to insert selected items in multiple JCombobox into database mysql? (Java)如何将多个JCombobox中的选定项目插入数据库mysql? (爪哇)
【发布时间】:2015-01-19 11:37:48
【问题描述】:

日期:(日组合框)(月组合框)(年组合框)

我有这三个组合框,我想获取所选项目并将其存储到字符串日期中;存储到数据库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);

【问题讨论】:

    标签: java mysql database swing jcombobox


    【解决方案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 格式的日期

    【讨论】:

      【解决方案2】:

      使用它来初始化你的连击:

          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

      【讨论】:

        【解决方案3】:

        代替使用 JComboBox 选择 Dates ,更好的解决方案是使用 JXDatePicker,它在 Swing 编程中提供日期选择器

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

        【讨论】:

        • 我尝试了一些日期选择器,但我无法将日期选择器的结果存储到数据库中 >_
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多