Websphere dışından yani java consol uygulamasından websphere de tanımlanmış olan bir datasource a erişim örneği aşağıdadır. build path e com.ibm.ws.ejb.thinclient_8.5.0.jar dosyasını eklemek gerekiyor.
package tr.com.company.mys.mobile;
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 com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource;
public class Test {
public static void main(String[] args) {
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:2810");
try {
Context initialContext = new InitialContext(pdEnv);
WSJdbcDataSource datasource = (WSJdbcDataSource) initialContext
.lookup("jdbc/NYSDataSource");
Connection connection = null;
if (datasource != null) {
connection = datasource.getConnection("mys", "MYSpassword");
// DB credintials
} 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();
}
}
}
package tr.com.company.mys.mobile;
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 com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource;
public class Test {
public static void main(String[] args) {
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:2810");
try {
Context initialContext = new InitialContext(pdEnv);
WSJdbcDataSource datasource = (WSJdbcDataSource) initialContext
.lookup("jdbc/NYSDataSource");
Connection connection = null;
if (datasource != null) {
connection = datasource.getConnection("mys", "MYSpassword");
// DB credintials
} 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();
}
}
}
Yukarıdaki kodu çalıştırdığımızda aşağıdaki hata mesajını alabiliriz.
JSAS1480I: Security is not enabled because the ConfigURL property file is not set.
Anda baru saja membaca artikel yang berkategori dengan judul Java consol uygulamasından websphere datasource a erişim.. Jika kamu suka, janganlike dan bagikan keteman-temanmu ya... By : En Kolay Yolu
Ditulis oleh:
Adsız -
Belum ada komentar untuk "Java consol uygulamasından websphere datasource a erişim."
Yorum Gönder