Java 从数据库中获取日期值,然后设置为字符串
如何从数据库中获取日期值,然后将其设置为字符串。有人能告诉我为什么下面的代码不能正常工作吗?这是给人带来麻烦的部分:Java 从数据库中获取日期值,然后设置为字符串,java,mysql,string,date,javafx,Java,Mysql,String,Date,Javafx,如何从数据库中获取日期值,然后将其设置为字符串。有人能告诉我为什么下面的代码不能正常工作吗?这是给人带来麻烦的部分: // 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.getIn
// 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")));
}
您是否遇到任何异常、编译错误等。问题是什么?什么是“工作不正常”?
date
是有效的日期/时间列还是文本列?你能从数据库中获取java.sql.Date
或java.sql.Timestamp
值吗?你得到了什么错误?我希望你调用的是rs.getDate(“PatientBirthDate”)代码>在while(rs.next)
循环中…需要代码听起来很糟糕!