开发者

how to create initialcontext to connect to sql server

I am having difficulty connecting my java code to sql server database. I can connect from App server just fine..but now I want to try to connect with app server out of the picture and using InitialContext with some env properties. So a stand alone java class connecting to sql server using InitialContext

I have mssqlserver.jar in my projects libraries. Do I need some other jar as well?

I looked around for some code sample but dont know what to put in Context.INITIAL开发者_StackOverflow中文版_CONTEXT_FACTORY

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "what do i put here?");
env.put("java.naming.factory.initial",
"what do i put here?");
...
what other properties would I need?

on the App server I have following properties.

datasourceName = mydb
user = myuser
port = 1433
password = mypwd
databaseName = mydb
servername = localhost


You are not on the right track here. InitialContext is strictly about the application server. In order to connect to the database directly without the application server you have to create your own JDBC connection to the database, or use a different JDBC connection pool implementation.


You may find it helpful to consult the JDBC tutorial, you'll see that you can use a DriverManager to get your connection. The JNDI approach you are familiar with is a nice, general abstraction for giving access to all manner of services provided in an AppServer environment. For stand-alone apps one usually just uses the raw APIs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜