binddatasource.java
来自「精通java核心技术》随书源代码」· Java 代码 · 共 58 行
JAVA
58 行
// ==================== Program Discription ==========================
// 程序名称:示例13-4 : BindDataSource.java
// 程序目的:注册JDBC 数据源
// ==============================================================
import com.inet.tds.TdsDataSource;
import java.util.Hashtable;
import javax.naming.*;
import javax.naming.directory.*;
import java.sql.* ;
import javax.sql.* ;
public class BindDataSource {
// define the parameters for the datasource
private String serverName = "persistentjava.com";
private int portNumber = 1433;
private String login = "java";
private String password = "sun";
private String databaseName = "jdbc";
private String filePath = "jdbc/datasource";
public BindDataSource()
{
// create a Hashtable to pass the parameters
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);
// create the actual DataSource and set the 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");
// bind the DataSource object to the name we selected earlier
ctx.bind(filePath, ds);
ctx.close();
}
catch (Exception ex) {
System.err.println(ex.getMessage());
}
}
public static void main(String args[]) {
new BindDataSource ();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?