0
我有這個代碼,我正在使用從DOB計算年齡。但我不確定它是否正常工作,或者當我從另一個文件調用此函數時,我不是能夠看到任何輸出。計算從DOB提取的年齡從數據庫
public ArrayList<Integer> getEmployeeAge() {
// TODO Auto-generated method stub
ArrayList<Integer> employeeage = new ArrayList<Integer>();
ResultSet rs = null;
try {
LibraryConnection lc = new LibraryConnection(library, dbConnection);
rs = lc.executeQuery("GetEmployeeAge", null);
while(rs.next()){
/* QuartReport qcd = new QuartReport();
qcd.setYear(rs.getInt(1));
qcd.setQuarter(rs.getInt(2));
qcd.setCount(rs.getInt(3));
*/
String dob = rs.getString(1);
int yearDOB = Integer.parseInt(dob.substring(6, 10));
int monthDOB = Integer.parseInt(dob.substring(3, 5));
int dayDOB = Integer.parseInt(dob.substring(0, 2));
//CALCULATE THE CURRENT YEAR, MONTH AND DAY
//INTO SEPERATE VARIABLES
DateFormat dateFormat = new SimpleDateFormat("yyyy");
java.util.Date date = new java.util.Date();
int thisYear = Integer.parseInt(dateFormat.format(date));
dateFormat = new SimpleDateFormat("MM");
date = new java.util.Date();
int thisMonth = Integer.parseInt(dateFormat.format(date));
dateFormat = new SimpleDateFormat("dd");
date = new java.util.Date();
int thisDay = Integer.parseInt(dateFormat.format(date));
//CREATE AN AGE VARIABLE TO HOLD THE CALCULATED AGE
//TO START WILL – SET THE AGE EQUEL TO THE CURRENT YEAR MINUS THE YEAR
//OF THE DOB
int age = thisYear - yearDOB;
//IF THE CURRENT MONTH IS LESS THAN THE DOB MONTH
//THEN REDUCE THE DOB BY 1 AS THEY HAVE NOT HAD THEIR
//BIRTHDAY YET THIS YEAR
if(thisMonth < monthDOB){
age = age -1;
}
//IF THE MONTH IN THE DOB IS EQUEL TO THE CURRENT MONTH
//THEN CHECK THE DAY TO FIND OUT IF THEY HAVE HAD THEIR
//BIRTHDAY YET. IF THE CURRENT DAY IS LESS THAN THE DAY OF THE DOB
//THEN REDUCE THE DOB BY 1 AS THEY HAVE NOT HAD THEIR
//BIRTHDAY YET THIS YEAR
if(thisMonth == monthDOB && thisDay < dayDOB){
age = age -1;
}
employeeage.add(age);
}
return employeeage;
}catch (SQLException e) {
e.printStackTrace();
}catch (Exception e) {
logger.error(e);
e.printStackTrace();
}finally {
SqlUtils.close(rs);
}
return null;
}
,我執行的XML查詢:
<s:query name="GetEmployeeAge" xmlns:s="http://www.slamb.org/axamol/sql-library/statement">
<s:sql databases="mysql">
SELECT DOB(`Date Of Birth`)
FROM `Employee` LEFT JOIN Employee-Detail ON `Employee.MRN` = `Employee-Detail.MRN`
GROUP BY DOB(`Date Of Birth`)
ORDER BY DOB(`Date Of Birth`) ASC
</s:sql>
</s:query>
誰能幫我在指着什麼可能是這個原因?
UPDATE1:的SQL查詢工作correctly.I執行它在MySQL Workbench和它在這種格式YYYY-MM-DD 00:00:00
UPDATE2返回結果:我使用的這個結果生成一個圖表其詳細信息在Generating Histogram through Java and PrimeFaces
使用JodaTime,將數據庫中的值轉換爲DateTime對象 – MadProgrammer
我不確定你的意思聲明,你「無法看到任何輸出「。您的代碼中是否存在一些記錄或System.out語句? –
@MadProgrammer問題是我在使用API時受到了限制。系統不支持它。你可以看看代碼,並在出現任何錯誤時指出幫助嗎? – user2916886