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

2018-09-29 04:02:41来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Mybatis源码解析,一步一步从浅入深(七):执行查询

下一篇:SpringBoot+MyBatis+MySQL读写分离