connectionfactory.java

来自「JAVA EXCEL操作API」· Java 代码 · 共 128 行

JAVA
128
字号
/*
 * Created on 2004-3-27
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package com.zosatapo.xls.util;

/**
 * @author zosatapo
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
import java.sql.*;
import javax.sql.*;
import javax.naming.*;

public final class ConnectionFactory
{ 
  /**
   * default datasource jndi name
   */
  public final static String DEFAULT_DATASOURCE_JNDI="dsWangll";  //^_^ my honey  
  
  public final static String TYPE_DRIVER_MANAGER  =  "DriverManager";
  public final static String TYPE_DATA_SOURCE     =  "DataSource";
  
  public final static int CONN_DATESOURCE          = 1; 
  public final static int CONN_DATESOURCE_LOGIN    = CONN_DATESOURCE+1; 
  public final static int CONN_DRIVERMANAGER       = CONN_DATESOURCE_LOGIN+1; 
  public final static int CONN_DRIVERMANAGER_LOGIN = CONN_DRIVERMANAGER+1;
  public final static int CONN_DEFAULT             = CONN_DRIVERMANAGER_LOGIN+1;
  
  /** ConnectionFactory private constructor*/
  private ConnectionFactory(){}
  
  public static Connection getConnection(String url)
  throws SQLException
  {
    if(url==null || url.trim().length()==0)
    {
      throw new SQLException("Database connection string must not be null .");
    }
    
    return DriverManager.getConnection(url);
  }
  
  public static Connection getConnection(String url,String user,String pwd)
  throws SQLException
  {   
    if(url==null || url.trim().length()==0)
    {
      throw new SQLException("Database connection string must not be null .");
    }
    
    return DriverManager.getConnection(url,user,pwd);
  } 
  
  public static Connection getConnectionByJDNI(String name)
  throws SQLException
  {
    if(name==null || name.length()==0)
    {
      throw new SQLException("DataSource JNDI name must not be null .");
    }
    
    DataSource ds =null;    
    try
    { 
      InitialContext ctx=InitialCtxFactory.getInitialContext();
      ds = (DataSource)ctx.lookup(name);
    }
    catch(Exception ex)
    {
      throw new SQLException(ex.toString());
    }
    
    return ds.getConnection();
  } 
   
  public static Connection getConnectionByJDNI(String name,String user,String pwd)
  throws SQLException
  {
    if(name==null || name.length()==0)
    {
      throw new SQLException("DataSource JNDI name must not be null .");
    }
    
    DataSource ds =null;    
    try
    { 
      Context ctx= InitialCtxFactory.getInitialContext();
      ds = (DataSource)ctx.lookup(name);
    }
    catch(Exception ex)
    {
      throw new SQLException(ex.toString());
    }

    return ds.getConnection(user,pwd);
  }
}

class InitialCtxFactory
{
  private static InitialContext initialCtx = null;
    
  public static synchronized InitialContext getInitialContext() 
  throws NamingException
  {
    if(initialCtx!=null) 
    {
      return initialCtx;
    }
    else 
    {
      initialCtx = new InitialContext();
      return initialCtx;
    }
  }
  
  public static InitialContext newInitialContext() 
  throws NamingException
  {
    return new InitialContext();
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?