1. 程式人生 > >給出一個JNDI用來查找dataSource的例子?

給出一個JNDI用來查找dataSource的例子?

alc chapter 視頻下載 user conn vax ref com ktr

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

給出一個JNDI用來查找dataSource的例子?