Springboot cannot connect to the database

the first attempt to use springboot, intended to use jdbctemplate, but the startup project reported an error.

Database is mysql5.7, operating system ubuntu16.04

pom.xml

    <name>demoboot</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

application.proerties

spring.datasource.url=jdbc:mysql://localhost:3306/demo01
spring.datasource.username=
spring.datasource.password=

DemobootApplication.java

@SpringBootApplication
public class DemobootApplication
{
    public static void main(String[] args)
    {
        SpringApplication.run(DemobootApplication.class, args);
    }
}

MainController.java

@Controller
public class MainController
{
    @Autowired
    private UserDao userDao;

    @ResponseBody
    @RequestMapping(value = "/usr/{id}", method = RequestMethod.GET)
    public String doGet(@PathVariable Long id)
    {
        User user = userDao.getUserById(id);

        ObjectMapper mapper = new ObjectMapper();
        String respJson;
        try
        {
            respJson = mapper.writeValueAsString(user);
        }
        catch (JsonProcessingException e)
        {
            e.printStackTrace();
            respJson = "";
        }
        return respJson;
    }
}

UserDao.java

@Repository
public class UserDao
{
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public User getUserById(Long userId)
    {
        String sql = "select * from t_user where user_id=?";
        try
        {
            Map<String, Object> resultMap = jdbcTemplate.queryForMap(sql, userId);

            User user = new User();
            user.setUserId((Long) resultMap.get("user_id"));
            user.setUsername((String) resultMap.get("username"));
            user.setPassword((String) resultMap.get("password"));

            return user;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }
}
Apr.03,2021

personal opinion: you can try to add the configuration to the configuration (the following is the MySQL configuration, if other databases need to be modified accordingly)

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

about springboot connecting to the database

I hope I can help you. Thank you


<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        

application.proerties file name is misspelled

      <scope>runtime</scope> 
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eac87d-20de.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eac87d-20de.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?