I want to make a web server with spring boot, but when I try to connect to my database, it comes some errors. Here are some parts of my code.
@Controller
public class BackgroundController {
private JdbcTemplate jdbcTemplate;
@RequestMapping("/RankingList")
@ResponseBody
public String RankingList(Model model){
String sql="select userName,score from user;";
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try {
list = jdbcTemplate.queryForList(sql);
}catch (Exception e){
return e.toString();
}//return "RankingList";
}
}
Here are the dependencies in pom.xml
;
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
Here are some parts of application.properties, where the 'em' is the name of my database;
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/em
spring.datasource.username=root
spring.datasource.password=qwerty
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
Here is the status
of my database.
Connection id: 3
Current database: em
Current user: root@localhost
SSL: Not in use
Using delimiter: ;
Server version: 5.7.18-log MySQL Community Server (GPL)
Protocol version: 10
Connection: localhost via TCP/IP
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
TCP port: 3306
But when I get the url:http://localhost:4908/RankingList
, the output is java.lang.NullPointerException
.
I want to get help, and I will appreciate it very much.
You should add @Autowired
to private JdbcTemplate jdbcTemplate;