1 // ==================== Program Description ==========================
2 // 程序名称:示例13-4 : BindDataSource.java
3 // 程序目的:注册JDBC 数据源
4 // ==============================================================
5 import com.inet.tds.TdsDataSource;
6 import java.util.Hashtable;
7 import javax.naming.*;
8 import javax.naming.directory.*;
9 import java.sql.* ;
10 import javax.sql.* ;
11
12 public class BindDataSource {
13
14 // 定义数据源参数
15 private String serverName = "persistentjava.com";
16 private int portNumber = 1433;
17 private String login = "java";
18 private String password = "sun";
19 private String databaseName = "jdbc";
20
21 private String filePath = "jdbc/datasource";
22
23 public RegDataSource()
24 {
25 // 创建哈希表用以传递参数
26 Hashtable env = new Hashtable();
27 env.put( Context.INITIAL_CONTEXT_FACTORY,
28 "com.sun.jndi.fscontext.RefFSContextFactory");
29
30 try {
31 // 创建初始上下文
32 Context ctx = new InitialContext(env);
33
34 // 创建实际数据源并设置参数
35 TdsDataSource ds = new TdsDataSource();
36
37 ds.setServerName(serverName);
38 ds.setPortNumber(portNumber);
39 ds.setDatabaseName(databaseName);
40 ds.setUser(login);
41 ds.setPassword(password);
42 ds.setDescription("JDBC DataSource Connection");
43
44 // 绑定数据源
45 ctx.bind(filePath, ds);
46 ctx.close();
47 }
48 catch (Exception ex) {
49 System.err.println(ex.getMessage());
50 }
51 }
52
53 public static void main(String args[]) {
54 new BindDataSource ();
55 }
56 }
|