OCCI连接服务器 : 数据库 « Servlets « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » Servlets » 数据库屏幕截图 
OCCI连接服务器

/*

Java Programming with Oracle JDBC
by Donald Bales 
ISBN: 059600088X
Publisher: O'Reilly

*/

import oracle.jdbc.pool.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.Vector;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.ConnectionPoolDataSource;

public class OCCIConnectionServlet extends HttpServlet {

  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Oracle Cached Connection "
        "Implementation Test Servlet</title>");
    out.println("</head>");
    out.println("<body>");

    // let's turn on verbose output
    OCCIConnection.setVerbose(true);
    // now let's get a cached connection
    Connection connection = OCCIConnection.checkOut();

    Statement statement = null;
    ResultSet resultSet = null;
    String userName = null;
    try {
      // test the connection
      statement = connection.createStatement();
      resultSet = statement
          .executeQuery("select initcap(user) from sys.dual");
      if (resultSet.next())
        userName = resultSet.getString(1);
    catch (SQLException e) {
      out.println("DedicatedConnection.doGet() SQLException: "
          + e.getMessage() "<p>");
    finally {
      if (resultSet != null)
        try {
          resultSet.close();
        catch (SQLException ignore) {
        }
      if (statement != null)
        try {
          statement.close();
        catch (SQLException ignore) {
        }
    }

    // let's add a little delay so we can force
    // multiple connections in the connection cache
    for (int o = 0; o < 3; o++) {
      for (int i = 0; i < 2147483647; i++) {
      }
    }

    // let's return the conection
    OCCIConnection.checkIn(connection);

    out.println("Hello " + userName + "!<p>");
    out.println("You're using an Oracle Cached "
        "Connection Implementation connection!<p>");
    out.println("</body>");
    out.println("</html>");
  }

  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
    doGet(request, response);
  }
}

class OCCIConnection {
  private static boolean verbose = false;

  private static int numberImplementations = 0;

  private static Vector cachedImplementations = new Vector();

  public static synchronized Connection checkOut() {
    return checkOut("Database");
  }

  public static synchronized Connection checkOut(String baseName) {
    boolean found = false;
    OracleConnectionCacheImpl cached = null;
    Connection connection = null;
    if (verbose) {
      System.out.println("There are "
          + Integer.toString(numberImplementations)
          " connections in the cache");
      System.out.println("Searching for a matching implementation...");
    }
    for (int i = 0; !found && i < numberImplementations; i++) {
      if (verbose) {
        System.out.println("Vector entry " + Integer.toString(i));
      }
      cached = (OracleConnectionCacheImplcachedImplementations.get(i);
      if (cached.getDescription().equals(baseName)) {
        if (verbose) {
          System.out.println("found cached entry "
              + Integer.toString(i" for " + baseName);
        }
        found = true;
      }
    }
    if (!found) {
      if (verbose) {
        System.out.println("Cached entry not found ");
        System.out.println("Allocating new entry for " + baseName);
      }
      try {
        cached = new OracleConnectionCacheImpl(
            getConnectionPoolDataSource(baseName));
        cached.setDescription(baseName);
        cachedImplementations.add(cached);
        numberImplementations++;
      catch (SQLException e) {
        System.err.println(e.getMessage()
            " creating a new implementation for " + baseName);
      }
    }
    if (cached != null) {
      try {
        connection = cached.getConnection();
      catch (SQLException e) {
        System.err.println(e.getMessage() " getting connection for "
            + baseName);
      }
    }
    return connection;
  }

  public static ConnectionPoolDataSource getConnectionPoolDataSource(
      String baseName) {
    Context context = null;
    ConnectionPoolDataSource cpds = null;
    try {
      Properties properties = new Properties();
      properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
          "com.sun.jndi.fscontext.RefFSContextFactory");
      properties.setProperty(Context.PROVIDER_URL, "file:/JNDI/JDBC");
      context = new InitialContext(properties);
      cpds = (ConnectionPoolDataSourcecontext.lookup(baseName);
    catch (NamingException e) {
      System.err.println(e.getMessage() " creating JNDI context for "
          + baseName);
    }
    return cpds;
  }

  protected static synchronized void checkIn(Connection c) {
    try {
      c.close();
    catch (SQLException e) {
      System.err.println(e.getMessage() " closing connection");
    }
  }

  public static String[] getReport() {
    int line = 0;
    String[] lines = new String[numberImplementations * 7];
    OracleConnectionCacheImpl cached = null;

    for (int i = 0; i < numberImplementations; i++) {
      cached = (OracleConnectionCacheImplcachedImplementations.get(i);
      lines[line++= cached.getDescription() ":";
      switch (cached.getCacheScheme()) {
      case OracleConnectionCacheImpl.DYNAMIC_SCHEME:
        lines[line++"Cache Scheme  = DYNAMIC_SCHEME";
        break;
      case OracleConnectionCacheImpl.FIXED_RETURN_NULL_SCHEME:
        lines[line++"Cache Scheme  = FIXED_RETURN_NULL_SCHEME";
        break;
      case OracleConnectionCacheImpl.FIXED_WAIT_SCHEME:
        lines[line++"Cache Scheme  = FIXED_WAIT_SCHEME";
        break;
      }
      lines[line++"Minimum Limit = "
          + Integer.toString(cached.getMinLimit());
      lines[line++"Maximum Limit = "
          + Integer.toString(cached.getMaxLimit());
      lines[line++"Cache Size    = "
          + Integer.toString(cached.getCacheSize());
      lines[line++"Active Size   = "
          + Integer.toString(cached.getActiveSize());
      lines[line++" ";
    }
    return lines;
  }

  public static void setVerbose(boolean v) {
    verbose = v;
  }

}

           
       
Related examples in the same category
1. Servlets数据库查询
2. 在Servlets中使用JDBC
3. 缓存连接服务器
4. 交易连接服务器
5. 会话登录的JDBC
6. JDBC和Servlet
7. 数据库和服务器:数据库数据
8. 数据库和服务器:存储过程
9. 数据库交易
10. 典型的数据库命令
11. Process a raw SQL query; use ResultSetMetaData to format it
12. See Account
13. Guest Book Servlet
14. Dedicated Connection Servlet
15. 登录Servlets
16. Get Column Names From ResultSet
17. Display Clob Servlet
18. 从服务器中删除blob
19. 从Servlet删除Clob
20. 显示 blob Servlet
21. 在servlet中从Oracle删除Clob
22. 插入Clob到MySQL服务器
23. 从servlet更新Clob MySQL中存储的数据
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.