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 ...
随机推荐
- Visual studio 2010 TFS地址解析,让团队资源管理器不再显示IP地址
第一步: 找到名为hosts的配置文件(路径C:\Windows\System32\drivers\etc\hosts)用记事本打开并写入需要的配置,例如我用到的是TFS服务器的IP地址为192.16 ...
- 安装wamp后,其显示目录的图标显示不出来
解决办法:wamp的安装目录中,到 wamp\bin\apache\Apache2.2.21\conf \extra下打开httpd-autoindex.conf文件,这里是索引文件图标的配置文件.修 ...
- Spring 配置String转Date
操作步骤: 1. 实现 org.springframework.core.convert.converter.Converter 接口 2. 配置 org.springframework.contex ...
- vue中如何实现pdf文件预览?
今天产品提出一个优化的需求,就是之前我们做的图片展示就是一个img标签搞定,由于我们做的是海外后台管理系统,那边的人上传的文件时pdf格式,vue本事是不支持这种格式文件展示的,于是就google搜索 ...
- lintcode539 移动零
移动零 给一个数组 nums 写一个函数将 0 移动到数组的最后面,非零元素保持原数组的顺序 注意事项 1.必须在原数组上操作2.最小化操作数 您在真实的面试中是否遇到过这个题? Yes 样例 给出 ...
- 【转】VSstudio中的一些宏
说明 $(RemoteMachine) 设置为“调试”属性页上“远程计算机”属性的值.有关更多信息,请参见更改用于 C/C++ 调试配置的项目设置. $(References) 以分号分隔的引用列表被 ...
- POJ 2184 Cow Exhabition
"Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - Cows with G ...
- Attention注意力机制介绍
什么是Attention机制 Attention机制通俗的讲就是把注意力集中放在重要的点上,而忽略其他不重要的因素.其中重要程度的判断取决于应用场景,拿个现实生活中的例子,比如1000个人眼中有100 ...
- Block的声明与定义语法
Block的声明 Block的声明与函数指针的声明类似 返回值类型(^变量名)(参数列表) Block的定义 ^返回值类型(参数列表) { 表达式 } 其中: 1 如果返回值类型是void,可以省略 ...
- 图的遍历——DFS(邻接矩阵)
递归 + 标记 一个连通图只要DFS一次,即可打印所有的点. #include <iostream> #include <cstdio> #include <cstdli ...