java当中JDBC当中JNDI用来查找dataSource的例子

[学习笔记]

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;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
import net.sourceforge.jtds.jdbcx.*;
public class ClassGet {
        public static void main(String a[]) {

                try {
                        Properties prop = new Properties();
                        prop.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                                        "com.sun.jndi.fscontext.RefFSContextFactory");
                        Context ctx=new InitialContext(prop);

文章转载自原文:https://blog.csdn.net/qq_43650923/article/details/100652625

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...