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 Creating a Datasource Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Creating a Datasource" Watch "Creating a Datasource" New topic
Author

Creating a Datasource

Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Hey all.
I have read the following from a tutorial at IBM :
creating a datasource :

my questions are :
1. I am using Mysql4.0 , so what should ds.setServerName("persistentjava.com"); be ?
(localhost string in my case ?)
2. same question for ds.setPortNumber(6789) and ds.setDatabaseName("jdbc") ?
3. to use the datasource , he wrote :
// locating the datasource
Connection con = ds.getConnection("java", "sun") ;
why he wrote java, sun ?
the author dosen't provide any explaination for this.
thanks alot.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

John,
Out of curiousity what kind of tutorial was this? Because you usually use interfaces (like DataSource) rather than the specific implementation class (DB2DataSource.)

1. Yes, localhost.
2. Check the mysql docs for the default port number. Use the database name that you used to create the database.
3. "Java" is the username and "sun" is the password.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

it was a tutorial at developerWorks...
thanks alot.
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

OK, I have created a database and its URL is :
jdbc:mysql://localhost:3306/ToddDB
and the code is :

correct my if I am wrong but I don't think the code will work because DataSource
is an interface and can't instatiate it using new.
assuming the code is right, what should I do ?
1. run the class for one time and only one.
2. to use this datasource :
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/ToddDB");
thanks again.
[ August 11, 2004: Message edited by: John Todd ]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

John,
You are correct that you can't use new to create a datasource. The initial context code you posted is correct.
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Thank you alot..
please forgive me to ask you again...
what I should write instead of :
DataSource ds = new DataSource();
since DataSource is an interface not a class.
and the code needs the database's url (at least at creation time), right ?
thanks again..


[ August 12, 2004: Message edited by: John Todd ]
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Hi John,

I think you've picked a rather unfortunate example to start from. It's unlikely you need to unbind anything and you don't need to set the properties of the DataSource in your Java code - that's the point of using JNDI; i.e. to look them up.

You need code that looks like this:

In order for this to work something needs to have bound a DataSource object to the binding "jdbc/DBTodd". I expect this would be done by your J2EE container (you'd have to add the values to the server config somewhere). Note that you can also store the username and password on the server (i.e. outside the source code) using JNDI.

I'm afraid I get a bit lost here. You'd benefit from reading up about JNDI but I also recommend you check your app server documentation to find out how to configure a DataSource.

I've found that when things seem to get really complicated in Java it often means that you're doing it wrong.

I hope this helps.

Jules
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating a Datasource