jdbc + springboot + axios

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
WARN: Establishing SSL connection without server’s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn’t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false’. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
Class.forName()是反射的方式,其实采用connection、statement也是可以的。

遍历查询
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
可以看到返回的字符串依然不符合目标,返回的依然是指针,所以我们需要改写一下,toString()函数。在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

package com.example.demo;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class Hello {
    
    
    @CrossOrigin
    @GetMapping("/")
    public String say(){
    
    
        return "hello";
    }

    public class Student{
    
    
        int id;
        String name;
        boolean gender;
        Date birthday;

        public void setId(int id) {
    
    
            this.id = id;
        }

        public void setName(String name) {
    
    
            this.name = name;
        }

        public void setGender(boolean gender) {
    
    
            this.gender = gender;
        }

        public void setBirthday(Date birthday) {
    
    
            this.birthday = birthday;
        }

        @Override
        public String toString() {
    
    
            JSONObject json = new JSONObject();
            json.put("id",this.id);
            json.put("name",this.name);
            json.put("gender",this.gender);
            json.put("birthday",this.birthday);
            return json.toString();
            //return (this.id + " " + this.name + " " + this.gender + " " +this.birthday).toString();
        }

    }

    @CrossOrigin
    @GetMapping("/sel")
    public List<String> selStudents(){
    
    
        List<String> students = new ArrayList<>();
        try {
    
    
            String url ="jdbc:mysql://192.168.108.128:3306/data?useUnicode=true&characterEncoding=utf8&useSSL=false";
            Connection connection = DriverManager.getConnection(url,"root","123456");
            PreparedStatement ps = connection.prepareStatement("select * from student");
            ResultSet resultSet = ps.executeQuery();
            while (resultSet.next()){
    
    
                Student student = new Student();
                student.setId(resultSet.getInt("id"));
                student.setGender(resultSet.getBoolean("gender"));
                student.setName(resultSet.getString("name"));
                student.setBirthday(resultSet.getDate("birthday"));
                students.add(student.toString());
            }
            connection.close();
            resultSet.close();
            ps.close();
            for (String stu: students){
    
    
                System.out.println(stu);
            }
        }catch (Exception e){
    
    
            System.out.println(e);
        }
        return students;
    }

    @CrossOrigin
    @GetMapping("/selS")
    public List<String> selStudent(Integer id,String name){
    
    
        List<String> students = new ArrayList<>();
        try {
    
    
            String url ="jdbc:mysql://192.168.108.128:3306/data?useUnicode=true&characterEncoding=utf8&useSSL=false";
            Connection connection = DriverManager.getConnection(url,"root","123456");
            String sql = "select * from student where name = ?";
            //String sql = "select * from student where id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            System.out.println(id);
            System.out.println(name);
            //ps.setInt(1,id);
            ps.setString(1,name);
            ResultSet resultSet = ps.executeQuery();
            if(resultSet.next()){
    
    
                Student student = new Student();
                student.setId(resultSet.getInt("id"));
                student.setName(resultSet.getString("name"));
                student.setGender(resultSet.getBoolean("gender"));
                student.setBirthday(resultSet.getDate("birthday"));
                students.add(student.toString());
            }else {
    
    
                System.out.println("查无此人!!!");
            }
            connection.close();
            resultSet.close();
            ps.close();
            for (String stu: students){
    
    
                System.out.println(stu);
            }
        }catch (Exception e){
    
    
            System.out.println(e);
        }
        return students;
    }

    //@CrossOrigin
    //@GetMapping("/ins")
    //public List<String> insStudent(String name,boolean gender,String birthday){
    
    
    //    List<String> students = new ArrayList<>();
    //    try {
    
    
    //        String url ="jdbc:mysql://192.168.108.128:3306/data?useUnicode=true&characterEncoding=utf8&useSSL=false";
    //        Connection connection = DriverManager.getConnection(url,"root","123456");
    //        PreparedStatement ps = connection.prepareStatement("insert into student values (null ,?,?,?)");
    //        //ps.setString(1,"xiao");
    //        //ps.setBoolean(2,true);
    //        //ps.setDate(3,java.sql.Date.valueOf("1999-11-11"));
    //        ps.setString(1,name);
    //        ps.setBoolean(2,gender);
    //        //String birthday = "1999-05-10";
    //        ps.setDate(3,java.sql.Date.valueOf(birthday));
    //        int row = ps.executeUpdate();
    //        System.out.println("插入了" + row + "条记录");
    //        connection.close();
    //        ps.close();
    //    }catch (Exception e){
    
    
    //        System.out.println(e);
    //    }
    //    return students;
    //}

    @CrossOrigin
    @PostMapping("/ins")
    public String insStudent(@RequestParam("name") String name,
                                   @RequestParam("gender") boolean gender,
                                   @RequestParam("birthday") String birthday){
    
    
        //List<String> students = new ArrayList<>();
        try {
    
    
            String url ="jdbc:mysql://192.168.108.128:3306/data?useUnicode=true&characterEncoding=utf8&useSSL=false";
            Connection connection = DriverManager.getConnection(url,"root","123456");
            PreparedStatement ps = connection.prepareStatement("insert into student values (null ,?,?,?)");
            //ps.setString(1,"xiao");
            //ps.setBoolean(2,true);
            //ps.setDate(3,java.sql.Date.valueOf("1999-11-11"));
            ps.setString(1,name);
            ps.setBoolean(2,gender);
            ps.setDate(3,java.sql.Date.valueOf(birthday));
            int row = ps.executeUpdate();
            System.out.println("插入了" + row + "条记录");
            connection.close();
            ps.close();
        }catch (Exception e){
    
    
            System.out.println(e);
        }
        return "接收";
    }
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
    <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
    <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
</head>
<body>
<div id="app">
    {
    
    {
    
     info }}
</div>
<script type = "text/javascript">
    new Vue({
    
    
        el: '#app',
        data () {
    
    
            return {
    
    
                info: null
            }
        },
        mounted () {
    
    
            var _this = this;
            axios
                .get('http://127.0.0.1:8001/selS',{
    
    params:{
    
    
                    id:7,
                    name:"sun"
                }})
                .then(
                    // response => (this.info = response.data[0])
                    function (res) {
    
    
                        // console.log(res.data[0])
                        // console.log("*********************************")
                        console.log(JSON.parse(res.data[0]))
                        console.log("*********************************")
                        // console.log(JSON.parse(res.data[0]).id)
                        // console.log("*********************************")
                        // console.log(JSON.parse(res.data[0]).name)
                        // console.log("*********************************")
                        // console.log(JSON.parse(res.data[0]).birthday)
                        // console.log("*********************************")
                        console.log(res.data)
                        _this.info = res.data
                    }
                ).catch(function (error) {
    
     // 请求失败处理
                    console.log(error);
                });

            var params = new URLSearchParams();
            params.append('name','baiyueguang');
            // params.append('gender',false);
            // params.append('birthday','1999-08-10');
            axios.get('http://127.0.0.1:8001/selS',
                params
                // {
    
    params:{
    
    
                //     name:'baiyueguang',
                //     gender:false,
                //     birthday:'1999-08-10'
                //     }
                // }
            ).then(
                function (res) {
    
    
                    if(res.status == 200){
    
    
                        console.log('发送成功')
                    }else {
    
    
                        console.log(res)
                    }
                }
            ).catch(
                function (error) {
    
    
                    console.log('网络错误')
                }
            )


            // axios
            //     .get('http://127.0.0.1:8001/sel')
            //     .then(
            //         // response => (this.info = response.data[0])
            //         function (res) {
    
    
            //             console.log(res.data[0])
            //             console.log("*********************************")
            //             console.log(JSON.parse(res.data[0]))
            //             console.log("*********************************")
            //             console.log(JSON.parse(res.data[0]).id)
            //             console.log("*********************************")
            //             console.log(JSON.parse(res.data[0]).name)
            //             console.log("*********************************")
            //             console.log(JSON.parse(res.data[0]).birthday)
            //             console.log("*********************************")
            //             console.log(res.data)
            //             _this.info = res.data
            //         }
            //     )
            //     .catch(function (error) {
    
     // 请求失败处理
            //         console.log(error);
            //     });

            // var params = new URLSearchParams();
            // params.append('name','baiyueguang');
            // params.append('gender',false);
            // params.append('birthday','1999-08-10');
            // // params.append("name",'baiyueguang');
            // // params.append("gender",false);
            // // params.append("birthday",'1999-08-10');
            // axios.post('http://127.0.0.1:8001/ins',
            //     params
            //     // {
    
    params:
            //     //         {
    
    
            //     //     "name":'baiyueguang',
            //     //     "gender":false,
            //     //     "birthday":'1999-08-10'
            //     // }
            //     // }
            // ).then(
            //     function (res) {
    
    
            //         if(res.status == 200){
    
    
            //             console.log('发送成功')
            //         }else {
    
    
            //             console.log(res)
            //         }
            //     }
            // ).catch(
            //     function (error) {
    
    
            //         console.log('网络错误')
            //     }
            // )

        }
    })
</script>
</body>
</html>

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44716147/article/details/117898231
今日推荐