Create a new project and add support for the database
Download MySQL Driver pack mysql-connector-java-5.1.7-bin.jar, shortcut key ctrl+alt+shift+s, add jar package to Project
Writing Test Text Jdbctemplatedemo.java
1 PackageCom.jdbc;2 3 Importorg.junit.Test;4 Importorg.springframework.jdbc.core.JdbcTemplate;5 ImportOrg.springframework.jdbc.datasource.DriverManagerDataSource;6 7 Public classJdbctemplatedemo {8 @Test9 Public voidAdd () {TenDrivermanagerdatasource DataSource =NewDrivermanagerdatasource (); OneDatasource.setdriverclassname ("Com.mysql.jdbc.Driver");//Add Driver ADatasource.seturl ("Jdbc:mysql:///test");//Add a database path -Datasource.setusername ("root");//User name -Datasource.setpassword ("xxxx");//Password the - //Create a JdbcTemplate object, set the database -JdbcTemplate JdbcTemplate =NewJdbcTemplate (dataSource); - + //invoke the method inside the JdbcTemplate object to implement the Operation -String sql = "INSERT into User values (?,?)"; + introws = jdbctemplate.update (sql, "Lucy", "250"); A System.out.println (rows); at } -}
Output
1
You can see that the data is already inserted in the database
Java Framework Spring Boot learning Note (16): Working with MySQL database