程序中手工创建JNDI数据源的代码如下:
java 代码
- //创建JNDI DataSource,原文见 http://dev2dev.bea.com.cn/bbsdoc/20060831320.html
- private static void setDataSource() throws NamingException{
- if(ic==null){
- //Create initial context
- System.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.apache.naming.java.javaURLContextFactory");
- System.setProperty(Context.URL_PKG_PREFIXES,"org.apache.naming");
- ic = new InitialContext();
- // Construct BasicDataSource reference
- Reference ref = new Reference("javax.sql.DataSource","org.apache.commons.dbcp.BasicDataSourceFactory", null);
- ref.add(new StringRefAddr("driverClassName",SysParas.getPara("driverClassName")));
- ref.add(new StringRefAddr("url",SysParas.getPara("url")));
- ref.add(new StringRefAddr("username", SysParas.getPara("username")));
- ref.add(new StringRefAddr("password", SysParas.getPara("password")));
- ic.rebind(SysParas.getPara("jndiDs"), ref);
- }
- }
- //创建Connection
- public static Connection testConnection() throws NamingException {
- setDataSource();
- Connection con = null;
- try {
- Context initCtx = new InitialContext();
- DataSource ds = (DataSource) initCtx.lookup(SysParas.getPara("jndiDs"));
- con = ds.getConnection();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return con;
- }
xml 代码
- <?xml version="1.0" encoding="GBK"?>
- <sys_para>
- <!-- 系统参数配置文件 -->
- <para>
- <item code="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver" description=""/>
- <item code="url" value="jdbc:microsoft:sqlserver://192.168.2.18;databasename=ewebsafety;SelectMethod=cursor" description=""/>
- <item code="username" value="sa" description=""/>
- <item code="password" value="eweb" description=""/>
- <item code="jndiDs" value="safe" description="数据源名称"/>
- </para>
- </sys_para>
java 代码
- /**
- * @author HuangYong
- * To change the template for this generated type comment go to
- * Window>Preferences>Java>Code Generation>Code and Comments
- */
- public class SysParas {
- private static HashMap prop;
- private static long lastLoadTime;
- private static long interval = 1000 * 100;
- /**
- * 读取配置文件参数
- * @param paras
- * @return
- * @throws Exception
- */
- public static synchronized String getPara( String paras ) throws IllegalArgumentException {
- if( paras == null || paras.trim().length()==0 ){
- throw( new IllegalArgumentException ( "Parameter's value invalid." ) );
- }
- long currentTime = System.currentTimeMillis();
- if( (prop == null) || (currentTime-lastLoadTime)>interval ){
- reloadDom();
- }
- Object obj = prop.get( paras );
- if( obj != null ){
- //System.out.println(paras + " = "+(String)obj);
- return( (String)obj );
- }
- return( null );
- }
- private static synchronized void reloadDom() {
- if( prop == null ){
- prop = new HashMap();
- }
- SAXBuilder builder = new SAXBuilder();
- Document read_doc = null;
- try {
- ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
- InputStream in = classLoader.getResourceAsStream("sysConfig.xml");
- read_doc = builder.build(in);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (JDOMException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- Element rootElement = read_doc.getRootElement();
- List list = rootElement.getChildren("para");
- for (Iterator i = list.iterator(); i.hasNext();) {
- Element current = (Element) i.next();
- List item = current.getChildren("item");
- for (Iterator j = item.iterator(); j.hasNext();) {
- Element init = (Element) j.next();
- Attribute code = init.getAttribute("code");
- Attribute value = init.getAttribute("value");
- prop.put(code.getValue(),value.getValue());
- }
- }
- System.out.println("reading sysConfig.xml successfully...");
- lastLoadTime = System.currentTimeMillis();
- }
- public static void main(String [] args){
- System.out.println(SysParas.getPara("uploadSerialNo"));
- }
- }