JAVA Tomcat configuration Jndi and use C3P0 connection pool __java

Source: Internet
Author: User
Tags auth getmessage

Configure MySQL to be taken in a jndi way when the Meta-inf folder creates the Jndi for the Context.xml configuration application. Advantages for each application of independent jndi.

The configuration file and its steps are as follows:

1, Meta-inf create the <context.xml content as follows:

<pre name= "code" class= "HTML" ><?xml version= "1.0" encoding= "UTF-8"?>
<Context>
< Resource
        name= "Jdbc/mysql"
        auth= "
        Container" factory= "Org.apache.naming.factory.BeanFactory" Type= "Com.mchange.v2.c3p0.ComboPooledDataSource"
        driverclass= "Com.mysql.jdbc.Driver"
        idleconnectiontestperiod= "acquireretryattempts=" "
        3"
        checkouttimeout= "10000"
        maxpoolsize= "50"
        minpoolsize= "2"
        acquireincrement= "2"
        user= "root"
        password= "root"
        jdbcurl= "Jdbc:mysql" ://127.0.0.1:3306/ccc?useunicode=true&characterencoding=utf-8 "/>
</Context>



2, add references in the Web.xml file in Web-inf:

<!--Oracle database Jndi Data Source reference--> <!--<resource-ref>--> <!--<description>oracle DB Connection </description>--> <!--<res-ref-name>oracleDataSource</res-ref-name>--> <!--<
Res-type>javax.sql.datasource</res-type>--> <!--<res-auth>Container</res-auth>--> <!--</resource-ref>--> <!--mysql database jndi data source Reference--> <Resource-ref> <Description>mysql 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> <!--SQL Server database Jndi data source reference--> <!--<resource-ref>--> <!--<description>sqlserver DB Co
    Nnection</description>--> <!--<res-ref-name>sqlserverDataSource</res-ref-name>--> <!--<res-type>javax.sql.DataSource</res-type>--> <!--<res-auth>container</ Res-auth>--> <!--</resource-ref>-->
3, DAO (a servlet or specialized data access Class) read the configuration and its creation connection and DataSource

Import Sun.jdbc.odbc.ee.ConnectionPool;
Import javax.naming.*;
Import Javax.sql.DataSource;
Import java.sql.*;
Import java.sql.*;
Import java.sql.SQLException;
Import java.util.Hashtable;

/**
 * Created by Wuxituong on 2016-10-01.
 * * Public
class Sqldao {
    private static connectionpool instace;
    private static <pre style= "font-family: Song body; font-size:9pt; Background-color:rgb (255, 255, 255); ><span style= "Background-color: #e4e4ff;" >ComboPooledDataSource</span>
ds Get data source private static <span style= "Font-family:arial, Helvetica, Sans-serif;" >
Combopooleddatasource
</span><span style= "font-family:arial, Helvetica, Sans-serif;" >createdatasource () </span> {if (ds = null) {System.out.println ("DS created"); try {Context ct = new Initialco ntext (); if (ct = = null) System.out.println ("No Configuration Environment"); Context Envcontext = (context) ct.lookup ("java:/comp/env"); ds = (<span style= "font-family:arial, Helvetica, Sans-serif;" >
Combopooleddatasource
</span><span style= "font-family:arial, Helvetica, Sans-serif;" >) envcontext.lookup ("Jdbc/mysql"); Obtain data source </span>} catch (Namingexception e) {e.printstacktrace () by name return DS; ///Get Connection object from connection pool public static synchronized Connection getconnection () throws sqlexception,namingexception{Connection Con=null; try {System.out.print ("Preparing to get con");//Get Connection con= (Connection) CreateDataSource (). getconnection ();} catch (Exception e) {E . Printstacktrace (); System.out.print ("Get Connection failed.") "); } return con; //Free connection public static synchronized void freeconnection (Connection con) {try {con.close ();} catch (SQLException e) {E.P Rintstacktrace (); SYSTEM.OUT.PRINTLN ("Close connection failed.") "); } }}
4, use DAO Remember in use con must con.close () put the connection back to the connection pool, otherwise it will cause the connection is occupied complete, can not get con. Causing the server to reboot and release con

Public ResultSet getresultsetdata (String sql) {
        ResultSet rs= null;
        try {
            Statement sta =  con.createstatement ();
            rs = sta.executequery (sql);
            return  rs;
        } catch (Exception e) {
            System.out.println (e.getmessage ());
            return RS;
        }
    public int exesql (String sql) {
        try {
            Statement sta =  con.createstatement ();
            return  sta.executeupdate (SQL);
        catch (Exception e) {
            System.out.println (e.getmessage ());
            Return-1
        }
    }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.