• 利用myeclipse配置数据库连接池


    作为一个习惯使用myeclipse的人来说,即使是数据库连接池也肯定是用ide配置了.

    下面说一下用数据库连接池的配置.

    1 创建工程.不多说了.

    2 添加数据库连接程序驱动包.直接放到lib目录下即可

    3 配置context.xml文件

    <?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="1234" 
    driverClassName="com.mysql.jdbc.Driver" 
    url="jdbc:mysql://localhost:3306/pos?autoReconnect=true" /> 
    </Context>
    解释: 
    name="jdbc/mysql"   //连接名,jndi中使用。具在JSP中用<sql:query var="rs" dataSource="jdbc/mysql">调用,servlet用 DataSource ds
    
    = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");调用。这里是tomcat的格式,不同的服务器可能有所不同。
    
    auth="Container"     
    type="javax.sql.DataSource"    
    maxActive="100" 
    maxIdle="30" 
    maxWait="10000" 
    username="root"    //mysql的用户名 
    password=""        //mysql的用户密码,我这里是空  
    driverClassName="com.mysql.jdbc.Driver"   //驱动类名,一般确定 
    url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true" //javatest是mysql中要使用的数据库名

    4 配置web.xml文件

      <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>

    如此就完成了数据库的连接.我喜欢用servlet,给出一个servlet的测试代码:

    package com.pos.test;
    
    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 TEST extends HttpServlet 
    {
    
        /** 
         * Constructor of the object. 
         */ 
        public TEST() 
        { 
            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 admininfo");
               while(rs.next()){
               out.print(rs.getString(1)); }
            } 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 
        }
    
    }
    View Code

    这里我的数据库名称:pos,我查询了admininfo表中的第二列的数据。

    关键代码:

    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 admininfo");
               while(rs.next()){
               out.print(rs.getString(1)); }
            } catch (NamingException e) { 
                e.printStackTrace(out); 
               System.out.println(e.getMessage()); 
            } catch (SQLException e) { 
               e.printStackTrace(out); 
            } 

    结果集rs,返回之后跟直接jdbc访问数据库差别不大了。

    ps:其实我到没有感觉用数据库连接池有什么方便之处。但是学习的过程中,示例项目用到,就学习了一下。不然这个地方一直卡住。。。唉~还是太弱了。

    参考:Myeclipse中的jdbc连接池配置

  • 相关阅读:
    spark学习
    推荐系统-摘录
    matplotlib安装问题解决
    秒杀系统分析
    大数据常用启动命令
    推荐系统简介
    java面试-JVM内存结构
    推荐算法-基于内容的推荐
    推荐算法-基于模型的协同过滤
    监督学习之模型评估与选择
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/6029234.html
Copyright © 2020-2023  润新知