Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从数据库中获取日期值,然后设置为字符串_Java_Mysql_String_Date_Javafx - Fatal编程技术网

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)
循环中…需要代码听起来很糟糕!