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

» JWorld@TW » Application Server » Tomcat  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Tomcat 5.5.9的JNDI設定(for MySQL) [精華]
pico2k

地球真危險



發文: 177
積分: 0
於 2005-05-04 12:13 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. 請在webapps/{您的web app名稱}/META-INF/ 新增一個context.xml的檔案.

2. 在context.xml新增以下的內容
1
2
3
4
5
6
7
8
<Context path="/test" docBase="test" debug="5" reloadable="true"> 
   <Resource name="jdbc/MySQL" auth="Container" type="javax.sql.DataSource"
         factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
         driverClassName="com.mysql.jdbc.Driver"
         url="jdbc:mysql://localhost:3306/test"
         username="root" password="root"
         maxActive="20" maxIdle="10" maxWait="-1" />
   </Context>

Ps.我先假設您的Wep App 的名稱為test,你的MySQL的root密碼為root,連接到Test這個資料庫,您應該視您本身的情況而改上述的設定.

3. 重新啟動Tomcat 5.5.9

4. Sample Code
index.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@page import="java.util.*,javax.naming.*,java.sql.*,javax.sql.*" %>
<%@page contentType="text/html;charset=BIG5"%>
<%    
    Context ctx = new InitialContext();      
    String strLookup = "java:comp/env/jdbc/MySQL"; 
    DataSource ds =(DataSource) ctx.lookup(strLookup);
    Connection con = ds.getConnection();
    Statement smt = con.createStatement();
    String sql = "";
    ResultSet rs = null;    
      
    sql = "SELECT * FROM user";
    rs = smt.executeQuery(sql);
    while(rs.next())
    {
         out.println(rs.getString(1));
         out.println(rs.getString(2));         
    }    
%>

Ps.我先假設您在Test資料庫裡有一個User資料表,User資料表內有二個欄位,您應該視您本身的情況而改上述的Code.

Best Regards.


reply to postreply to post
作者 Re:Tomcat 5.5.9的JNDI設定(for MySQL) [Re:pico2k]
zongyou





發文: 5
積分: 0
於 2005-06-09 20: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
嗯~~第二步驟可以從admin控制台內新增

登入後
先看左邊
Tomcat
+Service
+Host
+[要使用JNDI的資料夾]
+Resources
+Data Sources

接下來設定右邊
下拉的選單Data Sources Actions -> 選擇 Create New Data Source

接下來設定
照前輩的設定
JNDI Name: jdbc/MySQL
Data Source URL: jdbc:mysql://localhost:3306/test
JDBC Driver Class: com.mysql.jdbc.Driver
User Name: root
Password: root

接著按 Save

最後按上方 Commit Changes 按鈕就可以了


reply to postreply to post
作者 Re:Tomcat 5.5.9的JNDI設定(for MySQL) [Re:pico2k]
millertime





發文: 29
積分: 0
於 2005-08-07 12:06 user profilesend a private message to usersend email to millertimereply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
原來 5.5.9版的JNDI改了這麼多,害我奮戰了快三天才連成功。Tongue

reply to postreply to post
作者 Re:Tomcat 5.5.9的JNDI設定(for MySQL) [Re:zongyou]
millertime





發文: 29
積分: 0
於 2005-08-07 12:07 user profilesend a private message to usersend email to millertimereply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
zongyou wrote:
嗯~~第二步驟可以從admin控制台內新增

登入後
先看左邊
Tomcat
+Service
+Host
+[要使用JNDI的資料夾]
+Resources
+Data Sources

接下來設定右邊
下拉的選單Data Sources Actions -> 選擇 Create New Data Source

接下來設定
照前輩的設定
JNDI Name: jdbc/MySQL
Data Source URL: jdbc:mysql://localhost:3306/test
JDBC Driver Class: com.mysql.jdbc.Driver
User Name: root
Password: root

接著按 Save

最後按上方 Commit Changes 按鈕就可以了


如果用admin的方式來新增的話,記得要加context.xml喔,不然會失敗


reply to postreply to post
作者 Re:Tomcat 5.5.9的JNDI設定(for MySQL) [Re:millertime]
browser

戀香

版主

發文: 3570
積分: 1
於 2005-08-07 12:37 user profilesend a private message to usersend email to browserreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
millertime wrote:
如果用admin的方式來新增的話,記得要加context.xml喔,不然會失敗


沒錯 ...
我最近測試使用 netbeans 4.1
設定 netbeans 內建 Tomcat 5.5.7 的 JNDI
也才知道用 admin 的方式來新增時,要加上 context.xml
我是參考 netbeans 4.1 document,位置如下:
http://www.netbeans.org/kb/41/using-netbeans/dbconn.html

這篇講的滿清楚的 ....
我也按照它上面所寫的來測試
都沒有什麼問題 ...

Good Luck


reply to postreply to post
作者 Re:Tomcat 5.5.9的JNDI設定(for MySQL) [Re:browser]
millertime





發文: 29
積分: 0
於 2005-08-10 22:55 user profilesend a private message to usersend email to millertimereply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
browser wrote:
沒錯 ...
我最近測試使用 netbeans 4.1
設定 netbeans 內建 Tomcat 5.5.7 的 JNDI
也才知道用 admin 的方式來新增時,要加上 context.xml
我是參考 netbeans 4.1 document,位置如下:
http://www.netbeans.org/kb/41/using-netbeans/dbconn.html

這篇講的滿清楚的 ....
我也按照它上面所寫的來測試
都沒有什麼問題 ...

Good Luck


對阿‥tomcat5.x的說明文件說明的有些不清楚‥


reply to postreply to post
作者 Re:Tomcat 5.5.9的JNDI設定(for MySQL) [Re:pico2k]
lynn2005





發文: 3
積分: 0
於 2005-08-18 16: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
請教一下是否是只有Tomcat 5.5.9才能使用JNDI呢?

Best Regards.


reply to postreply to post
作者 Re:Tomcat 5.5.9的JNDI設定(for MySQL) [Re:pico2k]
lynn2005





發文: 3
積分: 0
於 2005-08-18 16:28 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
請問一下,下面的javax.servlet.ServletException: Name jdbc is not bound in this Context org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)

是指<Resource name="jdbc/MySQL" ....,還是其他,這裡需要需改嗎?

麻煩指教了,謝謝。

HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Name jdbc is not bound in this Context
  org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
  org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
  org.apache.jsp.index_jsp._jspService(index_jsp.java:68)
  org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
  org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
  org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
  org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
  org.apache.naming.NamingContext.lookup(NamingContext.java:768)
  org.apache.naming.NamingContext.lookup(NamingContext.java:138)
  org.apache.naming.NamingContext.lookup(NamingContext.java:779)
  org.apache.naming.NamingContext.lookup(NamingContext.java:138)
  org.apache.naming.NamingContext.lookup(NamingContext.java:779)
  org.apache.naming.NamingContext.lookup(NamingContext.java:151)
  org.apache.naming.SelectorContext.lookup(SelectorContext.java:136)
  javax.naming.InitialContext.lookup(InitialContext.java:351)
  org.apache.jsp.index_jsp._jspService(index_jsp.java:51)
  org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
  org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
  org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
  org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.

--------------------------------------------------------------------------------

Apache Tomcat/5.0.28


reply to postreply to post
作者 Re:Tomcat 5.5.9的JNDI設定(for MySQL) [Re:lynn2005]
pico2k

地球真危險



發文: 177
積分: 0
於 2005-08-18 18: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
Tomcat 5.0 也可以使用JNDI,
不過,其設定檔的格式以及放置的位置跟Tomcat 5.5版不一樣..

Best Regards.


reply to postreply to post
作者 Re:Tomcat 5.5.9的JNDI設定(for MySQL) [Re:pico2k]
smartcdrw





發文: 39
積分: 0
於 2005-09-22 10:44 user profilesend a private message to usersend email to smartcdrwreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
哇勒想說我用4.1x的版本方法來用5.5.9怎麼沒有成功

後來也沒在意因為我的連結有兩個方式
一個失敗另一個連結沒失敗過

敢快來試試大大的方法


smartcdrw edited on 2005-09-22 12:35
reply to postreply to post
作者 Re:Tomcat 5.5.9的JNDI設定(for MySQL) [Re:smartcdrw]
vincent_hwu





發文: 153
積分: 2
於 2005-10-03 13: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
謝謝~好帖

reply to postreply to post
作者 Re:Tomcat 5.5.9的JNDI設定(for MySQL) [Re:zongyou]
xwilson





發文: 12
積分: 0
於 2008-01-09 21:37 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
zongyou wrote:
嗯~~第二步驟可以從admin控制台內新增

登入後
先看左邊
Tomcat
+Service
+Host
+[要使用JNDI的資料夾]
+Resources
+Data Sources

接下來設定右邊
下拉的選單Data Sources Actions -> 選擇 Create New Data Source

接下來設定
照前輩的設定
JNDI Name: jdbc/MySQL
Data Source URL: jdbc:mysql://localhost:3306/test
JDBC Driver Class: com.mysql.jdbc.Driver
User Name: root
Password: root

接著按 Save

最後按上方 Commit Changes 按鈕就可以了


我照著前面大大的作法做,一直試不成功,後來終於找到要在 {tomcat_HOME}\conf\context.xml 多加一行配置
1
<ResourceLink name="jdbc/MySQL" global="jdbc/MySQL" type="javax.sql.DataSource"/>

才執行成功,配置了DataSource卻不能執行的人可以試試


reply to postreply to post
» JWorld@TW »  Application Server » Tomcat

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

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8