struts数据库配置

 

首先在SERVERLIB中加入:commons-pool-1.2.jarcommons-dbcp-1.2.1.jarstruts-legacy.jar

然后

第一种:在STRUTS—CONFIG.xml中如此配置就可以了

加入SERVLET

<data-sources>

 <data-source type="org.apache.commons.dbcp.BasicDataSource">

    <set-property

      property="driverClassName"

      value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />

    <set-property

      property="url"

      value="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=test" />

    <set-property

      property="username"

      value="sa" />

    <set-property

      property="password"

      value="xiaohan" />

    <set-property

      property="maxActive"

      value="10" />

    <set-property

      property="maxWait"

      value="5000" />

    <set-property

      property="defaultAutoCommit"

      value="false" />

    <set-property

      property="defaultReadOnly"

      value="false" />

    <set-property

      property="validationQuery"

      value="SELECT COUNT(*) FROM [user]" />

</data-source>

 

 </data-sources>

 

 

第二种是如此用:在WEBXML中配置:

<servlet>

    <servlet-name>dbInit</servlet-name>

    <servlet-class>emptyprj.jdbc.util.DBInitServlet</servlet-class>

    <init-param>

      <param-name>driverClass</param-name>

      <param-value>com.microsoft.jdbc.sqlserver.SQLServerDriver</param-value>

    </init-param>

    <init-param>

      <param-name>jdbcURL</param-name>

      <param-value>jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=users</param-value>

    </init-param>

    <init-param>

      <param-name>minCount</param-name>

      <param-value>1</param-value>

    </init-param>

    <init-param>

      <param-name>maxCount</param-name>

      <param-value>10</param-value>

    </init-param>

    <load-on-startup>1</load-on-startup>

    <init-param>

      <param-name>user</param-name>

      <param-value>xiaohan</param-value>

    </init-param>

    <init-param>

      <param-name>password</param-name>

      <param-value>xiaohan</param-value>

    </init-param>

 </servlet>

这里还要写一个连接数据的接口:

有两个CLASS文件组成:第一个如下:

package emptyprj.jdbc.util;

 

import java.sql.Connection;

import java.sql.SQLException;

import javax.sql.DataSource;

 

public class ConnectionPool

{

 

    private DataSource ds;

    private static ConnectionPool mySelf;

 

   private ConnectionPool(DataSource datasource)

    {

        ds = datasource;

    }

 

    public static void init(DataSource datasource)

    {

        mySelf = new ConnectionPool(datasource);

    }

 

    public static ConnectionPool getInstance()

    {

        if(mySelf == null)

            throw new IllegalStateException("Pool not initialized.");

        else

            return mySelf;

    }

 

    public Connection getConnection()

        throws SQLException

    {

        return ds.getConnection();

    }

}

然后在文件中如此用就可以连了

request 就是HttpServletRequest

javax.sql.DataSource ds = null;

ds = getDataSource(request);

Connection  conn=ds.getConnection();

第二个:

package emptyprj.jdbc.util;

 

import java.sql.SQLException;

import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import org.apache.struts.util.GenericDataSource;

 

// Referenced classes of package emptyprj.jdbc.util:

//            ConnectionPool

 

public class DBInitServlet extends HttpServlet

{

 

    public DBInitServlet()

    {

    }

 

    public void init(ServletConfig servletconfig)

        throws ServletException

    {

        super.init(servletconfig);

        try

        {

            GenericDataSource genericdatasource = new GenericDataSource();

            genericdatasource.setDriverClass(getInitParameter("driverClass"));

            genericdatasource.setUrl(getInitParameter("jdbcURL"));

            genericdatasource.setMinCount(Integer.parseInt(getInitParameter("minCount")));

            genericdatasource.setMaxCount(Integer.parseInt(getInitParameter("maxCount")));

            genericdatasource.setAutoCommit(false);

            genericdatasource.setUser(getInitParameter("user"));

            genericdatasource.setPassword(getInitParameter("password"));

            genericdatasource.open();

            ConnectionPool.init(genericdatasource);

        }

        catch(SQLException sqlexception)

        {

            sqlexception.printStackTrace();

            throw new ServletException("Unable to open datasource.");

        }

    }

}

然后在文件中如此用就可以连了

Connection connection = ConnectionPool.getInstance().getConnection();

posted on 2006-04-11 21:26  freeliver54  阅读(1318)  评论(2编辑  收藏

导航