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