This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
i dont use any container. I would like to know if i can connect with a MS SQL database through a datasource from a simple core java application not from any web application. I know how to connect to a database from web application with datasource. But the question is without using any web server or application server can it be done from a simple java application as has been show in the program. If there is, then please let me know. Thanks for replying. :-)
hem kumar wrote:i dont know how to provide that DataSource here. Thats the main problem.
Since you don't have a container to provide the DataSource, you have to write your own code to provide a DataSource. Or go out and find an open-source JNDI provider which you can include.
Can you remind us again why you thought this was a better idea than just connecting to the database directly via DriverManager?
Joined: Sep 05, 2005
Thanks for the reply. In fact i was just getting curious to know if thing can be done this way and was getting a little adventurous. But now i know its not possible to do this way. So thank you again for the help.
Using a DataSource object increases application portability by making it possible for an application to use a logical name for a data source instead of having to supply information specific to a particular driver. The following example shows how to use a DataSource to establish a connection:
You can configure a DataSource using a tool or manually. For example, Here is an example of a DataSource lookup:
Well, like I said, you can do that. But you have to have some infrastructure which will allow you to configure the DataSource and which will give it to via the JNDI reference. You can't expect that to just happen. Obviously it's possible to do it, because those Java EE containers do it, but it isn't a trivial exercise.