利用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連接池配置


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM