java - Fail to connect to mysql when I using spring boot -
i want make web server spring boot, when try connect database, comes errors. here parts of code.
@controller public class backgroundcontroller { private jdbctemplate jdbctemplate; @requestmapping("/rankinglist") @responsebody public string rankinglist(model model){ string sql="select username,score 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 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 parts of application.properties, 'em' name of 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 status
of 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 url:http://localhost:4908/rankinglist
, output java.lang.nullpointerexception
.
i want help, , appreciate much.
you should add @autowired
private jdbctemplate jdbctemplate;
Comments
Post a Comment