java直接访问JNDI工具代码
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;
java直接访问JNDI工具代码的更多相关文章
- java获取时间整点工具代码
/**获取上n个小时整点小时时间 * @param date * @return */ public static String getLastHourTime(Date date,int n){ C ...
- 使用poco 的NetSSL_OpenSSL 搭建https 服务端,使用C++客户端,java 客户端访问,python访问(python还没找到带证书访问的代码.)
V20161028 由于项目原因,需要用到https去做一些事情. 这儿做了一些相应的研究. 这个https 用起来也是折腾人,还是研究了一周多+之前的一些积累. 目录 1,java client 通 ...
- java服务器访问其他服务器工具类编写
java服务器访问其他服务器工具类编写适合各种消息推送及微服务交互 package com.xiruo.medbid.components; import com.xiruo.medbid.util. ...
- 【JAVA系列】使用JavaScript实现网站访问次数统计代码
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[JAVA系列]使用JavaScript实现网站 ...
- Java中常用的设计模式代码与理解
Java中常用的设计模式代码与理解 一.单例模式 1.饿汉式 (太饿了,类加载的时候就创建实例) /** * 饿汉式单例模式 */ public class HungrySingleInstance ...
- Java并发包同步工具之Exchanger
前言 承接上文Java并发包同步工具之Phaser,讲述了同步工具Phaser之后,搬家博客到博客园了,接着未完成的Java并发包源码探索,接下来是Java并发包提供的最后一个同步工具Exchange ...
- Java安全之JNDI注入
Java安全之JNDI注入 文章首发:Java安全之JNDI注入 0x00 前言 续上篇文内容,接着来学习JNDI注入相关知识.JNDI注入是Fastjson反序列化漏洞中的攻击手法之一. 0x01 ...
- Java线程的并发工具类
Java线程的并发工具类. 一.fork/join 1. Fork-Join原理 在必要的情况下,将一个大任务,拆分(fork)成若干个小任务,然后再将一个个小任务的结果进行汇总(join). 适用场 ...
- java性能优化常用工具jmap、jstack
jmap:java内存映像工具 jmap用于生成堆转储快照,比较常用的option包括-heap,-histo,-dump [root@localhost script]# jmap -h Usage ...
随机推荐
- Fliptil_KEY
Fliptil(fliptile.pas/c/cpp) [问题描述] 约翰知道,那些高智力又快乐的奶牛产奶量特别高.所以他做了一个翻瓦片的益智游戏来娱乐奶牛. 在一个M×N的骨架上,每一个格子里都有一 ...
- 成都Uber优步司机奖励政策(2月7日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- ORB-SLAM(八)ORBmatcher 特征匹配
该类负责特征点与特征点之间,地图点与特征点之间通过投影关系.词袋模型或者Sim3位姿匹配.用来辅助完成单目初始化,三角化恢复新的地图点,tracking,relocalization以及loop cl ...
- 海思NB-IOT的RA功能
就带ReleaseAssistance标志的特殊发送指令,发送数据的时候会向网络侧请求立即释放RRC进入Idle态. 降低20秒50mA连接态的能量消耗.
- c++ singleton
http://www.yolinux.com/TUTORIALS/C++Singleton.html
- Kotlin的密封(Sealed)类:超强的枚举(KAD 28)
作者:Antonio Leiva 时间:Jun 27, 2017 原文链接:https://antonioleiva.com/sealed-classes-kotlin/ Kotlin的封装类是Jav ...
- Selenium安装(二)
安装python 安装Selenium之前首先来说一下Python,python是一门动态性语言,python的编写比较灵活,简洁,开发效率高.因此以python结合selenium来进行自动化测试. ...
- Shader-水流效果
效果图:(贴图类似于泥石流) 代码: Shader "CookbookShaders/Chapter02/ScrollingUVs" { Properties { _MainTin ...
- Spring Boot下的lombok安装 (日志) 不能识别log变量问题
参考地址:http://blog.csdn.net/blueheart20/article/details/52909775 ps:除了要加载依赖之外 还要安装lombok插件
- Spring Cloud(十):服务网关 Zuul(路由)【Finchley 版】
Spring Cloud(十):服务网关 Zuul(路由)[Finchley 版] 发表于 2018-04-23 | 更新于 2018-05-09 | 通过之前几篇 Spring Cloud 中 ...