简体   繁体   中英

SPRING 3 MVC - MySQL JDBC DataBase Connection Configuration

Lost in the multitude of Java API's and XML configuration.

I am trying to create an app with the Spring MVC but struggling with the XML configuration.

I want to be able to connect to a mysql database... but I am struggling to find any concise way of how to do it. I do not want to use Hibernate or any additional frameworks, the JDBC will be adequate on it's own.

I would just like to be able to create a database connection and access to a String variable that can change the query as necessary. I think the problem lies within the xml configuration, but I may be wrong.

I have pasted the details shown below in the application-context.xml file, but the server cannot be built unless I remove them. I am not sure if I am missing something simple!

<bean id="JdbcDao" class="com.bcash.DbAccess.JdbcDao">
    <property name="dataSource" ref="dataSource"/>
</bean>      

<bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"
      p:driverClassName="com.mysql.jdbc.Driver"
      p:url="jdbc:mysql://localhost:3306/db_name"
      p:username="root"
      p:password=""
      destroy-method="close" />

This is the associated class that I wrote for the xml declaration

package com.bcash.DbAccess;

import javax.sql.DataSource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

public class JdbcDao {

private JdbcTemplate jdbcTemplate;
protected String query = "INSERT INTO    user('username','email','password','access_level') VALUES ('admin','test@test.com','testPassWord','admin')";


public void insertUser(){

    try{
        jdbcTemplate.update(query);

    } catch(DataAccessException e){

       String error =  e.getMessage();
       System.out.println(error);

    }

}

}

The only error that I get is that the server could not be deployed on line 726 of the ant build script

<target if="netbeans.home" name="-run-deploy-nb">
    <nbdeploy clientUrlPart="${client.urlPart}" debugmode="false" forceRedeploy="${forceRedeploy}"/>
</target>

Although, I am okay with PHP, I am a little confused as I am fairly new to Java.

Thanks in advance

I don't know that I really follow the question you are asking about the build, but looking at the code and how you have things setup I do see one thing that looks like a problem. It looks like you never instantiate your JdbcTemplate and associate it with the dataSource.

You should create your JdbcTemplate instance as a bean like this:

  <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource">
      <ref bean="dataSource" />
    </property>
  </bean>

Then make sure you have a set method for your jdbcTemplate variable and pass the reference to the template into your bean instead of the dataSource.

<bean id="JdbcDao" class="com.bcash.DbAccess.JdbcDao">
    <property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>  

Doing this your JdbcTemplate will have a reference to your dataSource and you should then be able to perform queries.

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