简体   繁体   中英

How to setup a JDBC connection to MySQL in Spring3 MVC?

I am working on a small web project in java and Spring3 MVC. Although I have been studying java for the past 5 months, this is my first time making anything substantial with either of these technologies.

The problem I am having is setting up a Mysql database connection using Dependency Injection in the Spring applicationContext.xml file.

I build a new project in NetBeans and do the following:

  1. Include Spring3 with Dependency Injection
  2. Import mysql JDBC library
  3. Create a jdbc.properties file with associated name value pairs
  4. Run project (works fine)
  5. Configure Database connection in applicationContext.xml
  6. Re load Project
  7. Build Fails

This is my applicationContext.xml and jdbc.properties

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

<bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"
      p:driverClassName="${jdbc.driverClassName}"
      p:url="${jdbc.url}"
      p:username="${jdbc.username}"
      p:password="${jdbc.password}" />

<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
</beans>

This is the jdbc.properties

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/bcash
username=root
password=myPassword

I have spent the best part of a week trying to configure Spring3, I have also been reading through Spring in Action and Spring Recipes, but I can't seem to get past the first hurdle of just configuring the Spring Container.

Am I overlooking something simple?

Any help is truly appreciated, thanks in advance

UPDATE Buid error message

/home/bcash/NetBeansProjects/bcash.com/nbproject/build-impl.xml:726: The module has not been deployed.
See the server log for details.
BUILD FAILED (total time: 6 seconds)

Corresponding build-XML line message

<nbdeploy clientUrlPart="${client.urlPart}" debugmode="false" forceRedeploy="${forceRedeploy}"/>

You might need to add the connector to your classpath, it's hard to see from your error but it's needed.

If you run tomcat it should be placed in it's /lib directory.

It can be downloaded from mysql

or through maven

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.18</version>
</dependency>

Thanks everyone for the advice. I was doing something so blatantly obvious.

I was putting the database connection beans in the wrong xml file.

I was using the appication-context.xml file instead of the dispatcher-servlet.xml file

Thanks anyway guys

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM