import java.sql.*;
import java.util.*;
import javax.naming.*;
import javax.sql.DataSource;

public class SqlAccess {

    //数据库连接
    public Connection conn = null;
    //结果集
    private ResultSet rs = null;
    //数据库访问段
    public Statement stmt = null;
    //是否连接数据库
    private boolean isConnDB = false;
    private boolean isDefault = true;
    private String newDSName = null;
    public static HashMap logmap = new HashMap();
    public static int begincont = 0;
    private int count = 0;
    public String serverType="";
    public String jndiName = "";

    public static final String WLS_INITIAL_CONTEXT_FACTORY =  "weblogic.jndi.WLInitialContextFactory";
    public static final String TBJBOSS_INITIAL_CONTEXT_FACTORY = "org.jboss.naming.JNDIView";
    public static final String WEBSPHERE_INITIAL_CONTEXT_FACTORY = "com.ibm.websphere.naming.WsnInitialContextFactory";

    public SqlAccess() {
        try {
            initial();
            conn.setAutoCommit(true);
        }
        catch (Exception exp) {
        }
    }
    public SqlAccess(String jndi,String serverType) {
        this.serverType = serverType;
        this.jndiName = jndi;
        try {
            initial();
            conn.setAutoCommit(true);
        }
        catch (Exception exp) {
        }
    }

    public static SqlAccess createConn(String ds) throws Exception {
        SqlAccess sq = new SqlAccess(ds,"websphere");
        if (sq == null || sq.conn == null)
            throw new Exception("Errorx");
        return sq;
    }

    public static SqlAccess createConn() throws Exception {
        return createConn("");
    }

    public static void main(String[] args) {

    }

    public static final String ST_WEBSPHERE = "websphere";
    public static final String ST_WEBLOGIC = "weblogic";
    public static final String ST_TOMCAT = "tomcat";
    public static final String ST_JBOSS = "jboss";
    public static final String ST_RESIN = "resin";

    public void initial() throws SQLException {
        try {
            Context ctx = null;
            DataSource ds = null;
            String dsName = this.jndiName;
            try {
                count = ++begincont;
            }
            catch (Exception e) {
                begincont = 0;
                count = ++begincont;
            }

            System.out.println("[count]"+count);

            if (ST_WEBLOGIC.equals(serverType)) {
                Properties props = new Properties();
                //取数据库连接
                props.put("weblogic.codeset", "GBK");
                Hashtable parms = new Hashtable();
                parms.put(    Context.INITIAL_CONTEXT_FACTORY,    this.WLS_INITIAL_CONTEXT_FACTORY);
                ctx = new InitialContext(parms);
            }
            else if (ST_JBOSS.equals(serverType)) {
                System.out.println("Jboss connection init");
                ctx = new InitialContext();
                dsName = "java:/" + dsName;
            }
            else if (ST_RESIN.equals(serverType)) {
                System.out.println("Resin Connection init");
                ctx = new InitialContext();
                dsName = "java:comp/env/" + dsName;
            }
            else if (ST_WEBSPHERE.equals(serverType)) {
                System.out.println("Websphere Connection init");
                Hashtable prop = new Hashtable();
                prop.put(Context.INITIAL_CONTEXT_FACTORY,this.WEBSPHERE_INITIAL_CONTEXT_FACTORY);
                ctx = new InitialContext(prop);
            }
            else if (ST_TOMCAT.equals(serverType)) {
                System.out.println("Tomcat JNDI Connection init");
                ctx = new InitialContext();
                dsName = "java:comp/env/" + dsName;
            }

            System.out.println("[SqlAccess]dsName:" + dsName + "...");

            ds = (DataSource) ctx.lookup(dsName);
            conn = ds.getConnection();

            //--System.out
            System.out.println("conn:" + conn.toString());
            logmap.put(conn,count+"");
            //--System.out
            System.out.println("ConnectionPool:making a connection...[" + count + "]");
            //--System.out
            //--System.out
            //stmt = conn.createStatement();
            isConnDB = true;

        }
        catch (SQLException sqle) {
            sqle.printStackTrace();
            System.out.println("SQLException during connection(): " + sqle.getMessage());
            throw sqle;
        }
        catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }

    //设置AutoCommit
    public void setAutoCommit(boolean autoCommit) throws SQLException {
        try {
            if (isConnDB)
                conn.setAutoCommit(autoCommit);
        }
        catch (SQLException sqle) {
            System.out.println("SQLException during autoCommit(): " + sqle.getMessage());
            throw sqle;
        }
        catch (Exception e) {
            System.out.println(e.getMessage());

        }
    }

    //执行Commit命令
    public void commit() throws SQLException {
        try {
            if (isConnDB)
                conn.commit();
        }
        catch (SQLException sqle) {
            System.out.println("SQLException during commit(): " + sqle.getMessage());
            throw sqle;
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    //执行Rollback命令
    public void rollback() {
        try {
            if (isConnDB)
                conn.rollback();
        }
        catch (SQLException sqle) {
            System.out.println("SQLException during rollback(): " + sqle.getMessage());
            //throw sqle;
        }
        catch (Exception e) {
            System.out.println(e.getMessage());

        }
    }

    //查询与数据库之间的连接是否关闭,true - 有连接:false - 没连接
    public boolean isConnectDB() {
        return isConnDB;
    }

    //关闭连接
    public void close() {

        try {

            if (conn != null)
                conn.close();
            logmap.remove(conn);
            logmap.remove("" + count);
            System.out.println("ConnectionPool::Closing connection...[" + count + "]\n");
        }
        catch (SQLException sqle) {
            System.out.println("SQLException during close(): " + sqle.getMessage());
            //throw sqle;
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    //public static String SERVER="JBOSS";
    //public static String SERVER="WEBLOGIC";
    /**
     * Returns the logmap.
     * @return HashMap
     */
    public static HashMap getLogmap() {
        return logmap;
    }

    /**
     * Returns the count.
     * @return int
     */
    public int getCount() {
        return count;
    }

}

使用此方法获取连接,需要从容器中配置连接池

调用方法演示:

    SqlAccess sa = new SqlAccess("HIS", "websphere");
    if (sa != null) {
        System.out.println("成功连接=" + sa.conn);
    } else {
        System.out.println("失败连接=" + sa.conn);
    }
    Connection hisConn = sa.conn;

最新文章

  1. 使用 NuGet 下载最新的 Rafy 框架及文档
  2. PHP 做文件校验,MD5,CRC32,SHA等
  3. iOS按钮设置图片在上文字在下
  4. SQL Server:查看SQL日志文件大小命令:dbcc sqlperf(logspace)
  5. Linux ACL管理详解
  6. Javascript 数组之判断取值和数组取值
  7. Go support for Android
  8. Linux中断处理体系结构分析
  9. android 65 文件访问权限
  10. TCP实现P2P通信
  11. ajax跨域请求解决方案
  12. JDBC数据库编程
  13. Java JVM里堆和栈的区别
  14. scp传输文件,自动填充密码
  15. 技术人员在小公司成长 vs 大公司成长路径和建议
  16. 初识HTTPS
  17. 小程序url传参如何写变量
  18. linux下通过curl访问web服务器
  19. 【Java】【THINK】
  20. LocationActivity

热门文章

  1. web项目由简入繁
  2. HashMap在并发场景下踩过的坑
  3. HBase 数据的多版本特性潜在的意外
  4. 「日常训练」Uncle Tom's Inherited Land*(HDU-1507)
  5. APP性能测试工具-GT(随身调)
  6. GIT: 分布式开发 代码管理工具使用命令大全
  7. 幸运的袋子(深度优先遍历(Depth First Search,DFS))
  8. solidity事件详解
  9. 4.安装hive
  10. [C++] Variables and Basic Types