欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

给出一个JNDI用来查找dataSource的例子?

程序员文章站 2022-05-30 09:28:47
8.JNDI用来查找dataSource的例子:(视频下载) (全部书籍) import javax.naming.InitialContext;import javax.naming.Context;import com.mysql.jdbc.jdbc2.optional.MysqlConnect ......

8.jndi用来查找datasource的例子: 

import javax.naming.initialcontext;
import javax.naming.context;

import com.mysql.jdbc.jdbc2.optional.mysqlconnectionpooldatasource;

import java.util.properties;
import net.sourceforge.jtds.jdbcx.*;
public class classput {
        public static void main(string a[]) {
                try {
/*                  jtdsdatasource datasource=new  jtdsdatasource();
                        datasource.setservername("localhost");
                        datasource.setdatabasename("northwind");
                        datasource.setuser("sa");
                        datasource.setpassword("1234");
*/
                    mysqlconnectionpooldatasource ds = new mysqlconnectionpooldatasource();
                    ds.seturl("jdbc:mysql://localhost:3306/test");
                    ds.setuser("root");
                    ds.setpassword("1234");                     
                       
                       
                        properties prop = new properties();
                        prop.setproperty(context.initial_context_factory,
                                        "com.sun.jndi.fscontext.reffscontextfactory");

                        context ctx=new initialcontext(prop);
/*here the following statement writes a file .bindings under the f disk. because this project is under the f disk.*/
                        ctx.rebind("abc",ds);


                } catch (exception e) {
                        e.printstacktrace();
                }
        }
}
上面的程序只是存入硬盘,想查找出来得用下面的程序:

import javax.naming.initialcontext;
import javax.naming.context;

import com.mysql.jdbc.jdbc2.optional.mysqlconnectionpooldatasource;

详情请见:http://www.mark-to-win.com/index.html?content=javabeginner/javaurl.html&chapter=javabeginner/javabeginner10_web.html#jndifinddatasource