• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating a Datasource

 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34669
366
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it was a tutorial at developerWorks...
thanks alot.
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34669
366
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic