Java Framework Spring Boot learning Note (16): Working with MySQL database

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.