Setup mysql datasource
import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import com.mysql.jdbc.jdbc2.optional.*;
import javax.sql.*;
import javax.naming.*;
import java.util.*;
public class SetupJNDIDataSource {
public static void main(String args[]) {
try {
startRegistry();
ConnectionPoolDataSource dataSource = new MysqlConnectionPoolDataSource();
dataSource.setUser("username");
dataSource.setPassword("password");
dataSource.setServerName("localhost");
dataSource.setPort(3306);
dataSource.setDatabaseName("databasename");
InitialContext context = createContext();
context.rebind("HrDS", dataSource);
} catch (Exception e) {
System.out.println("SetupJNDIDataSource err: " + e.getMessage());
e.printStackTrace();
}
}
private static void startRegistry() throws RemoteException {
LocateRegistry.createRegistry(1099);
System.out.println("RMI registry ready.");
}
private static InitialContext createContext() throws NamingException {
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
env.put(Context.PROVIDER_URL, "rmi://localhost:1099");
InitialContext context = new InitialContext(env);
return context;
}
}
Related examples in the same category