⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 initializejndi.java

📁 本套光盘提供了本书各章实例的所需的部分源程序文件以及数据库文件。读者 需要使用JDK 1.4(必需)版
💻 JAVA
字号:
// We need to import the actual DataSource implementation 
import com.inet.tds.TdsDataSource;
import java.util.Hashtable;
import javax.naming.*;
import javax.naming.directory.*;
import java.sql.* ;
import javax.sql.* ;

public class InitializeJNDI {
// First we define the relevant parameters for this datasource
private String serverName = "localhos";
private int portNumber = 1433;
private String login = "javadb";
private String password = "javadb";
private String databaseName = "Northwind";

/*This is the name we will assign to our datasource. Because we are using the file system provider, our name follows the file system naming rules. The JNDI reserved subcontext for JDBC applications is jdbc, thus our name starts appropriately.*/
private String filePath = "ProgramFiles/Javadb";
public InitializeJNDI() {
// To pass in the necessary parameters, we need to create and then populate a Hashtable.
Hashtable env = new Hashtable();
 env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
try {
// Create the initial context
   Context ctx = new InitialContext(env);
// Here we create the actual DataSource and then set the relevant // parameters.
    TdsDataSource ds = new TdsDataSource();

 		ds.setServerName(serverName);
        ds.setPortNumber(portNumber);
        ds.setDatabaseName(databaseName);
        ds.setUser(login);
        ds.setPassword(password);
        ds.setDescription("JDBC DataSource Connection");
// Now we bind the DataSource object to the name we selected earlier.
       ctx.bind(filePath, ds);
       ctx.close();
// Generic Exception handler, in practice, this would be replaced by an 
// appropriate Exception handling hierarchy.
   } catch (Exception ex) {
            System.err.println("ERROR: " + ex.getMessage());
        }
    }
    public static void main(String args[]) {
        new InitializeJNDI();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -