JavaWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JavaWorld@TW » JDBC/SQL討論區 » JDBC 應用  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
話題被移動
該話題已被移動 - browser , 2005-02-22 15:26
如果您尚不清楚該話題被移動的原因,請參考論壇規則以及本版公告或者聯系本版版主。
本主題所含的標籤
無標籤
作者 要如何取回 PreparedStatement 內的 SQL 字串
karry





發文: 243
積分: 2
於 2005-02-22 15:23 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
請問各位大大
當我們用
1
2
3
4
PreparedStatement ps = cn.prepareStatement("sql statement 及一些問號");
ps.setString(1,"value");
ps.setString(2,"value2");
...

產生一個 PreparedStatement 後
有沒有辦法得知最後的 SQL Statement 是如何

因為執行有錯時, 有時候回傳的訊息並不是很明確
所以想知道在 setXXX() 完之後的 SQL 語法到底是怎樣


vote up 0 vote down
reply to postreply to post
作者 Re:要如何取回 PreparedStatement 內的 SQL 字串 [Re:karry]
metavige

麥塔.米奇

版主

發文: 2080
積分: 10
於 2005-02-22 16:44 user profilesend a private message to usersend email to metavigereply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
比較簡單的方法,自己用String先存一份起來
然後有錯的時候,Print到Log內


vote up 0 vote down
reply to postreply to post
請各位新手參考 論壇規範Java 新手 FAQ
作者 Re:要如何取回 PreparedStatement 內的 SQL 字串 [Re:metavige]
karry





發文: 243
積分: 2
於 2005-02-22 22:27 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
metavige wrote:
比較簡單的方法,自己用String先存一份起來
然後有錯的時候,Print到Log內

大大你好
我想你的意思是
1
2
3
4
5
6
String sql = "sql statement 及一些問號";
System.out.print(sql);
PreparedStatement ps = cn.prepareStatement(sql);
ps.setString(1,"value");
ps.setString(2,"value2");
...

可是我的意思是 sql 字串中有一些是問號, 等著要給 PreparedStatement 去 set
而我所不確定的是 set 完後是不是會得到如我想的般的 SQL 語法
所以去 print 未 set 之前含問號的字串, 對我幫助不大
我是想要 set 完後即將執行的真正 SQL 語法
再麻煩你了


vote up 0 vote down
reply to postreply to post
作者 Re:要如何取回 PreparedStatement 內的 SQL 字串 [Re:karry]
metavige

麥塔.米奇

版主

發文: 2080
積分: 10
於 2005-02-23 08:35 user profilesend a private message to usersend email to metavigereply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我想目前JDBC是達不到這樣的要求吧
目前大多數的作法,僅僅是將含有?的SQL印出,再加上按順序印出的參數
來達到Debug的功用

當然,也或許是我沒有讀到相關的API
如果您有找到其他的作法,歡迎分享
不過比較傳統的作法就如同我說的一樣

1
2
3
4
5
6
7
8
9
String sql = "sql statement 及一些問號";
System.out.print(sql);
PreparedStatement ps = cn.prepareStatement(sql);
String param1= "value";
String param2= "value2";
System.out.print("param 1:"+param1);
System.out.print("param 2:"+param1);
ps.setString(1,param1);
ps.setString(2,param2);

類似的作法....


vote up 0 vote down
reply to postreply to post
請各位新手參考 論壇規範Java 新手 FAQ
作者 Re:要如何取回 PreparedStatement 內的 SQL 字串 [Re:karry]
anthonychen

外線交給我

版主

發文: 2008
積分: 8
於 2005-02-23 11:27 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我以前也遇過這類的需求,就是:如何知道 PreparedStatement 裡組成的SQL語句到底是什麼?

後來我想破頭也想不出來,後來靈機一動:既然程式無法取得SQL語句,我在你到資料庫的途中攔截你總可以吧....

所以找到了這個工具 -- SQL Profiler

透過這個GUI工具,你可以得知 PreparedStatement 傳遞了什麼樣的SQL字串給資料庫,是很方便的除錯工具。注意看畫面下方,他還會告訴你原本的 prepared statement 是什麼喔。

SQL Profiler 是基於 p6spy 這個開放原始碼的元件寫成的,如果你想在程式裡自行攔截並記錄sql 語句,你可以用 p6spy 自己
實作這樣的功能。



vote up 0 vote down
anthonychen edited on 2005-02-23 11:31
reply to postreply to post
作者 Re:要如何取回 PreparedStatement 內的 SQL 字串 [Re:karry]
karry





發文: 243
積分: 2
於 2005-02-25 08:24 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
謝謝兩位大大的幫忙Smile

vote up 0 vote down
reply to postreply to post
作者 Re:要如何取回 PreparedStatement 內的 SQL 字串 [Re:anthonychen]
karry





發文: 243
積分: 2
於 2005-05-10 11:29 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
請問一下
要如何使用 SQLProfiler?
我按照它的 readme.txt 做, 可是 SQLProfiler 的狀態列總是寫著 Not connected to P6Spy

我是使用 Tomcat, 以下是我的步驟
1.將 p6spy.jar 及 sqlprofiler.jar 放在我的 webapp 下的 WEB-INF\lib
2.將 spy.properties 放在 WEB-INF\classes
3.修改 spy.properties 的 realdriver 改成使用 oracle
4.執行 SQLProfiler
1
java -jar sqlprofiler.jar

5.啟動 Tomcat
6.將連結資料庫語法的
1
Class.forName("oracle.jdbc.driver.OracleDriver")

改為
1
Class.forName("com.p6spy.engine.spy.P6SpyDriver")

7.執行 JSP 讀取資料庫

結果 JSP 已可以透過 p6spy 讀到資料
但 SQLProfiler 的狀態列總是寫著 Not connected to P6Spy
不知道我究竟是那裡有問題, 請你幫幫忙


vote up 0 vote down
reply to postreply to post
作者 Re:要如何取回 PreparedStatement 內的 SQL 字串 [Re:karry]
anthonychen

外線交給我

版主

發文: 2008
積分: 8
於 2005-05-10 14:43 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
你的 spy.properties 是用 p6spy 所附的 properties 檔嗎?
要用 SQL Profiler 所附的 spy.properties 喔。
因為 SQL Profiler 預設用內部的log4j appender 來記錄sql statement。


vote up 0 vote down
reply to postreply to post
作者 Re:要如何取回 PreparedStatement 內的 SQL 字串 [Re:anthonychen]
karry





發文: 243
積分: 2
於 2005-05-10 17:16 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
anthonychen wrote:
你的 spy.properties 是用 p6spy 所附的 properties 檔嗎?
要用 SQL Profiler 所附的 spy.properties 喔。
因為 SQL Profiler 預設用內部的log4j appender 來記錄sql statement。


有了, 謝謝Smile


vote up 0 vote down
reply to postreply to post
作者 Re:要如何取回 PreparedStatement 內的 SQL 字串 [Re:anthonychen]
karry





發文: 243
積分: 2
於 2005-05-12 09:45 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
請問一下要怎麼設 Filter 啊
我的每次 select 後, 有幾筆資料就會有幾筆 category 為 resultset 的 log
可是我看你附的圖檔很乾浄, 只有 statement,commit,rollback等
我嘗試在 logger 頁籤的 Filter category 設定 filter
可是都沒作用, 應該是我語法不對還是怎樣
以下是我嘗試過的語法
1
2
3
statement
!=resultset
category==statement

又沒有 help 可以看, 只好再麻煩你了


vote up 0 vote down
reply to postreply to post
作者 Re:要如何取回 PreparedStatement 內的 SQL 字串 [Re:karry]
jfwu





發文: 1
積分: 0
於 2006-04-22 13:52 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
可參考
http://www.javaworld.com/javaworld/jw-01-2002/jw-0125-overpower.html


vote up 0 vote down
reply to postreply to post
作者 Re:要如何取回 PreparedStatement 內的 SQL 字串 [Re:karry]
godnesss





發文: 43
積分: 0
於 2006-10-16 13:55 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我測試的結果:

1
2
3
PStmt = Conn.prepareStatement( "SELECT * FROM USERDATA WHERE UNAME = ?" );
PStmt.setString( 1, "Peter's Wife : ;x<>{}()cs\\dfew''d'wd\"oop[]+das |" );
System.out.println( "PStmt = " + PStmt.toString() );  Result = PStmt.executeQuery();

---------------------------------------------------------------------------------
得到

1
PStmt = com.mysql.jdbc.ServerPreparedStatement[1] - SELECT * FROM USERDATA WHERE UNAME = 'Peter\'s Wife : ;x<>{}()cs\\dfew\'\'d\'wd"oop[]+das |'


不知道原發文者是否是想要得到這樣的SQL結果呢?


vote up 0 vote down
reply to postreply to post
作者 Re:要如何取回 PreparedStatement 內的 SQL 字串 [Re:karry]
secretguest0824

別當出頭鳥



發文: 342
積分: 1
於 2008-09-24 12:04 user profilesend a private message to usersend email to secretguest0824reply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
這一篇還不錯,最後面有程式碼下載...
看起來很容易套來用

http://www.javaworld.com/javaworld/jw-01-2002/jw-0125-overpower.html?page=1


vote up 0 vote down
reply to postreply to post
參見,出頭鳥 Blog

» JavaWorld@TW »  JDBC/SQL討論區 » JDBC 應用

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JavaWorld@TW


Powered by Powerful JuteForum® Version Jute 1.5.8
Copyright© 2002-2003 Rainman Zhu,Zua,Netboy,Scott. All Rights Reserved.