编程语言

img bluesky35

[原创]在JAVA环境下建立数据库连接池

发表于2004/9/27 9:30:00  1541人阅读

分类: JAVA技术专栏

                                                                                   作者:bluesky35(蓝天)

开发环境: Windows2000 + Eclipse3.0 + Tomcat4.1.27 + Oracle9i

1.首先在Tomcat下进行配置
<Context path="/tomcattry" reloadable="true" docBase="D:/eclipse3/workspace/tomcattry" workDir="D:/eclipse3/workspace/tomcattry/work/org/apache/jsp">
  <Resource name="jdbc/wapOracle" auth="Container"
        type="javax.sql.DataSource"/>
  <ResourceParams name="jdbc/wapOracle">       
   <parameter>
     <name>factory</name>
     <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
   </parameter> 
    <parameter>
      <name>url</name>
      <value>jdbc:oracle:thin:@192.168.0.1:1521:oracle</value>  
    </parameter>
    <parameter>
      <name>maxIdle</name>
      <value>20</value>
    </parameter>
    <parameter>
      <name>maxActive</name>
      <value>10</value>
    </parameter>
    <parameter>
      <name>driverClassName</name>
      <value>oracle.jdbc.driver.OracleDriver</value>
    </parameter>
    <parameter>
      <name>maxWait</name>
      <value>1000</value>
    </parameter>
    <parameter>
      <name>removeAbandoned</name>
      <value>true</value>
    </parameter>
    <parameter>                     
      <name>username</name>          
      <value>abc</value>
    </parameter>
    <parameter>
      <name>logAbandoned</name>
      <value>true</value>
    </parameter>
    <parameter>
      <name>removeAbandonedTimeout</name>
      <value>60</value>
    </parameter>
    <parameter>
      <name>password</name>        
      <value>123</value>
    </parameter>
  </ResourceParams>
</Context>

其中maxActive为最大连接数

Servlet代码如下:
package Sampleservlet;

import java.io.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class servlet extends HttpServlet {

 protected void doPost(
  HttpServletRequest request,
  HttpServletResponse response)
  throws ServletException, IOException {

  //DataBean sample = new DataBean();
  String txtname = request.getParameter("txtname");
  String txtpassword = request.getParameter("txtpassword");
  PrintWriter out = response.getWriter();

     try{
   boolean loginSuccessd = false;
      String url_  = "192.168.0.1:1521:oracle";
      String id_ = "abc";
      String pass_ = "123";
      Connection conn = null;
      Statement stmt = null;
      
         Context initContext = new InitialContext();
         Context envContext = (Context) initContext.lookup("java:/comp/env");
         DataSource ds = (DataSource) envContext.lookup("jdbc/wapOracle");      
         conn = ds.getConnection();
   stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
   ResultSet res = stmt.executeQuery("select * from test");
   while(res.next()){
    String usr = res.getString("USERNAME");
    String pwd = res.getString("PASSWORD");
    if(usr!=null && usr.equals(txtname.toString()) && pwd.equals(txtpassword.toString())){
     loginSuccessd = true;
     break;
    }
   }
   
   if(loginSuccessd == true){
    out.println("Welcome!!!");
   }else{
    out.println("Username or Password is not correct");
   }
   //request.setAttribute("beanobject", sample);
     } catch (NamingException ne) {
            System.out.println("Initial DataSource error.");
            ne.printStackTrace();
        } catch (SQLException e) {
         System.out.println("ORACLE: " + e);
            e.printStackTrace();
            out.println("Connection Full!");
        }
    }
}

Bean中代码如下:
package Sampleservlet;

public class DataBean {
 private String username = "";
 private String password = "";
 
 public String getUserName(){
  return this.username;
 }

 public void setUserName(String username){
  this.username = username;
 }
 
 public String getPassword(){
  return this.password;
 }

 public void setPassword(String password){
  this.password = password;
 }
}

Comming.html如下:
<HTML>
<HEAD>
 <TITLE>Comming</TITLE>
 <META http-equiv="Content-Type" content="text/html; charset=shift_jis">
</HEAD>
<BODY>
<center>
Login
<form action="servlet" method="post">
 USERNAME  <input type="text" name="txtname" value="" />
 <br>
 PASSWORD  <input type="text" name="txtpassword" value="" />
 <br>
 <input type="submit" value="Login" />
</form>
</center>
</BODY>
</HTML>

Web.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "
http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
 <!-- Servlet -->
 <servlet>
  <servlet-name>servlet</servlet-name>
  <servlet-class>Sampleservlet.servlet</servlet-class>
 </servlet>

 <!-- Mapping -->
 <servlet-mapping>
  <servlet-name>servlet</servlet-name>
  <url-pattern>/servlet</url-pattern>
 </servlet-mapping>

  <resource-ref>
    <description>Oracle Datasource example</description>
    <res-ref-name>jdbc/wapOracle</res-ref-name>          
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

</web-app>
以上整个过程就是从comming.html中输入用户名和密码,然后调用servlet通过数据库连接池连接数据库进行验证,大家试一下吧^_^

阅读全文
0 0

相关文章推荐

img
取 消
img