• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem using DataSource with core java app

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i want to know if i can use DataSource mechanism instead of DriverManager to connect to a MS SQL Database.
I use- 1) NetBeans 6.8
2) MS SQL Database 2005 with ms sql connector for java

The below is my program which compiles and runs fine but without any output.
The Database tables are correct.

Thanks in advance.


 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does your container provide a DataSource that can be used outside its JVM? Not all do.
 
hem kumar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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. :-)
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK. So what is supplying your DataSource?
 
hem kumar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i dont know how to provide that DataSource here. Thats the main problem. Any help? Thank you.....
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
hem kumar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
hem kumar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyway i was getting my adventurous ideas from this tutorial from sun site - http://java.sun.com/docs/books/tutorial/jdbc/basics/connecting.html


An excerpt is provided below-

Using a DataSource Object for a connection

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:

InitialContext ic = new InitialContext();

DataSource ds = ic.lookup("java:comp/env/jdbc/myDB");
Connection con = ds.getConnection();
DataSource ds = (DataSource) org.apache.derby.jdbc.ClientDataSource()
ds.setPort(1527);
ds.setHost("localhost");
ds.setUser("APP")
ds.setPassword("APP");

Connection con = ds.getConnection();

-----------------------------------------------------------
so i was thinking there must be some ClientDataSource() method in Ms SQL connector .....
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic