---
title: "调用weblogic9数据源代码"
author: "hcyoo"
date: "2009-11-30T16:01:30Z"
tags: []
slug: "weblogic9"
draft: false
lastmod: "2009-12-01T16:40:09Z"
id: 537404
url: "https://hcyoo.iteye.com/blog/537404"
---

```java

package lhm.hcy.frameset.db;

import java.sql.*;
import java.util.*;
import javax.naming.*;
import java.util.ResourceBundle;

/**
 * <p>Title: Connection public class</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2008</p>
 *
 * <p>Company: </p>
 *
 * @author Deepblue 2009-11-20 
 * @version 1.0
 */

public abstract class Conndb {
 String LOCATION_URL=null;
  static String JNDI_DS_NAME=null;
  ResourceBundle bundle = null;
  private static Hashtable ht = null;

  private Hashtable getHt(){
    if (ht == null) {
      ht = new Hashtable();
      if (bundle == null) {
//JAVA读取配置文件, lhm.hcy.frameset.db 为properties文件的路径,jndi为文件名,后缀名可省略
        bundle = ResourceBundle.getBundle("lhm.hcy.guge.frameset.db.jndi");
        this.LOCATION_URL = bundle.getString("url");
        this.JNDI_DS_NAME = bundle.getString("datasourcename");
      }
      ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
      ht.put(Context.PROVIDER_URL,this.LOCATION_URL);
    }
    return ht;
  }

    protected Connection conn = null;
    public void execute() throws SQLException {
      Context ctx = null;

        try {
          ctx = new InitialContext(getHt());
          javax.sql.DataSource ds = (javax.sql.DataSource)ctx.lookup(this.JNDI_DS_NAME);
          conn = ds.getConnection();
            conn.setAutoCommit(false);
            myexecute();
            conn.commit();
        } catch(SQLException sex){
          sex.printStackTrace();
        }catch (Exception ex) {
          ex.printStackTrace();
        } finally {
            if (conn!=null){
              conn.close();conn=null;
            }
        }
    }

    /**
     * myexecute
     */
    public abstract void myexecute() throws SQLException;
}

```


============================================
jndi.properties文件如下:

url=t3:127.0.0.1:80
datasourcename=myDS