servlet JDBC 數(shù)據(jù)庫(kù)連接池_第1頁(yè)
servlet JDBC 數(shù)據(jù)庫(kù)連接池_第2頁(yè)
servlet JDBC 數(shù)據(jù)庫(kù)連接池_第3頁(yè)
servlet JDBC 數(shù)據(jù)庫(kù)連接池_第4頁(yè)
servlet JDBC 數(shù)據(jù)庫(kù)連接池_第5頁(yè)
已閱讀5頁(yè),還剩2頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

servletJDBCmysql數(shù)據(jù)庫(kù)連接池標(biāo)簽:servletmysqljdbc2014-11-2621:432166人讀評(píng)(0)收藏舉報(bào)分類:javaweb開(4)版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。具體操作過(guò)程:1、配置全局?jǐn)?shù)據(jù)庫(kù)連接池

MySQL數(shù)據(jù)庫(kù)在tomcat安目錄\conf\server.xml文中找到標(biāo)簽下列配置:[html]viewplaincopy<Resourcename="jdbc/webdb"auth="Container"type="javax.sql.DataSource"driverClassName="com.mysql.jdbc.Driver"url="jdbc:mysql://localhost:3306/webdb?characterEncoding=UTF-8"username="root"password="root"maxActive="200"maxIdle="50"maxWait="3000"/>2、下載JDBC驅(qū)的jar包放在tomcat的目下3、在創(chuàng)建的webdemo工中web.xml文中增加下列配置:[html]viewplaincopy<resource-ref><res-ref-name>jdbc/webdb</res-ref-name><res-type>javax.sql.DataSource</res-type><res-auth>Container</res-auth><res-sharing-scope>Shareable</res-sharing-scope></resource-ref>4、在webdemo工的META-INF目下增加一個(gè)context.xml文,內(nèi)容如下:[html]viewplaincopy<?xmlversion="1.0"encoding="UTF-8"?><Context><ListenerclassName="org.apache.catalina.startup.TldConfig"/><ListenerclassName="org.apache.catalina.startup.TldConfig"/><ListenerclassName="org.apache.catalina.startup.TldConfig"/><ListenerclassName="org.apache.catalina.startup.TldConfig"/>

<Resourceauth="Container"description="webdb"name="jdbc/webdb"password="root"username="root"type="javax.sql.DataSource"driverClassName="com.mysql.jdbc.Driver"url="jdbc:mysql://localhost:3306/webdb?characterEncoding=UTF-8"/></Context>5、訪問數(shù)據(jù)庫(kù)事例Java文[java]viewpackagechapter4;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;publicclassHelloworldextendsHttpServlet{publicdoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{response.setContentType("text/html;charset=UTF-8");PrintWriterout=response.getWriter();try{javax.naming.Context=newjavax.naming.InitialContext();javax.sql.DataSource(javax.sql.DataSource)ctx.lookup("java:/comp/env/jdbc/webdb");Connectionconn=ds.getConnection();

PreparedStatementpst=conn.prepareStatement("SELECT*t_dictionary");ResultSetrspst.executeQuery();StringBuildertable=StringBuilder();table.append("<tableborder='1'>");table.append("<tr><td>eg-name</td><td>ch-name</td></tr>");while(rs.next()){table.append("<tr><td>"+rs.getString("english")+"</td><td>");table.append(rs.getString("chinese")+"</td></tr>");}table.append("</table>");out.println(table.toString());pst.close();}catch(Exception{out.println(e.getMessage());}}}差不多訪問MySQL數(shù)庫(kù)能測(cè)試成功。SQLServer

Jsp中用數(shù)據(jù)庫(kù)連接池.在tomcat服器目錄下面的conf中到一個(gè)叫Context.xml的配置文件,在其中加入以下代碼<Resourcename="jdbc/books"auth="Container"type="javax.sql.DataSource"maxActive="100"maxIdle="30"maxWait="10000"username="sa"password="120010"driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"url="jdbc:sqlserver://localhost:1433;DatabaseName=news"/>參數(shù)含義:JNDI(javanaminganddirectoryinterface):Java命名和目錄接口name指定的名稱

auth指定管理Resource的(Container:由器創(chuàng)建和管|Application由Web應(yīng)創(chuàng)建和管理)type指定所的類maxActive指定連接池中處于活動(dòng)狀態(tài)的數(shù)據(jù)庫(kù)連接的最大數(shù)目maxIdle指定連接池中處于空閑狀態(tài)的數(shù)據(jù)庫(kù)連接的最大數(shù)目maxWait指定連接池中的連接處于空閑的最長(zhǎng)時(shí)間,超過(guò)這個(gè)時(shí)間會(huì)拋出異常,取值,示可以無(wú)限期等待第步將數(shù)據(jù)動(dòng)jar放目下的lib或common\lib下第步,打開應(yīng)程序的Web.xml文件,添加以下配置<resource-ref><res-ref-name>jdbc/books</res-ref-name><res-type>javax.sql.DataSource</res-type><res-auth>Container</res-auth></resource-ref>節(jié)點(diǎn)數(shù)據(jù)來(lái)源于Context.xml里設(shè)置的數(shù)據(jù)在文中先導(dǎo)入以下包importjavax.sql.DataSource;importjavax.naming.Context;importjavax.naming.InitialContext;importjavax.naming.NamingException;將原來(lái)的數(shù)據(jù)庫(kù)連接操作Class.forName("oracle.jdbc.driver.OracleDriver");conn==null||conn.isClosed()connDriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","system","accp");

換成ContextInitialContext();DataSource(DataSource)c.lookup("java:comp/env/jdbc/books");connds.getConnection();記得要捕獲NamingException與SQLException異常使用連接池的好處是數(shù)據(jù)庫(kù)操作性能得到提升通過(guò)連接池管理數(shù)據(jù)庫(kù)的連接與釋放、提高了系統(tǒng)資源的使用效率sqlserverjsp數(shù)據(jù)庫(kù)連接池配置(2013-09-0817:45:05)載▼標(biāo)簽:jsp數(shù)據(jù)庫(kù)連接池配置分:學(xué)習(xí)tomcat數(shù)庫(kù)連接池配置2010-12-26配方式總結(jié)有兩種:第一種:在應(yīng)用程序工程目錄下置個(gè)context.xml注意的這里的文件名必須是contexttomcat啟動(dòng)的時(shí)候就讀取這個(gè)context.xml文并將其發(fā)布到tomcat下的conf\Catalina\localhost下成一個(gè)工程目錄名的xml文,如果你的工程名為,么生成的就是一個(gè)test.xml文,里面的內(nèi)容是Copy的context.xml的內(nèi)容。如果不是這個(gè)名字那么報(bào)錯(cuò)tomcat容啟動(dòng)后不會(huì)在conf\Catalina\localhost下生成一個(gè)工程目錄名的xml文件而在訪問頁(yè)面test.jsp時(shí)報(bào)javax.naming.NameNotFoundException:communicationnotinthisContext錯(cuò)換句說(shuō)如果你在META-INF下沒有一個(gè),直接在conf\Catalina\localhost目下置一個(gè)工程目錄.xml也可的type="javax.sql.DataSource"maxIdle="30"maxWait="10000"username="sa"password="sa"driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"url="jdbc:sqlserver://localhost:1433;DataBaseName=test"/>另外一種方式:在下context.xml文中加上

type="javax.sql.DataSource"maxIdle="30"maxWait="10000"username="sa"password="sa"driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"url="jdbc:sqlserver://localhost:1433;DataBaseName=test"/>剛開始學(xué)的時(shí)候總是也在web.xml中加上下面一段配置但后來(lái)發(fā)現(xiàn)配不配置這個(gè)都無(wú)關(guān)緊要。我試過(guò)可行。不信的話可以嘗試一下此種方式不會(huì)在conf\Catalina\localhost路下生成一個(gè)工程名.xml文這兩種的java代packagecommon;importjavax.naming.Context;importjavax.naming.InitialContext;importjavax.naming.NamingException;importjavax.sql.DataSource;importpublicclassPoolConnectionpublicstaticConnectiongetConnection()NamingException,SQLException{//context接口提供了查找JNDIResource的口ContextInitialContext();//datasource對(duì)由tomcat供,所不能在程序中實(shí)例化獲需查找獲取DataSourceds=(DataSource)ct.lookup("java:comp/env/communication");//獲datasource對(duì)象后

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論