<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    jialisoftw

    Myeclipse中JDBC連接池的配置

    軟件版本myeclispe8.0,自帶tomcat6.0.13。

    jdbc:mysql-connector-java-5.1.13-bin.jar


    第一步:建立工程。

    在Myeclipse中file->new->web project。

    因?yàn)樵跍y(cè)試數(shù)據(jù)源(jsp)時(shí)用到了標(biāo)簽庫(kù),所以可以在這里選上jsdl支持,當(dāng)然也可以在工程建好后右鍵工程文件夾->myeclipse->add JSTL

    Libraries…實(shí)現(xiàn)同樣的功能。


    第二步:導(dǎo)入jdbc的jar包。

    要直接將mysql-connector-java-3.1.7-bin.jar復(fù)制到“工程文件夾/WebRoot/WEB-INF/lib”文件夾下。這時(shí),myeclipse會(huì)自動(dòng)生成一個(gè)

    Referenced Libraries,不用管。


    第三步:建立context.xml文件。

    在“工程文件夾/WebRoot/META-INF”下,新建context.xml文件。文件內(nèi)容如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <Context debug="5" reloadable="true">
    <Resource
    name="jdbc/mysql"
    auth="Container"
    type="javax.sql.DataSource"
    maxActive="100"
    maxIdle="30"
    maxWait="10000"
    username="root"
    password=""
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true" />
    </Context>

    解釋:
    name="jdbc/mysql"   //連接名,jndi中使用。具在JSP中用<sql:query var="rs" dataSource="jdbc/mysql">調(diào)用,servlet用 DataSource ds

    = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");調(diào)用。這里是tomcat的格式,不同的服務(wù)器可能有所不同。

    auth="Container"    
    type="javax.sql.DataSource"   
    maxActive="100"
    maxIdle="30"
    maxWait="10000"
    username="root"    //mysql的用戶名
    password=""        //mysql的用戶密碼,我這里是空 
    driverClassName="com.mysql.jdbc.Driver"   //驅(qū)動(dòng)類名,一般確定
    url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true" //javatest是mysql中要使用的數(shù)據(jù)庫(kù)名

    第四步:修改web.xml文件。

    在web.xml文件中添加以下內(nèi)容:                  //重要一定添加進(jìn)去

    <resource-ref>
          <description>DB Connection</description>
          <res-ref-name>jdbc/mysql</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <res-auth>Container</res-auth>
    </resource-ref>

    這部分內(nèi)容一般是確定的。

    OK,現(xiàn)在可以測(cè)試數(shù)據(jù)源是否好了。下面是JSP測(cè)試文件:

    <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%
    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">
        -->
    <sql:query var="rs" dataSource="jdbc/mysql">
    select id, username, password from user
    </sql:query>
    </head>

    <body>
       <h2>Results</h2>

    <c:forEach var="row" items="${rs.rows}">
        Foo ${row.username}<br/>
        Bar ${row.password}<br/>
    </c:forEach> <br>
    </body>
    </html>

    當(dāng)然,如果你用servlet測(cè)試數(shù)據(jù)源也是可以的,下面是一個(gè)servlet測(cè)試?yán)樱?/p>

    package fx;

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;

    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.sql.DataSource;

    public class DsTest extends HttpServlet
    {

        /**
         * Constructor of the object.
         */
        public DsTest()
        {
            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
        {

            doPost(request,response);
        }

        /**
         * 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
        {

            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out
                    .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
            out.println("<HTML>");
            out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
            out.println(" <BODY>");
            out.print("    This is ");
            out.print(this.getClass());
            out.println(", using the POST method");
            try
            {

               Context ctx = new InitialContext();
               DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
               Connection conn = ds.getConnection();
               ResultSet rs=conn.createStatement().executeQuery("select * from user");
               rs.next();
               out.print(rs.getString(2));
            } catch (NamingException e) {
                e.printStackTrace(out);
               System.out.println(e.getMessage());
            } catch (SQLException e) {
               e.printStackTrace(out);
            }
            out.println("connection pool connected !!haha"); 
            out.println(" </BODY>");
            out.println("</HTML>");
            out.flush();
            out.close();
        }

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

    }


    我的mysql數(shù)據(jù)庫(kù)名為javatest,表名為user,有三列"id","username","password"。servlet運(yùn)行結(jié)果將打印出user表中的第一個(gè)用戶名。

    注意:如果你新建servlet,myeclipse會(huì)自動(dòng)幫你在web.xml中生成相應(yīng)的mapping,而這部分內(nèi)容可能“插”進(jìn)

    <resource-ref>
          <description>DB Connection</description>
          <res-ref-name>jdbc/mysql</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <res-auth>Container</res-auth>
    </resource-ref>

    中,造成錯(cuò)誤。注意自己手動(dòng)調(diào)整。      //要看一下,因?yàn)槲业暮孟駴]有這段代碼

    別外,注意myeclipse中的servlet映射為“服務(wù)器ip:端口/工程文件名/servlet/servlet名”。

     

     

     

     

    還有就是最好使用外部自己配置的Tomcat,并將mysql-connector-java-5.1.13-bin.jar包放到Tomcat的lib目錄下。

    最好不要用MyEclipse自帶的Tomcat,因?yàn)槲业腗yEclipse提示org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'

    即Tomcat出現(xiàn)異常,找不到j(luò)dbc驅(qū)動(dòng)包

    posted on 2012-10-11 15:34 飛豬一號(hào) 閱讀(2474) 評(píng)論(0)  編輯  收藏


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     

    導(dǎo)航

    <2012年10月>
    30123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910

    統(tǒng)計(jì)

    常用鏈接

    留言簿

    隨筆檔案

    友情鏈接

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 国产乱弄免费视频| 久久亚洲免费视频| 久久国产精品免费一区| 亚洲AV无码成人专区片在线观看| 99久久久精品免费观看国产| 亚洲av日韩专区在线观看| 久久久久噜噜噜亚洲熟女综合| 91高清免费国产自产拍2021| 亚洲 欧洲 自拍 另类 校园| 亚洲精品无码乱码成人| 久久久高清免费视频| 午夜亚洲国产精品福利| 日产亚洲一区二区三区| 免费v片在线观看| 男女免费观看在线爽爽爽视频| 五月婷婷免费视频| 亚洲啪啪免费视频| 亚洲乱亚洲乱妇无码麻豆| 日韩免费一区二区三区在线| 国产免费牲交视频免费播放| 中文字幕亚洲综合小综合在线| 国内精品99亚洲免费高清| 毛片免费在线视频| 中文字幕在线观看免费视频| 一级特级aaaa毛片免费观看 | 色播在线永久免费视频| 免费无码av片在线观看| 久久亚洲中文无码咪咪爱| 亚洲精品亚洲人成在线观看麻豆| 亚洲av无码专区在线观看素人| 成年女人18级毛片毛片免费 | 久久er国产精品免费观看8| 亚洲欧美一区二区三区日产| 亚洲黄色在线电影| 久热综合在线亚洲精品| 亚洲高清无码专区视频| 超pen个人视频国产免费观看| 在线看片v免费观看视频777 | 日韩毛片免费无码无毒视频观看| 国内少妇偷人精品视频免费| 成人a毛片视频免费看|