Create a springboot framework project for the first time

1.1_ Creation steps

(1) Create a spring project
Insert picture description here

(2)
Insert picture description here

(3)
Insert picture description here

Join the engine
Insert picture description here

Insert picture description here
Just the next step

2.1_ Problems encountered during startup

(1) There is no startup icon at the beginning, just wait a while.
Insert picture description here
(2) Afterwards, the startup fails and the error ERROR 3704 is reported — [main] osbdLoggingFailureAnalysisReporter:

Because the default port is 8080, if it is already occupied, you need to change the default port number

Insert picture description here
Modification method : Modificationapplication.propertiesFile, add in the file:

server.port=8081
server.context-path=/demo

Insert picture description here

Successful startup:
Insert picture description here

(3) The next visit to http://localhost:8081/ was successful, but after a day, it didn’t work and found that it was a directory problem. You
should visit http://localhost:8081/demo
Insert picture description here

2.2_ Start response web page test

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController//响应服务器,与@RequestMapping配合使用(“@RestController配合@RequestMapping”与“@Controller配合@ResponseBody再配合@RequestMapping”效果一样)
@SpringBootApplication//声明该类是一个springboot引导类,
public class DemoApplication {
    
    

    public static void main(String[] args) {
    
    
    //run方法表示运行springboot的引导类
        SpringApplication.run(DemoApplication.class, args);
    }

    @RequestMapping
    public String hello() {
    
    
        return "hello spring boot!";
    }
}

Search in the browser: "http://localhost:8081/"

Insert picture description here

2.3_ try to connect to the database

package com.example.demo;
import java.sql.*;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController//响应服务器,与@RequestMapping配合使用(“@RestController配合@RequestMapping”与“@Controller配合@ResponseBody再配合@RequestMapping”效果一样)
@SpringBootApplication
public class DemoApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(DemoApplication.class, args);
    }

    @RequestMapping
    public String hello()throws Exception {
    
    
        SqlOperation.main();
        ResultSet resultSet = SqlOperation.statement.executeQuery("select * from Score");
        String s;
        resultSet.next();
        s = resultSet.getString("name");
        return s;
    }
}


class SqlOperation {
    
    
    public static Connection connection = null;//定义连接数据库的对象(桥梁)
    public static String url = "jdbc:sqlserver://localhost:1433;DatabaseName=Studentinfo";
    public static Statement statement = null;//定义静态操作对象
    public static PreparedStatement preparedStatement = null;//定义动态操作对象
    public static void main() {
    
    
        try{
    
    
            //第一步加载驱动
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            System.out.println("加载驱动成功!");
            //第二步建立连接
            connection = DriverManager.getConnection(url,"sa","shejiashuai");
            System.out.println("连接数据库成功!");
            //第三步建立操作对象
            statement = connection.createStatement();
        }catch (Exception e){
    
    
            e.printStackTrace();
            System.out.println("连接数据库失败!");
        }
    }
    public static void close(){
    
    //关闭连接
        try{
    
    
            statement.close();
            connection.close();
        }catch (Exception e){
    
    
            e.printStackTrace();
        }
    }
}

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43907296/article/details/109399303