作者BILL0119 (BILL)
看板Web_Design
標題Re: [問題] JSP比較資料庫檔案時間與系統時間
時間Sat May 9 01:51:29 2009
※ 引述《BILL0119 (BILL)》之銘言:
: 標題: [問題] JSP比較資料庫檔案時間與系統時間
: 時間: Thu May 7 14:00:15 2009
:
: 目前有個JSP的問題想請板上的高手解答
:
: 我想抓出資料庫內的時間
:
: 並且比較目前系統時間 (只需要比較年份)
:
: 例如目前時間是2009年,資料庫內的時間是2003-7-20
:
: 我想要列出2003,2004,2005,2006,2007,2008,2009
:
: 但是資料庫內的時間格式是 年-月-日
:
: 目前時間
: Calendar thisMonth=Calendar.getInstance();
: String year=String.valueOf(thisMonth.get(Calendar.YEAR));
:
:
: 資料庫內的時間
: java.sql.Date dt = rs.getDate("DATE");
: SimpleDateFormat sFmt = new SimpleDateFormat("yyyy");
: String sDt = sFmt.format(dt);
:
:
: 我要怎麼比較現在的系統時間
:
: 謝謝
:
:
: --
: ※ 發信站: 批踢踢實業坊(ptt.cc)
: ◆ From: 114.42.160.15
: ※ 編輯: BILL0119 來自: 114.42.160.15 (05/07 14:04)
: ※ 編輯: BILL0119 來自: 114.42.160.15 (05/07 14:21)
: 推 pm2001:從資料庫拉資料出來不然就下sql指令給資料庫算 05/07 14:24
不好意思,我是新手
想再請教一下,我下了SQL給資料庫運算後,要怎麼取運算後的數值啊??
假設說我下了 select count(no) as count from db.pt_info
where year(current timestamp) - year(birthday) <35
and sex IN('F')
想要取得小於35歲女性的人數
我看網路的資料都是用
Connection conn = java.sql.DriverManager.getConnection
(url,user,password);
Statement stmt=conn.createStatement
(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("select count(no) ...........");
這表示他會回傳人數int給 ResultSet rs嗎? (但是這樣會有error訊息)
: → GALINE:都是四位數年份字串的話....sdt.equals(year) ? 05/07 14:28
: → GALINE:我自己是偏好資料庫內把時間存成字串,然後用substr做比對 05/07 14:29
: → BILL0119:資料型態都是String,要怎麼運算呢? 05/07 15:25
: 推 GALINE:用Integer.parseInt轉成數字再來算。不過最主要的原因是我 05/07 15:44
: → GALINE:用不習慣原生的日期型態…[汗] 05/07 15:45
: → TonyQ:其實可以直接下 year 給sql 去撈字串啊... 05/07 15:52
: → BILL0119:了解,謝謝各位高手 05/07 16:03
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.42.161.22
推 TonyQ: as count count是保留字 , 寫成 as [count] or 換個名字 05/09 09:06
→ BILL0119:我直接在資料庫下這些指令是ok的,但是從JSP丟過去就錯了 05/09 11:26
→ BILL0119:指向 ResultSet rs = stmt.executeQuery(SQL) 有問題 05/09 11:28
→ BILL0119:但是我換成較簡單的指令都可以過 05/09 11:28
→ BILL0119:例如:select test from db2admin.test_data 05/09 11:29
→ BILL0119:真是讓我百思不得其解 =.= 05/09 11:30
推 GALINE:錯誤訊息是?單純數筆數我會用count(*) 05/09 13:27
→ TonyQ:那就把error message 拿出來看看. 05/09 18:31