File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Problem using DataSource with core java app Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Problem using DataSource with core java app" Watch "Problem using DataSource with core java app" New topic
Author

Problem using DataSource with core java app

hem kumar
Ranch Hand

Joined: Sep 05, 2005
Posts: 97
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.



.....For Every Problem There Is a Solution.....
Regards, Hem
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Does your container provide a DataSource that can be used outside its JVM? Not all do.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
hem kumar
Ranch Hand

Joined: Sep 05, 2005
Posts: 97
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

Joined: Apr 14, 2004
Posts: 10336

OK. So what is supplying your DataSource?
hem kumar
Ranch Hand

Joined: Sep 05, 2005
Posts: 97
i dont know how to provide that DataSource here. Thats the main problem. Any help? Thank you.....
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18138
    
    8

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

Joined: Sep 05, 2005
Posts: 97
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

Joined: Sep 05, 2005
Posts: 97
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
Bartender

Joined: Oct 14, 2005
Posts: 18138
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem using DataSource with core java app
 
Similar Threads
Database access, potential threading problem?
Name is not bound in this context
SQL query takes too long to execute?
java.sql.SQLException: Result set is in an invalid state. May be before the first row or after the
How to bind DataSource using context.bind. Connection pooling etc.