SPRING 3 MVC - MySQL JDBC Database Connection Configuration

Lost in many Java APIs and XML configurations.

I am trying to create an application using Spring MVC, but am struggling with XML configuration.

I want to be able to connect to mysql database ... but I'm struggling to find some short way to do this. I do not want to use Hibernate or any additional frameworks, JDBC will be adequate as I see fit.

I would just like to create a database connection and access the String variable, which can modify the query if necessary. I think the problem lies in the xml configuration, but I could be wrong.

I inserted the details listed below in the application-context.xml file, but the server cannot be created unless I delete them. I'm not sure 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 related class I wrote for 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 I get is that the server cannot be deployed on line 726 ant build script

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

Although, I'm fine with PHP, I'm a little confused as I'm pretty new to Java.

Thank you in advance

+4
source share
1 answer

I don’t know that I really follow the question that you ask about the assembly, but looking at the code and how you have the settings, I see one thing that looks like a problem. It looks like you never create an instance of JdbcTemplate and do not bind it to a data source.

You must create your JdbcTemplate instance as a bean as follows:

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

Then, make sure you have the set method for the JdbcTemplate variable and pass the template link to your bean instead of the data source.

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

Doing this of your JdbcTemplate will have a link to your data source, and you will need to be able to execute queries.

+5
source

Source: https://habr.com/ru/post/1392355/


All Articles