Java架構spring Boot學習筆記(十六):操作MySQL資料庫

來源:互聯網
上載者:User

標籤:date   資料   work   輸出   一個   mysql   shift   image   username   

建立一個工程,添加對資料庫的支援

下載mysql驅動包 mysql-connector-java-5.1.7-bin.jar,快速鍵ctrl+alt+shift+s,添加jar包到工程

編寫測試文JdbcTemplateDemo.java

 1 package com.jdbc; 2  3 import org.junit.Test; 4 import org.springframework.jdbc.core.JdbcTemplate; 5 import org.springframework.jdbc.datasource.DriverManagerDataSource; 6  7 public class JdbcTemplateDemo { 8     @Test 9     public void add(){10         DriverManagerDataSource dataSource = new DriverManagerDataSource();11         dataSource.setDriverClassName("com.mysql.jdbc.Driver"); //添加驅動12         dataSource.setUrl("jdbc:mysql:///test"); //添加資料庫路徑13         dataSource.setUsername("root"); //使用者名稱14         dataSource.setPassword("xxxx"); //密碼15 16         //建立jdbcTemplate對象,設定資料庫17         JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);18 19         //調用jdbcTemplate對象裡面的方法實現操作20         String sql = "insert into User values(?,?)";21         int rows = jdbcTemplate.update(sql,"Lucy","250");22         System.out.println(rows);23     }24 }

輸出

1

可以看到資料庫中已經插入了資料

 

Java架構spring Boot學習筆記(十六):操作MySQL資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.