netbeans:如何设置值来控制从数据库中检索的内容?

时间:2012-08-03 09:44:48

标签: java netbeans jdatechooser

我想从数据库&获取记录想要在表单上设置。在此表单中,我使用JDateChooser中的JCalendar。我为此编写了以下代码:

public void showdata()

    int a=leaveView.getSelectedRow();
    int b=(Integer)leaveView.getValueAt(a, 0);
    String c=(String)leaveView.getValueAt(a, 1);
    String d=(String)leaveView.getValueAt(a, 2);
    String e=(String)leaveView.getValueAt(a, 3);
    String f=(String)leaveView.getValueAt(a, 4);
    String  g=(String)leaveView.getValueAt(a, 5);
    String h=(String)leaveView.getValueAt(a, 6);
    int i=(Integer)leaveView.getValueAt(a, 7);  
    String j = (String)leaveView.getValueAt(a, 8);
    String k = (String)leaveView.getValueAt(a, 9);


    AL.empid.setSelectedItem(b);
    AL.empname.setText(c);
    AL.empname.setEditable(true);
    AL.department.setText(d);
    AL.department.setEditable(true);
    AL.leavetype.setSelectedItem(e);
    AL.other.setText(f);
    AL.other.setEditable(true);
    AL.jDateChooser1.setDate(g);
    AL.jDateChooser2.setDate(h);

    AL.noofdays.setText(""+i);
    AL.noofdays.setEditable(true);
    AL.singleday.setSelected(true);
    AL.multipleday.setSelected(true);
}

但它默认将今天的日期设置为JDateChooser ...它没有显示从数据库中检索的日期...上面的代码在行AL.jDateChooser1.setDate(g)和{{1}处抛出错误对于g&我...我该怎么办?

1 个答案:

答案 0 :(得分:0)

假设您正在使用此JCalendar API

javadocs setDate接受日期对象而不是字符串。首先将dateString(g& h)转换(解析)为Date对象,然后设置日期。

DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); //adjust this according to your requirement
Date gDate,hDate;
        try {
            gDate = df.parse(g);
            hDate = df.parse(h);
        } catch (ParseException e) {
            e.printStackTrace();
        }

AL.jDateChooser1.setDate(gDate);
AL.jDateChooser2.setDate(hDate);

对于某些示例日期格式,请访问http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html