This week's book giveaway is in the General Computing forum.
We're giving away four copies of Emmy in the Key of Code and have Aimee Lucido on-line!
See this thread for details.
Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Injecting A DataSource Into A Factory

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am attempting to inject a DataSource into a factory class I wrote. This factory was SUPPOSED to have static methods that would take a String specifying SQL, and return a ResultSet from the SQL. This way I can call the code from a scriptlet in a JSP?

The big question will be "WHY would any sane person want to do this?" The answer is that this is an existing application that I have to fix, but am not allowed to rewrite from scratch using DAOs (*grumble*). So, I wanted to replace a bunch of ugly JDBC code in the scriptlet with something that looks like this:



However, Spring can't inject the datasource into a static variable (declared as "private static DataSource dataSource". If anyone has any ideas, I would LOVE to hear them. I don't want to have to instantiate the ResultSetFactory, because that seems to miss the point of a Factory class.

Here is my non-working code:



And here is my applicationContext.xml:



Again, any ideas are appreciated.
 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

you can make the method getResultSet static, but that does not mean that setDatasource and it's property have to be.

Apart from that: I don't think your code will work, if you close the connection I don't think you can access the resultset anymore.

Herman
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I now notice this in the ResultSet javadocs... thwarted again. This explains the ugly NullPointerExceptions that I noticed 10 minutes before leaving for the day yesterday.

Originally posted by Herman Scheltinga:
hi,

you can make the method getResultSet static, but that does not mean that setDatasource and it's property have to be.

Apart from that: I don't think your code will work, if you close the connection I don't think you can access the resultset anymore.

Herman

 
"I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!