当前位置: 代码迷 >> Java Web开发 >> 谁能教我怎样用连接池连接数据库
  详细解决方案

谁能教我怎样用连接池连接数据库

热度:215   发布时间:2010-06-12 19:29:11.0
谁能教我怎样用连接池连接数据库
我用过连接池连接数据库,连接池在服务器server.xml
配置好的,自己写了连接程序,但是在JSP应用当中,就出现错误了,我烦死了,请问熟悉这方面的认识教教我,我在一个JSP网站呢,比较急,在次先谢了,最好给我一个好的程序参考,至于分,我现在没有了,下次在给你们,拜托了!!!!
搜索更多相关主题的帖子: 数据库  

----------------解决方案--------------------------------------------------------
连接的是什么数据库啊
----------------解决方案--------------------------------------------------------
proxool.xml

<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
  <proxool>
    <alias>proMysql</alias>
    <driver-url>jdbc:mysql://mysqlhost:3306/heating?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF8&amp;zeroDateTimeBehavior=convertToNull</driver-url>
    <driver-class>org.gjt.mm.mysql.Driver</driver-class>
    <driver-properties>
      <property name="user" value="root"/>
      <property name="password" value=""/>
    </driver-properties>
    <maximum-connection-count>10</maximum-connection-count>
    <minimum-connection-count>5</minimum-connection-count>
    <house-keeping-sleep-time>90000</house-keeping-sleep-time>
    <prototype-count>5</prototype-count>
    <test-before-use>true</test-before-use>
    <simultaneous-build-throttle>5</simultaneous-build-throttle>
    <house-keeping-test-sql>SELECT CURRENT_DATE</house-keeping-test-sql>
  </proxool>

</something-else-entirely>

app-config-context.xml


    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName">
            <value>org.logicalcobwebs.proxool.ProxoolDriver</value>
        </property>
        <property name="url">
            <value>proxool.proMysql</value>
        </property>
    </bean>


监听器

public class ProxoolListener implements ServletContextListener {
    private static final Log LOG = LogFactory.getLog(ProxoolListener.class);

    private static final String XML_FILE_PROPERTY = "xmlFile";

    private static final String PROPERTY_FILE_PROPERTY = "propertyFile";

    private static final String AUTO_SHUTDOWN_PROPERTY = "autoShutdown";

    @SuppressWarnings("unused")
    private boolean autoShutdown = true;

    public void contextDestroyed(ServletContextEvent arg0) {
        System.out.println("destroy database pool....");
    }

    public void contextInitialized(ServletContextEvent contextEvent) {
        System.out.println("proxool Initializing...");
        ServletContext context = contextEvent.getServletContext(); //
----------------解决方案--------------------------------------------------------
String appDir = contextEvent.getServletContext().getRealPath("/");
        Properties properties = new Properties();

        Enumeration names = context.getInitParameterNames();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            String value = context.getInitParameter(name);

            if (name.equals(XML_FILE_PROPERTY)) {
                try {
                    File file = new File(value);
                    if (file.isAbsolute()) {
                        JAXPConfigurator.configure(value, false);
                    } else {
                        JAXPConfigurator.configure(appDir + File.separator + value, false);
                    }
                } catch (ProxoolException e) {
                    LOG.error("Problem configuring " + value, e);
                }
            } else if (name.equals(PROPERTY_FILE_PROPERTY)) {
                try {
                    File file = new File(value);
                    if (file.isAbsolute()) {
                        PropertyConfigurator.configure(value);
                    } else {
                        PropertyConfigurator.configure(appDir + File.separator + value);
                    }
                } catch (ProxoolException e) {
                    LOG.error("Problem configuring " + value, e);
                }
            } else if (name.equals(AUTO_SHUTDOWN_PROPERTY)) {
                autoShutdown = Boolean.valueOf(value).booleanValue();
            } else if (name.startsWith("jdbc")) { //
----------------解决方案--------------------------------------------------------
照着做。我正用着呢
----------------解决方案--------------------------------------------------------
回复 5楼 午夜屠猪男
成功了,谢谢,以后多多交流!!!!
----------------解决方案--------------------------------------------------------
  相关解决方案