有关jndi和DataSource菜鸟问题
import com.microsoft.jdbcx.sqlserver.SQLServerDataSource;
import javax.sql.*;
import java.sql.*;
import javax.naming.*;
import java.util.Hashtable;
public class Test {
public static void main(String[] args) {
try {
Test t = new Test();
t.bindDataSource ();
Connection con = t.getConnection ();
}
catch(NamingException e) {
System.out.println("namingException:"+e.getMessage ());
}
catch(SQLException e) {
System.out.println ("sqlException:"+e.getMessage ());
}
}
public Connection getConnection() throws NamingException,SQLException{
Hashtable<String,String> env = new Hashtable<String,String>();
env.put (Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put (Context.PROVIDER_URL,"ldap://zhouyu:389");
Context ctx = new InitialContext(env);
DataSource dataSource = (DataSource)ctx.lookup ("jdbc/TestDB");
return dataSource.getConnection ("zydgyy","19831009");
}
public void bindDataSource() throws NamingException,SQLException{
SQLServerDataSource dataSource = new SQLServerDataSource();
dataSource.setServerName ("ZHOUYU");
dataSource.setDatabaseName("jiAnMobile");
dataSource.setPortNumber(1433);
//dataSource.setUser("zydgyy");
//dataSource.setPassword("19831009");
Hashtable<String,String> env = new Hashtable<String,String>();
env.put (Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put (Context.PROVIDER_URL,"ldap://zhouyu:389");
Context ctx = new InitialContext(env);
ctx.bind ("jdbc/TestDB",dataSource);
}
}