[JAVA] I tried connecting to MySQL using JDBC Template with Spring MVC

Introduction

-Based on the environment of Last time, I tried to implement the connection to the database.

Add dependency from Maven

Open pom.xml and add it to the last line in .

Add MySQL JDBC Connector

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

Add Spring JDBC

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${org.springframework-version}</version>
</dependency>

Cleanup and installation

  1. Right-click on the project and run "Run> Maven clean".
  2. Similarly, execute "Run> Maven install".

MySQL setup

MySQL installation

brew install mysql

Start MySQL

mysql.server start

Creating a database

CREATE DATABASE sandbox DEFAULT CHARACTER SET utf8;

Creating a table

create table users(id int, name varchar(20));

Data input

Please insert appropriately.

JDBC connection settings

Open root-context.xml and add it to the last line in .

Connection settings

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName">
        <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
        <value>jdbc:mysql://127.0.0.1:3306/sandbox</value>
    </property>
    <property name="username">
        <value>root</value>
    </property>
    <property name="password">
        <value></value>
    </property>
</bean>

JDBC Template settings

<bean class="org.springframework.jdbc.core.JdbcTemplate">
    <constructor-arg ref="dataSource" />
</bean>

Implementation of connection confirmation program

Controller implementation

HomeController.java


package com.sandbox.app;

import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
 
		List<Map<String, Object>> list = jdbcTemplate.queryForList("SELECT * FROM users");
 
		model.addAttribute("data", list.get(0).get("name") );
	
		return "home";
	}
}

View implementation

home.jsp


<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
	<head>
		<title>Home</title>
	</head>
	<body>
	<h1>
		Hello world!  
	</h1>
	<p>  DB's data is ${data}. </p>
	</body>
</html>

Start tomcat

Right-click on the project and run Run> Run on Server. If the following page is displayed, it is successful.

FireShot Capture 066 - Home - http___localhost_8080_app_.jpg

Summary

――What is the difference from JPA? -Will you use the ORM library in practice?

Recommended Posts

I tried connecting to MySQL using JDBC Template with Spring MVC
Settings for connecting to MySQL with Spring Boot + Spring JDBC
I tried to implement file upload with Spring MVC
I tried to get started with Swagger using Spring Boot
I tried connecting to Oracle Autonomous Database 21c with JDBC Thin
I tried printing a form with Spring MVC and JasperReports 2/3 (form template creation)
Whether to enable SSL when using JDBC with MySQL.
I started MySQL 5.7 with docker-compose and tried to connect
I tried to get started with Spring Data JPA
[Java] I tried to connect using a connection pool with Servlet (tomcat) & MySQL & Java
How to create an Excel form using a template file with Spring MVC
[Java] I installed JDBC and tried to connect with servlet + MySQL. (There is a version using DAO / Bean)
I tried to make an introduction to PHP + MySQL with Docker
I tried using Spring + Mybatis + DbUnit
I tried using JOOQ with Gradle
I tried to interact with Java
I tried GraphQL with Spring Boot
I tried Flyway with Spring Boot
I tried printing a form with Spring MVC and JasperReports 1/3 (JasperReports settings)
I tried printing a form with Spring MVC and JasperReports 3/3 (Spring MVC control)
I want to INSERT Spring Local Time with MySQL Time (also milliseconds)
I tried to create a Spring MVC development environment on Mac
I tried to make an Android application with MVC now (Java)
I tried to get started with WebAssembly
I tried using Scalar DL with Docker
I tried using OnlineConverter with SpringBoot + JODConverter
I tried using OpenCV with Java + Tomcat
I tried Lazy Initialization with Spring Boot 2.2.0
I tried Spring Data JDBC 1.0.0.BUILD-SNAPSHOT (-> 1.0.0.RELEASE)
I tried to implement ModanShogi with Kinx
01. I tried to build an environment with SpringBoot + IntelliJ + MySQL (MyBatis) (Windows10)
I used Docker to solidify the template to be developed with spring boot.
I tried Spring.
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
I tried to manage struts configuration with Coggle
I tried to manage login information with JMX
I made blackjack with Ruby (I tried using minitest)
I tried to link JavaFX and Spring Framework.
I wanted to gradle spring boot with multi-project
I tried to implement a server using Netty
I tried to break a block with java (1)
I tried printing a form with Spring MVC and JasperReports Extra edition (Variables edition)
I tried printing a form with Spring MVC and JasperReports Extra edition (image edition)
I tried to clone a web application full of bugs with Spring Boot
[JDBC ③] I tried to input from the main method using placeholders and arguments.
I tried what I wanted to try with Stream softly.
[Rails] How to connect to an external API using HTTP Client (I tried connecting to Qiita API)
[I tried] Spring tutorial
I tried to read and output CSV with Outsystems
I tried to implement TCP / IP + BIO with JAVA
[Java 11] I tried to execute Java without compiling with javac
I tried to operate SQS using AWS Java SDK
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
I tried using TestNG
I tried Spring Batch
I tried using Galasa
I tried to draw animation with Blazor + canvas API
I tried to implement Stalin sort with Java Collector
I tried to investigate the mechanism of Emscripten by using it with the Sudoku solver
Try to work with Keycloak using Spring Security SAML (Spring 5)