zoukankan      html  css  js  c++  java
  • springboot连接mysql简单例子

    第一步:    打开 https://start.spring.io/

    输入 mysql   client,web,jdbc,jpa

    然后  idea   import 进入idea

    连接mysql逻辑

    package com.segmentfault.springbootlesson61;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    import sun.font.StandardGlyphVector;
    
    import javax.sql.DataSource;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * Created with IntelliJ IDEA.
     * User: @别慌
     * Date: 2019-07-31
     * Time: 11:04
     * Description:
     */
    @RestController   //返回json数据类型
    @RequestMapping("/user")    //获取web页面的mapping地址
    public class jdbcController {
    
        @Autowired              //自动装配,根据类型进行装配,map<Object,String>  装配的值就是  int,string
    
        private DataSource dataSource;
    
        @GetMapping("get")
    
        public Map<Object,String> getuser(@RequestParam(value = "idd",defaultValue = "1") String idd ){
            // RequestParam   请求的参数
            Map<Object,String> data=new HashMap<Object,String>();
    
                Connection connection=null;
            try {
                connection=dataSource.getConnection();
                Statement statement=connection.createStatement();
                ResultSet resultSet=statement.executeQuery("select idd,name from song where idd ="+idd);
                while (resultSet.next()){
                    int idd_=resultSet.getInt("idd");
                    String name=resultSet.getString("name");
    
                    data.put(idd_,name);
                    System.out.println(idd_+"="+name);
    
                }
    
    
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
    
            return data;
    
        }
    }
    spring.datasource.url=jdbc:mysql://localhost:3306/test    //连接的mysqlurl地址
    
    spring.datasource.username=root                           //连接的mysql账号密码
    spring.datasource.password=123456
    
    spring.datasource.driverClassName=com.mysql.jdbc.Driver   //连接的mysql数据驱动
    RUSH B
  • 相关阅读:
    迭代器和生成器
    案例:复制大文件
    案例:使用seek倒查获取日志文件的最后一行
    Leetcode165. Compare Version Numbers比较版本号
    Leetcode137. Single Number II只出现一次的数字2
    Leetcode129. Sum Root to Leaf Numbers求根到叶子节点数字之和
    Leetcode116. Populating Next Right Pointers in Each Node填充同一层的兄弟节点
    Leetcode114. Flatten Binary Tree to Linked List二叉树展开为链表
    Leetcode113. Path Sum II路径总和2
    C++stl中vector的几种常用构造方法
  • 原文地址:https://www.cnblogs.com/tangsonghuai/p/11277400.html
Copyright © 2011-2022 走看看