my dog learned polymorphism*
The moose likes JDBC and the fly likes Acessing a MySQL stored function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Acessing a MySQL stored function" Watch "Acessing a MySQL stored function" New topic
Author

Acessing a MySQL stored function

Mike Tabak
Ranch Hand

Joined: Mar 31, 2012
Posts: 38
I'm going over an example from the Ho Prospring3 book. It writes an app to access a function stored in the MySQL database. The app uses annotation configuration. There are only 3 lines in the xml file:

<import resource="datasource-dbcp.xml/>

<context:component-scan base-package="com.apress.prospring3.ch8.dao.jdbc.annotation"/>

<context:annotation-config/>

The book indicates the first line will import datasource.xml which contains the configuration to the MySQL database (which I have loaded and running with the example database). The goes on to indicate that to run this app. you need the Apache commons-dbcp.jar version 1.4. I put this dependency in my pom file and I can see commons-dbcp.1.4.jar loaded into my maven library folder. However, when I try to run the program I get the following error:

Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [dataSource-dbcp.xml]
Offending resource: class path resource [app-context-sf.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from URL [file:/D:/spring/workspace/Chp8SpringJDBCSupport/target/classes/dataSource-dbcp.xml]; nested exception is java.io.FileNotFoundException: D:\spring\workspace\Chp8SpringJDBCSupport\target\classes\dataSource-dbcp.xml (The system cannot find the file specified)

Note that D:\spring is where my workspace is located. I'm not sure what exactly is wrong here, can someone help? It's seems to be looking for dataSource-dbcp.xml, but the book makes no mention of such a file. I was under the impression having commons-dbcp.1.4.jar in my build path would take care of it.

Thanks in advance
Mike
Mike Tabak
Ranch Hand

Joined: Mar 31, 2012
Posts: 38
Please disregard the previous post, I solved the problem. Thanks.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1106

Could you let us know how, so that you can save anyone coming across your post the same heartache you had...
Mike Tabak
Ranch Hand

Joined: Mar 31, 2012
Posts: 38
Sure, I'll share, even though it was a P.O.K. error (person operating keyboard) and a little embarrassing......

The line <import resource="datasource-dbcp.xml"> imports the datasource-dbcp.xml file which contains the definitions to access the database. This file is in the same directory as the xml file containing the above statement and looks like:

<?xml version="1.0" encoding="UTF-8"?>
<beans xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans">

<!-- the jdbx.xxxx are read in from the jdbc.properties file -->
<bean destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource" id="dataSource">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property> </bean>

<!-- the following tag defines where the jdbc properties file is -->
<context:property-placeholder location="jdbc.properties"/>

</beans>

As you can see by the last tag <context:property-placeholder....> defines the property file where all of the ${jdbc.xxxxx} values com from. This file is also in the same directory and contains:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/prospring3_ch8
jdbc.username=prospring3
jdbc.password=prospring3

The last two lines define the login for the database. This file assumes the login name and password are both prospring3, which is not the case for my installation. I forgot to change the credentials. So my code was operating correctly, but I was not accessing the databbase. The example also had logging turned on so there was a ton of log statements coming out and I missed the one indicating I was not accessing the database.

I'm now on to a new example which uses java configuration to eliminate the XML file configuration. I initially got it working, but then I broke it somehow. But I'm not going to post anything until I exhaust debugging this time. Sorry to waste your time and I hope that this error does actually help someone, at least be sure you modify files correctly before you cut and paste them. I appreciate the help I have gotten on this site, I wish I could have offered some more significant solution other than a P.O.K.!

I did notice one thing about Spring. I usually backup my workspace each time I do an example. At one point I loaded the backup from when my example was working, but all my files were empty. I had to do a refresh to get them back. This seems a bit odd since the backup was just a copy of my workspace from a couple of hours previous and on the same machine. But I guess Spring keeps track of things. Once I refreshed, everything worked fine, except for the example I broke. I guess whatever I broke I saved also. So back to debugging that one.

Thanks
Mike
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2477
    
    7

Thank you Mike!


OCUP UML fundamental and ITIL foundation
youtube channel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Acessing a MySQL stored function
 
Similar Threads
Using Connection Pool To Access MySQL Database On Tomcat
GenericXmlApplicationContext classpath
BeanCreationException and NoClassDefFoundError : What am I missing in here?
Missing CharacterEncodingFilter, UrlRewriteFilter?
java.sql.SQLException: No suitable driver