请教一个问题,Java中使用连接池操作数据库的步骤,如何配置db.properties文件,怎么做啊,尽量具体些

解决方案 »

  1.   

    你都没说是用的什么连接池啊。连接池很多啊。配置也各种各样,不过一般也就那么些,自己看看就知道了。数据库驱动、连接url、用户名、密码、最大活动连接数等等。
      

  2.   

    db.properties文件内容:
    jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/test
    jdbc.username=xxxx
    jdbc.password=xxxxxx接着在数据库操作类中使用如下语句进行调用即可:
    public class DBConnection {
    public Connection getConnection() throws Exception {
    InputStream is = this.getClass().getClassLoader().getResourceAsStream("database.properties");
    Properties property = new Properties();
    property.load(is);
    String driverClassName = property.getProperty("jdbc.driverClassName");
    String url = property.getProperty("jdbc.url");
    String username = property.getProperty("jdbc.username");
    String password = property.getProperty("jdbc.password");
    Connection conn = null;
    try {
    Class.forName(driverClassName);
    conn = DriverManager.getConnection(url, username, password);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return conn;记得调用java.util.Properties包。
    不知道是否符合你的第二个问题。呵呵
      

  3.   

        
        <parameter>   
          <name>url</name>   
          <value>jdbc:jtds:sqlserver://localhost:1433;DatabaseName=pubs</value>   
        </parameter>   
        <parameter>   
          <name>driverClassName</name>   
          <value>net.sourceforge.jtds.jdbc.Driver</value>   
        </parameter>   
        <parameter>   
          <name>username</name>   
          <value>sa</value>   
        </parameter>   
        <parameter>   
          <name>password</name>   
          <value>sa</value>   
        </parameter>
      

  4.   

    用开源的proxool这人连接池,比较好.
      

  5.   

    如果你是要配置Tomcat连接池的话可以参考下:
    以连接Oracle数据库为例:新建一个web工程先在Tomcat的context.xml文件下添加如下内容:
     <Resource name="jdbc/myoracle" auth="Container"
                  type="javax.sql.DataSource" driverClassName="oracle.jdbc.driver.OracleDriver"
                  url="jdbc:oracle:thin:@localhost:1521:orcl"
                  username="scott" password="tiger" maxActive="20" maxIdle="10"
                  maxWait="-1"/> 
    新建一个Servlet,在doPost()方法里添加内容如下:
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { response.setContentType("text/html");
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
     Context initContext;
     PrintWriter out=response.getWriter();
    try {
    initContext = new InitialContext();
     Context envContext  = (Context)initContext.lookup("java:/comp/env");
     DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
     Connection conn = ds.getConnection();
     Statement st=conn.createStatement();
     ResultSet rs = st.executeQuery("select * from emp");
     while(rs.next()){
     out.print(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)+
     " "+rs.getString(4)+" "+rs.getDate(5)+" "+rs.getString(6)
     +" "+rs.getString(7)+" "+rs.getInt(8)+"<br>");
     }
     
    } catch (NamingException e) {
    e.printStackTrace();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }最后在web.xml里添加如下内容:
    <resource-ref>
    <description>Oracle Datasource example</description>
    <res-ref-name>jdbc/myoracle</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    </resource-ref>启动Tomcat
    http://localhost:8080/docs/jndi-datasource-examples-howto.html
    这里有相关说明
      

  6.   

    第一要在src文件下也就是类路径下创建属性文件,db.properties.jdbc.driverClassName=com.mysql.jdbc.Driver 
    jdbc.url=jdbc:mysql://localhost:3306/test 
    jdbc.username=xxxx 
    jdbc.password=xxxxxx 第二,也是在类路径下,建xml文件,<?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
    <sqlMapConfig>
      <properties resource="db.properties" />
      <transactionManager type="JDBC">
        <dataSource type="SIMPLE">
          <property name="JDBC.Driver" value="${driver}"/>
          <property name="JDBC.ConnectionURL" value="${url}"/>
          <property name="JDBC.Username" value="${username}"/>
          <property name="JDBC.Password" value="${password}"/>
        </dataSource>
      </transactionManager>
      <sqlMap resource="com/ibatis/User.xml" />
    </sqlMapConfig>那要看你要做什么用啦,