Jon
Jon

Reputation: 3204

Connecting to a MySQL database from a Spring project

I have been trying to access MySQL routines from my Spring project using SimpleJdbcDaoSupport.

I have a class called AdminSimpleMessageManager, which implements the interface AdminMessageManager.

AdminSimpleMessageManager has an instance of the class AdminSimpleJdbcMessageDao, which implements the interface AdminMessageDao.

AdminSimpleJdbcMessageDao has the following method:

public class AdminSimpleJdbcMessageDao extends SimpleJdbcDaoSupport implements AdminMessageDao {

public int addMessage(String from, String message) {
    return getJdbcTemplate().queryForInt("call insert_contact_message(?, ?)", from, message);
}

}

I have included the following in my application context:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/OctagonDB"/>
</bean>

<bean id="adminMessageManager" class="Managers.AdminSimpleMessageManager">
    <property name="adminMessageDao" ref="adminMessageDao"/>
</bean>

<bean id="adminMessageDao" class="Managers.dao.AdminSimpleJdbcMessageDao">
    <property name="dataSource" ref="dataSource"/>
</bean>

but I feel there are a few important lines missing. I get the error

SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/NewWebsite] threw exception [Request processing failed; nested exception is org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback; SQL [call insert_contact_message(?, ?)]; ResultSet is from UPDATE. No Data.; nested exception is java.sql.SQLException: ResultSet is from UPDATE. No Data.] with root cause java.sql.SQLException: ResultSet is from UPDATE. No Data.

I am wondering if the SQL routine needs to return some confirmation? Or maybe I am accessing the DB with the wrong information, is there a way of determining whether it even connected?

EDIT: insert_contact_message looks like this:

CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_contact_message`(
   _author VARCHAR(45),
   _message MEDIUMTEXT
)
BEGIN
   INSERT INTO contact_messages (`author`, `message`, `date_sent`) VALUES (_author, _message, NOW());
END

Upvotes: 1

Views: 1010

Answers (1)

David Harkness
David Harkness

Reputation: 36562

You're using queryForInt, but insert_contact_message doesn't return a result. I haven't used JdbcTemplate before, but it looks like you might want to use execute that doesn't expect a result set.

Another option is to change from using a procedure to a function.

CREATE DEFINER=`root`@`localhost` FUNCTION `insert_contact_message`(
   _author VARCHAR(45),
   _message MEDIUMTEXT
)
BEGIN
   INSERT INTO contact_messages (`author`, `message`, `date_sent`) VALUES (_author, _message, NOW());
   RETURN 1;
END

Upvotes: 1

Related Questions