Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Resource Injection

  1. #1
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Resource Injection

    I've recently discovered that instead of using

    Class.forName("com.mysql.jdbc.Driver");
    DriverManager.getConnection("jdbc:mysql://localhost/DBname", "username", "password");


    to connect to a database, you can use @Resource annotation to inject into a DataSource which will pool the connection rather than repeatedly open and close like the above, but the example I saw in a book only gave an example of:

    @Resource(name="jdbc/derby")
    private DataSource dataSource;

    and nothing else.

    My question is, how would I modify the first piece of code to work with the above @Resource style? I've tried playing around with it, but to no avail.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Resource Injection

    Been a while since I've used those sorts of annotations, but from memory I believe you need to specify the data source through some sort of xml file read by the container. Again, been a while so take that with a grain of salt. An alternative is to just create a manager class which encapsulates the connections - thus you need those two lines of code in one place in the project rather that needing to reuse them continually

  3. The Following User Says Thank You to copeg For This Useful Post:

    newbie (March 22nd, 2011)

  4. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Resource Injection

    Yeah you were right, needed to create a persistence.xml file, which was easy enough to create via IDE wizard. Afterwards, all that was needed was to specify my database name in the annotation parameters.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. Method injection
    By java? in forum Object Oriented Programming
    Replies: 1
    Last Post: December 29th, 2010, 03:16 PM
  2. Dll injection Java
    By TheEnd in forum Java Theory & Questions
    Replies: 1
    Last Post: March 2nd, 2010, 04:35 PM
  3. Object Injection
    By Json in forum Java Programming Tutorials
    Replies: 1
    Last Post: December 4th, 2009, 04:08 AM
  4. Replies: 2
    Last Post: August 4th, 2009, 11:25 PM
  5. Java Learning Resource for Beginners
    By Freaky Chris in forum The Cafe
    Replies: 1
    Last Post: April 29th, 2009, 04:57 AM