This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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 ]
author & internet detective
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..
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.