Dependency Required: Spring MVC Database Connectivity Using XML Configuration
Dependency Required: Spring MVC Database Connectivity Using XML Configuration
Dependency Required: Spring MVC Database Connectivity Using XML Configuration
Dependency Required
To connect Spring MVC application to the
database, you need the following dependencies.
pom.xml
<dependencies>
<groupId>org.springframework</groupId>
<artifactId>spring-
webmvc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-
jdbc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
Technology Use
Find the list of all technologies used in this
application.
Eclipse Oxygen
Tomcat 9
JDK 8
Maven 3
Spring5.0.2.RELEASE
Project Structure
Final project structure of our application in
Eclipse IDE will look like as follows.
Register Front Controller and Enable
Spring MVC
We are using Java-based configuration so we
don’t need to create any XML configuration but
we configure it through the annotation.
FontControllerConfig.java
package org.websparrow.config;
import
org.springframework.web.servlet.support.Abstrac
tAnnotationConfigDispatcherServletInitializer;
@Override
protected Class<?>[]
getRootConfigClasses() {
@Override
protected Class<?>[]
getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
WebMvcConfig.java
package org.websparrow.config;
import
org.springframework.context.annotation.Bean;
import
org.springframework.context.annotation.Componen
tScan;
import
org.springframework.context.annotation.Configur
ation;
import
org.springframework.web.servlet.config.annotati
on.EnableWebMvc;
import
org.springframework.web.servlet.view.InternalRe
sourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {
"org.websparrow.controller",
"org.websparrow.dao" })
public class WebMvcConfig {
@Bean
public InternalResourceViewResolver
viewResolver() {
InternalResourceViewResolver vr = new
InternalResourceViewResolver();
vr.setPrefix("/");
vr.setSuffix(".jsp");
return vr;
}
}
import
org.springframework.context.annotation.Bean;
import
org.springframework.context.annotation.Configur
ation;
import
org.springframework.jdbc.datasource.DriverManag
erDataSource;
@Configuration
public class DatabaseConfig {
@Bean
public DriverManagerDataSource
getDataSource() {
bds.setDriverClassName("com.mysql.jdbc.Dri
ver");
bds.setUrl("jdbc:mysql://localhost:3306/we
bsparrow");
bds.setUsername("root");
bds.setPassword("");
return bds;
}
}
Controller
Create controller class handles user to request
to check whether the application is established
a connection or not. Learn more
about @Autowired annotation.
MyController.java
package org.websparrow.controller;
import java.sql.SQLException;
import javax.sql.DataSource;
import
org.springframework.beans.factory.annotation.Au
towired;
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.Request
Mapping;
import
org.springframework.web.servlet.ModelAndView;
@Controller
public class MyController {
@Autowired
private DataSource dataSource;
@RequestMapping("/checkConnection")
public ModelAndView greet() {
try {
if (dataSource.getConnection() !=
null) {
return new
ModelAndView("index", "msg", "Database
Connection Successfully Established.");
} else {
return new
ModelAndView("index", "msg", "Failed to connect
database.");
}
} catch (SQLException e) {
e.printStackTrace();
View
JSP pages that visible to the user.
index.jsp
<html>
<body>
<h2>Spring MVC Database Connectivity
Example</h2>
<b>Message:</b> ${msg}
</body>
</html>
Output:
Finally, run your application and check the
below URL in your browser. You will get the
following result on the JSP page.
URL: localhost:8080/spring-mvc-db-conn-
anno/checkConnection