看板 Web_Design 關於我們 聯絡資訊
※ 引述《BILL0119 (BILL)》之銘言: : 標題: Re: [問題] JSP比較資料庫檔案時間與系統時間 : 時間: Sat May 9 23:35:17 2009 : : ※ 引述《BILL0119 (BILL)》之銘言: : : 標題: 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 : : 訊息指出 : : HTTP Status 500 : : type Exception report : : message : : description The server encountered an internal error () : that prevented it from fulfilling this request. : : exception : : org.apache.jasper.JasperException: : Exception in JSP: /Pages/AA_SQL_TEST.jsp:65 : : 62: : 63: : 64: String sql = "select count(*) as count from db2admin.pt_info : where year(current timestamp) - year(birthday) <35 and sex IN('F')"; : 65: ResultSet rs = stmt.executeQuery(sql); : 66: : 67: : 68: : : : 這表示65行有問題? : : 這個SQL指令我在資料庫指令列下是ok的 : : 我的環境是用apache-tomcat-5.5.27 : : eclipse java6-u13 : : db2資料庫 : : : -- : ※ 發信站: 批踢踢實業坊(ptt.cc) : ◆ From: 114.42.80.235 : → TonyQ:這訊息沒有幫助 , 去查tomcat log ,找出這internal error 05/09 23:38 : → TonyQ:的detail來看才有幫助. 05/09 23:38 : 推 etchen:應該是65行在執行sql指令的時候有問題...把sql指令改一下吧 05/10 12:18 : → etchen:Select count(*) as countinfo from pt_info 05/10 12:19 : → etchen:where (year(current timestamp) - year(birthday)) <35 05/10 12:20 : → etchen:and sex IN('F') 05/10 12:20 : → etchen:as什麼東西盡量不要用保留字,還有算年時加上括弧 05/10 12:21 : → etchen:最好把sql指令用print out出來再到資料庫QUERY下看看 05/10 12:22 : → etchen:有沒有問題。 05/10 12:22 : → BILL0119:我發現去掉COUNT指令就可以執行,這表示不能計數嗎? 05/10 14:34 : → BILL0119:有其他辦法可以計數嗎?? 05/10 14:34 : → TonyQ:如果在 dbms 上可以執行的查詢 , 搬到 jsp 就一定可以. 05/10 14:49 : → TonyQ:你去翻 tomcat log 先啦 , 有 log 不找瞎猜幹麼...-_- 05/10 14:49 : → TonyQ:通常會在tomcat根目錄的 logs資料夾下 05/10 14:50 哈~說得也是 2009/5/10 下午 04:09:37 org.apache.catalina.core.StandardWrapperValve invoke 嚴重的: Servlet.service() for servlet jsp threw exception com.ibm.db2.jcc.b.SqlException: DB2 SQL Error: SQLCODE=-727, SQLSTATE=56098, SQLERRMC=2;-243;36001;SQL_CURSH200C1, DRIVER=3.50.152 我在這裡找到錯誤訊息解釋 http://0rz.tw/cMbyC 但是我不懂他在說啥麼 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.42.80.235
TonyQ:就我們前面在講的保留字問題啊...有些字是db內建的函式名稱 05/10 16:50
TonyQ:或關鍵字 , 需要避開. 05/10 16:50
TonyQ:你先把as count 改成別的字眼看看... 05/10 16:50
TonyQ:ex. as cnt 05/10 16:50
BILL0119:別名我改過了,也是有一樣的情況 05/10 21:35
TonyQ:我突然想到 , 在某些db (ex. access) no也是保留字. 05/10 21:43
TonyQ:http://0rz.tw/Mpfqo 這篇有描述到no , 所以寫成count([no]) 05/10 21:44
TonyQ:看看. 05/10 21:44
BILL0119:我用的是 count(*) 我猜想應該不是保留字的問題 05/10 21:59
TonyQ:對欸 , 發現我看歪了 , 727 剛好在保留字的問題上面.-_- 05/10 22:03
TonyQ:727 那問題我也看不懂 :( 05/10 22:04
tomin:你說用select count(*)錯 用select * 就對? 05/11 00:18
BILL0119:對耶 我又開始猜,是不是資料庫避免過多的運算 05/11 00:27
BILL0119:所以禁止這類的SQL語法從頁面query 05/11 00:27
BILL0119:資料庫是DB2 05/11 00:28
etchen:先把where條件先拿掉count看看,如果ok就是where條件有問題 05/11 12:40
etchen:因為我看錯誤訊息寫到相關次查詢或搜尋條件 05/11 12:40
etchen:(例如 WHERE 子句或核對限制)不容許。 05/11 12:41
BILL0119:回etchen大:where拿掉也不能過 05/11 15:19