讀萬(wàn)卷書(shū)不如行千里路,經(jīng)驗(yàn)的積累又不是一蹴而就的,不但需要知識(shí)的沉積,還需要長(zhǎng)久經(jīng)驗(yàn)的總結(jié)升華

          常用鏈接

          統(tǒng)計(jì)

          積分與排名

          AJAX相關(guān)站點(diǎn)

          java壓力、性能等測(cè)試工具

          開(kāi)源軟件基地

          最新評(píng)論

          Tomcat 的數(shù)據(jù)庫(kù)連接池設(shè)置與應(yīng)用(Mysql篇)之(實(shí)例講解)

          我用的是配置:Tomcat5.5+JEE(jsdk1.5)+WINXP

          還是簡(jiǎn)單的說(shuō)一說(shuō)文件配置:
          1:修改%tomcat%/conf/server.xml在<GlobalNamingResources>后加如下內(nèi)容.
          ??????<Resource
          ??????name="jdbc/DBPool"?//數(shù)據(jù)源名稱(chēng)
          ??????type="javax.sql.DataSource"
          ??????password="xxxxxxxx"
          ??????driverClassName="com.mysql.jdbc.Driver"
          ??????maxIdle="2"
          ??????maxWait="5000"
          ??????username="root"
          ??????url="jdbc:mysql://127.0.0.1:3306/hptest"
          ??????maxActive="4"/>
          2.修改%tomcat%/conf/context.xm;在<Context>后加
          ???<ResourceLink
          ???name="jdbc/DBPool"?
          ???type="javax.sql.DataSource"?
          ???global="jdbc/DBPool"/>
          3.修改%tomcat%/conf/web.xml?
          <resource-ref>
          ????<description>MySQL?DB?Connection?Pool</description>
          ????<res-ref-name>jdbc/DBPool</res-ref-name>
          ????<res-type>javax.sql.DataSource</res-type>
          ????<res-auth>Container</res-auth>
          ????<res-sharing-scope>Shareable</res-sharing-scope>
          ?</resource-ref>
          ??這樣配置就算差不多了.如果具體的還不懂可見(jiàn)上次發(fā)的文章.
          4.寫(xiě)一個(gè)程序測(cè)試.(寫(xiě)一個(gè)WEB程序)
          ???我的是Myeclipse?寫(xiě)的程序,這里不能從電腦上貼圖真有點(diǎn)不方便(我想哭).
          ??????1:寫(xiě)一個(gè)連接類(lèi):????
          ??DBPool.java
          ?package?com.test;

          import?javax.naming.Context;
          import?javax.naming.InitialContext;
          import?javax.naming.NamingException;
          import?javax.sql.DataSource;

          public?class?DBPool?{
          ????private?static?DataSource?pool;
          ????static?{
          ?????????Context?env?=?null;
          ??????????try?{
          ??????????????env?=?(Context)?new?InitialContext().lookup("java:comp/env");
          ??????????????pool?=?(DataSource)env.lookup("jdbc/DBPool");
          ??????????????if(pool==null)?
          ??????????????????System.err.println("'DBPool'?is?an?unknown?DataSource");
          ???????????????}?catch(NamingException?ne)?{
          ??????????????????ne.printStackTrace();
          ??????????}
          ??????}
          ????public?static?DataSource?getPool()?{
          ????????return?pool;
          ????}
          ???
          }
          ????2:寫(xiě)一個(gè)Servlet:??
          ??其中有是用來(lái)連接數(shù)據(jù)庫(kù)和顯示查詢結(jié)果.
          ??Mytest.java
          ??package?com.test;

          import?java.io.IOException;
          import?java.io.PrintWriter;
          import?java.sql.*;

          import?javax.servlet.ServletException;
          import?javax.servlet.http.HttpServlet;
          import?javax.servlet.http.HttpServletRequest;
          import?javax.servlet.http.HttpServletResponse;

          public?class?Mytest?extends?HttpServlet?{

          ????/**
          ?????*?Constructor?of?the?object.
          ?????*/
          ????public?Mytest()?{
          ????????super();
          ????}

          ????/**
          ?????*?Destruction?of?the?servlet.?<br>
          ?????*/
          ????public?void?destroy()?{
          ????????super.destroy();?//?Just?puts?"destroy"?string?in?log
          ????????//?Put?your?code?here
          ????}

          ????/**
          ?????*?The?doGet?method?of?the?servlet.?<br>
          ?????*
          ?????*?This?method?is?called?when?a?form?has?its?tag?value?method?equals?to?get.
          ?????*?
          ?????*?@param?request?the?request?send?by?the?client?to?the?server
          ?????*?@param?response?the?response?send?by?the?server?to?the?client
          ?????*?@throws?ServletException?if?an?error?occurred
          ?????*?@throws?IOException?if?an?error?occurred
          ?????*/
          ????public?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)
          ????????????throws?ServletException,?IOException?{

          ????????response.setContentType("text/html;charset=gb2312");
          ????????PrintWriter?out?=?response.getWriter();
          ????????String?id=(String)request.getParameter("id");
          ????????Connection?con=null;
          ????????try{
          ????????????
          ????????????con=DBPool.getPool().getConnection();
          ?????????????Statement?stmt=con.createStatement();
          ????????????????ResultSet?rst=stmt.executeQuery("select?*?from?userinf?where?userid='"+id+"'");
          ??????????????????if(rst.next()){
          ??????????????????????????????????????????out.println("<br>ID號(hào):"+rst.getInt("userid"));
          ??????????????????????out.println("<br>用戶名:"+com.test.ASSICTOGBR2312.trans(rst.getString("name")));
          ??????????????????????out.println("<br>地址:"+rst.getString("address"?));
          ??????????????????????out.println("<br>生日"+rst.getDate("year"?));
          ??????????????????}
          ??????????????????else{
          ??????????????????out.println("沒(méi)有這個(gè)ID");
          ??????????????????stmt.close();
          ??????????????????}
          ????????}
          ????????catch(Exception?e){
          ????????????e.printStackTrace();
          ????????}
          ????????finally{??//一定要注意這里對(duì)數(shù)據(jù)庫(kù)的關(guān)閉不然
          ?????????????????????????????//自己想了
          ????????????try{
          ????????????????if(con!=null){
          ????????????????????con.close();
          ????????????????}
          ????????????????
          ????????????}
          ??????????catch(Exception?e){
          ???????????e.printStackTrace();
          ???????????
          ???????}
          ????????????
          ????????}
          ????
          ????}

          ????/**
          ?????*?The?doPost?method?of?the?servlet.?<br>
          ?????*
          ?????*?This?method?is?called?when?a?form?has?its?tag?value?method?equals?to?post.
          ?????*?
          ?????*?@param?request?the?request?send?by?the?client?to?the?server
          ?????*?@param?response?the?response?send?by?the?server?to?the?client
          ?????*?@throws?ServletException?if?an?error?occurred
          ?????*?@throws?IOException?if?an?error?occurred
          ?????*/
          ????public?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)
          ????????????throws?ServletException,?IOException?{
          ????????doGet(request,response);
          ????????
          ????}

          ????/**
          ?????*?Initialization?of?the?servlet.?<br>
          ?????*
          ?????*?@throws?ServletException?if?an?error?occure
          ?????*/
          ????public?void?init()?throws?ServletException?{
          ????????//?Put?your?code?here
          ????}

          }
          ??????3:寫(xiě)一個(gè)JSP頁(yè)面:
          ?????????Myjsp.jsp
          ?????
          <%@?page?language="java"?import="java.util.*"?pageEncoding="gb2312"%>
          <%
          String?path?=?request.getContextPath();
          String?basePath?=?request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
          %>

          <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">
          <html>
          ??<head>
          ????<base?href="<%=basePath%>">
          ????
          ????<title>My?JSP?'MyJsp.jsp'?starting?page</title>
          ????
          ????<meta?http-equiv="pragma"?content="no-cache">
          ????<meta?http-equiv="cache-control"?content="no-cache">
          ????<meta?http-equiv="expires"?content="0">
          ????<meta?http-equiv="keywords"?content="keyword1,keyword2,keyword3">
          ????<meta?http-equiv="description"?content="This?is?my?page">
          ????
          ????<!--
          ????<link?rel="stylesheet"?type="text/css"?href="styles.css">
          ????-->
          ??</head>
          ??
          ??<body>
          ??
          ????This?is?my?JSP?page.?<br>
          ????<FORM?action="/MyWeb/servlet/Mytest"?method="post">
          ????print?input?search?id:?<INPUT?type="text"?name="id">
          ????<INPUT?type="submit"?value="submit">
          ????</FORM>
          ??</body>
          </html>
          ???4:其中有轉(zhuǎn)字,為了不顯示亂碼
          ????package?com.test;

          import?java.io.*;
          public?class?ASSICTOGBR2312?{
          ??????public?static?String?trans(String?ass){
          ??????????String?res=null;
          ??????????byte?temp[];
          ??????????try{
          ??????????????temp=ass.getBytes("iso-8859-1");
          ??????????????res=new?String(temp);
          ??????????}
          ??????????catch(UnsupportedEncodingException?en)?{
          ??????????????System.out.println(en.toString());
          ??????????}
          ??????????
          ?????????
          ???????????
          ??????????return?res;?
          ????
          ??????}
          }


          ???5:web.xml

          ???<?xml?version="1.0"?encoding="UTF-8"?>
          <web-app?version="2.4"?
          ????xmlns="http://java.sun.com/xml/ns/j2ee"?
          ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?
          ????xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee?
          ????http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
          ??<servlet>
          ????<description>This?is?the?description?of?my?J2EE?component</description>
          ????<display-name>This?is?the?display?name?of?my?J2EE?component</display-name>
          ????<servlet-name>Mytest</servlet-name>
          ????<servlet-class>com.test.Mytest</servlet-class>
          ??</servlet>

          ??<servlet-mapping>
          ????<servlet-name>Mytest</servlet-name>
          ????<url-pattern>/servlet/Mytest</url-pattern>
          ??</servlet-mapping>

          </web-app>

          ???6:?其中數(shù)據(jù)庫(kù)結(jié)構(gòu)如下:
          ??????數(shù)據(jù)庫(kù)名:hptest
          ??????表:userinf
          ??????用下面的命令建一個(gè)數(shù)據(jù)庫(kù)和表
          ??????create?database?hptest;
          ??????create?table??userinf?(
          ???????userid?int(4)?not?null,
          ???????name?char(10)?not?null,
          ???????address?varchar(50),
          ???????year?date,
          ???????constraint?fk_userinf?primary?key(userid));
          ???????)
          ???????insert?into?userinf?values(19,'hp','cq','1982-10-22');
          ?????
          ?希望對(duì)某些人能有所幫助.謝謝大家支持

          posted on 2006-04-04 13:40 劉軍偉 閱讀(307) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Web應(yīng)用服務(wù)器(中間件)

          主站蜘蛛池模板: 南岸区| 石门县| 娱乐| 石城县| 洪湖市| 虎林市| 安龙县| 时尚| 漠河县| 平乐县| 赤水市| 都安| 轮台县| 江西省| 兴城市| 方山县| 桐柏县| 青阳县| 广平县| 长丰县| 滦平县| 定日县| 绥江县| 闽侯县| 鲜城| 紫金县| 马鞍山市| 高阳县| 宜宾市| 霞浦县| 禄丰县| 临汾市| 聊城市| 华阴市| 迁安市| 永春县| 茌平县| 东方市| 安宁市| 白玉县| 葵青区|