如何从数据库获取日期值,然后将其设置为字符串。谁能告诉我下面的代码为什么不能正常工作?这是给麻烦的部分:
// Date d = rs.getDate("PatientBirthDate");
// DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
// String date = df.format(d);
while (rs.next()) {
pid.setText(String.valueOf(rs.getInt("PatientID")));
ssn.setText(rs.getString("PatientSSN"));
firstname.setText(rs.getString("PatientFname"));
lastname.setText(rs.getString("PatientLname"));
gender.setText(rs.getString("PatientGender"));
// dob.setText(date);
streetnum.setText(rs.getString("PatientStreetNo"));
streetname.setText(rs.getString("PatientStreetName"));
city.setText(rs.getString("PatientCity"));
state.setText(rs.getString("PatientState"));
zip.setText(rs.getString("PatientZip"));
homephone.setText(rs.getString("PatientHomePhone"));
cellphone.setText(rs.getString("PatientCellPhone"));
email.setText(rs.getString("PatientEmailAddress"));
inspolicy.setText(rs.getString("InsurancePolicyNumber"));
inscompany.setText(String.valueOf(rs.getInt("InsuranceCompID")));
}
最佳答案
为什么要在循环之外读取日期?
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
while (rs.next()) {
pid.setText(String.valueOf(rs.getInt("PatientID")));
ssn.setText(rs.getString("PatientSSN"));
firstname.setText(rs.getString("PatientFname"));
lastname.setText(rs.getString("PatientLname"));
gender.setText(rs.getString("PatientGender"));
Date d = rs.getDate("PatientBirthDate");
String date = df.format(d);
dob.setText(date);
streetnum.setText(rs.getString("PatientStreetNo"));
streetname.setText(rs.getString("PatientStreetName"));
city.setText(rs.getString("PatientCity"));
state.setText(rs.getString("PatientState"));
zip.setText(rs.getString("PatientZip"));
homephone.setText(rs.getString("PatientHomePhone"));
cellphone.setText(rs.getString("PatientCellPhone"));
email.setText(rs.getString("PatientEmailAddress"));
inspolicy.setText(rs.getString("InsurancePolicyNumber"));
inscompany.setText(String.valueOf(rs.getInt("InsuranceCompID")));
}