I am writing a standalone application which will get the datasource from Websphere Application Server, get the db connection and perform some business logic.
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class TestDS {
public static void main(String[] args) {
// TODO Auto-generated method stub
Hashtable<String, String> pdEnv = new Hashtable<String, String>();
pdEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
pdEnv.put(Context.PROVIDER_URL, "iiop://localhost:2809");
System.out.println("Getting connection");
Context initialContext;
try {
System.setProperty("com.ibm.CORBA.Debug","true");
System.setProperty("com.ibm.CORBA.CommTrace","true");
System.setProperty("com.ibm.CORBA.Debug.Output","/usr/app/corba.log");
initialContext = new InitialContext(pdEnv);
System.out.println("Getting initial context");
DataSource datasource = (DataSource)initialContext.lookup("jdbc/sampleDB");
System.out.println("Getting datasource");
Connection connection = null;
System.out.println("Datasoure is "+ datasource);
if (datasource != null) {
//connection = datasource.getConnection("username","password"); // DB credintials
connection = datasource.getConnection();
} else {
System.out.println("Failed to lookup datasource.");
}
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
This gives the following error
java.sql.SQLNonTransientException: [jcc][t4][10205][11234][4.21.29] Null userid is not supported. ERRORCODE=-4461, SQLSTATE=42815 DSRA0010E: SQL State = 42815, Error Code = -4,461
But if provide userid/password, It works fine
connection = datasource.getConnection("username","password");
How do I get it working without specifying userid/password when getting db connections
Most JDBC driver vendors allow a user and password to be set on their data sources which then serves as a default for all connections that are requested without a user/password. If you go to the configuration for your data source in WebSphere Application Server, and then to "Custom properties", you should be able to add properties for "user" and "password", which should then be applied to the data source if the JDBC vendor supports it.