掃二維碼與項目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯(lián)網(wǎng)交流
1)啟動Tomcat服務器,打開瀏覽器,輸入http://localhost:8080/admin(其中l(wèi)ocalhost是名稱服務器或稱為主機),進入管理界面的登陸頁面,這時候請輸入原來安裝時要求輸入的用戶名和密碼,登陸到管理界面。

網(wǎng)站建設公司,為您提供網(wǎng)站建設,網(wǎng)站制作,網(wǎng)頁設計及定制網(wǎng)站建設服務,專注于成都定制網(wǎng)站,高端網(wǎng)頁制作,對iso認證等多個行業(yè)擁有豐富的網(wǎng)站建設經(jīng)驗的網(wǎng)站建設公司。專業(yè)網(wǎng)站設計,網(wǎng)站優(yōu)化推廣哪家好,專業(yè)網(wǎng)站推廣優(yōu)化,H5建站,響應式網(wǎng)站。
2)選擇Resources-Data sources進入配置數(shù)據(jù)源界面,選擇Data Source Actions ->選擇Create New Data Source,進入配置詳細信息界面
主要內(nèi)容例如下:
- JNDI Name: ->jdbc/mysql
- Data Source URL ->jdbc:mysql://localhost:3306/test
- JDBC Driver Class-> org.gjt.mm.mysql.Driver
3)修改\conf\Catalina\localhost目錄下建立一個xml文件,名稱為你所發(fā)布的web應用的名稱.xml,(如testpool.xml)打開添加內(nèi)容如下:
- name="jdbc/mysql"
- type="javax.sql.DataSource"
- password="123456"
- driverClassName="org.gjt.mm.mysql.Driver"
- maxIdle="2"
- maxWait="50"
- username="root"
- url="jdbc:mysql://localhost:3306/test"
- maxActive="4" />
- 內(nèi)容同conf/server.xml中
- name="jdbc/mysql"
- type="javax.sql.DataSource"
- password="123456"
- driverClassName="org.gjt.mm.mysql.Driver"
- maxIdle="2"
- maxWait="50"
- username="root"
- url="jdbc:mysql://localhost:3306/test"
- maxActive="4" />
少了這一步會報錯:Cannot create JDBC driver of class '' for connect URL 'null'
4)修改web.xml
打開%TOMCAT_HOME%\conf\web.xml或yourwebapp/web-inf/web.xml,添加以下內(nèi)容:
DB Connection jdbc/mysql javax.sql.DataSource Container
注意res-ref-name填寫的內(nèi)容要與在上文提到的JNDI Name名稱一致。
到這里,配置工作就基本完成了!
5)引用JNDI時用"java:comp/env/jdbc/mysql";
建立文件測試 test.jsp:
- <%@page contentType="text/html;charset=utf-8" %>
- <%@page import="java.sql.*" %>
- <%@page import="javax.sql.*" %>
- <%@page import="javax.naming.*" %>
Tomcat連接池測試 - <%
- Context ctx=new InitialContext();
- Connection conn=null;
- DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
- conn=ds.getConnection();
- Statement stmt=conn.createStatement(ResultSet.CONCUR_READ_ONLY,ResultSet.CONCUR_UPDATABLE);
- ResultSet rs=stmt.executeQuery("select * from testexample");
- while(rs.next()){
- out.println(rs.getInt(1));
- out.println(rs.getString(2));
- out.println(rs.getString(3));
- }
- out.println("數(shù)據(jù)庫操作成功!");
- rs.close();
- stmt.close();
- conn.close();
- %>
【編輯推薦】

我們在微信上24小時期待你的聲音
解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯(lián)網(wǎng)交流