当前位置:   article > 正文

IDEA连接sqlserver并在网页中显示_idea将数据库显示到网页

idea将数据库显示到网页

IDEA配置SpringBoot实现简单的连接sqlserver数据库并在网页中读取数据库中的内容

目录

新建maven web工程

创建之后等待maven配置完成

配置依赖

pom.xml

项目结构

java.mvc

springdemo

java.mvc.model

Student

java.mvc.Dao

StudentDao

java.mvc.Service

StudentService

java.mvc.controller

StudentController

java.resources

application.properties

java.resources.templates

view.html

创建数据库

运行springdemo

在浏览器中查看结果

错误


新建maven web工程

创建之后等待maven配置完成

配置依赖

注意新版的maven不支持之前的sqljdbc4的包,建议使用mssql-jdbc

pom.xml

  1. <project xmlns="<http://maven.apache.org/POM/4.0.0>" xmlns:xsi="<http://www.w3.org/2001/XMLSchema-instance>" xsi:schemaLocation="<http://maven.apache.org/POM/4.0.0>
  2. <http://maven.apache.org/maven-v4_0_0.xsd>">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.springmvc</groupId>
  5. <artifactId>com.springmvc</artifactId>
  6. <packaging>war</packaging>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>com.springmvc Maven Webapp</name>
  9. <url><http://maven.apache.org></url>
  10. <repositories>
  11. <repository>
  12. <id>public</id>
  13. <name>aliyun nexus</name>
  14. <url><http://maven.aliyun.com/nexus/content/groups/public/></url>
  15. <releases>
  16. <enabled>true</enabled>
  17. </releases>
  18. </repository>
  19. </repositories>
  20. <pluginRepositories>
  21. <pluginRepository>
  22. <id>public</id>
  23. <name>aliyun nexus</name>
  24. <url><http://maven.aliyun.com/nexus/content/groups/public/></url>
  25. <releases>
  26. <enabled>true</enabled>
  27. </releases>
  28. <snapshots>
  29. <enabled>false</enabled>
  30. </snapshots>
  31. </pluginRepository>
  32. </pluginRepositories>
  33. <parent>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-parent</artifactId>
  36. <version>2.1.0.RELEASE</version>
  37. <relativePath/> <!-- lookup parent from repository -->
  38. </parent>
  39. <!-- 导入 UTF-8 -->
  40. <properties>
  41. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  42. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  43. <java.version>1.8</java.version>
  44. </properties>
  45. <dependencies>
  46. <dependency>
  47. <groupId>junit</groupId>
  48. <artifactId>junit</artifactId>
  49. <version>3.8.1</version>
  50. <scope>test</scope>
  51. </dependency>
  52. <dependency>
  53. <groupId>org.springframework.boot</groupId>
  54. <artifactId>spring-boot-starter-tomcat</artifactId>
  55. <scope>provided</scope>
  56. </dependency>
  57. <dependency>
  58. <groupId>org.mybatis.spring.boot</groupId>
  59. <artifactId>mybatis-spring-boot-starter</artifactId>
  60. <version>1.3.1</version>
  61. </dependency>
  62. <dependency>
  63. <groupId>org.springframework.boot</groupId>
  64. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  65. </dependency>
  66. <dependency>
  67. <groupId>org.springframework.boot</groupId>
  68. <artifactId>spring-boot-starter-test</artifactId>
  69. <scope>test</scope>
  70. </dependency>
  71. <dependency>
  72. <groupId>com.microsoft.sqlserver</groupId>
  73. <artifactId>mssql-jdbc</artifactId>
  74. <version>6.4.0.jre8</version>
  75. </dependency>
  76. <dependency>
  77. <groupId>org.mybatis.spring.boot</groupId>
  78. <artifactId>mybatis-spring-boot-starter</artifactId>
  79. <version>1.3.1</version>
  80. </dependency>
  81. <dependency>
  82. <groupId>org.springframework.boot</groupId>
  83. <artifactId>spring-boot-starter-web</artifactId>
  84. </dependency>
  85. <dependency>
  86. <groupId>org.codehaus.janino</groupId>
  87. <artifactId>commons-compiler</artifactId>
  88. <version>2.7.8</version>
  89. </dependency>
  90. <dependency>
  91. <groupId>com.microsoft.sqlserver</groupId>
  92. <artifactId>mssql-jdbc</artifactId>
  93. </dependency>
  94. </dependencies>
  95. <build>
  96. <finalName>com.springmvc</finalName>
  97. </build>
  98. </project>

项目结构

java.mvc

springdemo

  1. package mvc;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class springdemo {
  6. public static void main(String[] args)
  7. { SpringApplication.run(springdemo.class, args); }
  8. }

java.mvc.model

Student

  1. package mvc.model;
  2. public class Student {
  3. private String id;
  4. private String name;
  5. private String email;
  6. public Student(String id, String name, String email) {
  7. this.id = id;
  8. this.name = name;
  9. this.email = email;
  10. }
  11. public Student() {
  12. }
  13. public String getEmail() {
  14. return email;
  15. }
  16. public void setEmail(String email) {
  17. this.email = email;
  18. }
  19. public String getName() {
  20. return name;
  21. }
  22. public void setName(String name) {
  23. this.name = name;
  24. }
  25. public String getId() {
  26. return id;
  27. }
  28. public void setId(String id) {
  29. this.id = id;
  30. }
  31. @Override
  32. public String toString() {
  33. return " id:" + id + " name:" + name + " email:" + email;
  34. }
  35. }

java.mvc.Dao

StudentDao

注意此文件是接口类型


  1. package mvc.Dao;
  2. import java.util.List;
  3. import org.apache.ibatis.annotations.Delete;
  4. import org.apache.ibatis.annotations.Insert;
  5. import org.apache.ibatis.annotations.Mapper;
  6. import org.apache.ibatis.annotations.Param;
  7. import org.apache.ibatis.annotations.Select;
  8. import org.apache.ibatis.annotations.Update;
  9. import mvc.model.Student;
  10. @Mapper
  11. public interface StudentDao {
  12. @Select("SELECT * FROM student WHERE name like #{name}")
  13. List<Student> find(@Param("name") String name);
  14. @Insert("INSERT INTO student ( id,name,email ) VALUES (#{id},#{name},#{email})")
  15. int insert(Student stu);
  16. @Delete("DELETE FROM student WHERE name=#{name}")
  17. int delete(@Param("name") String name);
  18. @Update("Update student set student.name=#{name},student.email=#{email} WHERE student.id=#{id}")
  19. int update(Student stu);
  20. @Select("SELECT * FROM student")
  21. List<Student> findall();
  22. }

java.mvc.Service

StudentService

  1. package mvc.Service;
  2. import java.util.List;
  3. import mvc.Dao.StudentDao;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. import mvc.model.Student;
  7. @Service
  8. public class StudentService {
  9. @Autowired
  10. private StudentDao dao;
  11. public List<Student> findstu(String name) { return
  12. dao.find(name+'%'); }
  13. public int insertstu(Student stu) { return dao.insert(stu);}
  14. public int deletestu(String name) { return dao.delete(name); }
  15. public int updatestu(Student stu) { return dao.update(stu); }
  16. public List<Student> findall() { return dao.findall(); }
  17. }

java.mvc.controller

StudentController

  1. package mvc.controller;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestParam;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import org.springframework.web.servlet.ModelAndView;
  9. import mvc.Service.StudentService;
  10. import mvc.model.Student;
  11. @RestController
  12. @RequestMapping(value="/demo")
  13. public class StudentController {
  14. @Autowired
  15. private StudentService svc;
  16. @RequestMapping(value="/select")
  17. public ModelAndView select(@RequestParam("name") String name) {
  18. ModelAndView mav=new ModelAndView("view");
  19. List<Student>stu=new ArrayList();
  20. stu=svc.findstu(name);
  21. mav.addObject("stu",stu);
  22. return mav;
  23. }
  24. @RequestMapping(value="/insert")
  25. public int insertstu ( @RequestParam("id") String
  26. id,@RequestParam("name") String name,@RequestParam("email") String email) {
  27. Student stu=new Student(id, name, email);
  28. return svc.insertstu(stu);
  29. }
  30. @RequestMapping(value="/delete")
  31. public int deletestu(@RequestParam("name") String name) { return
  32. svc.deletestu(name); }
  33. @RequestMapping(value="/update")
  34. public int updatestu ( @RequestParam("id") String
  35. id,@RequestParam("name") String name,@RequestParam("email") String email) {
  36. Student stu=new Student(id,name,email);
  37. return svc.updatestu(stu);
  38. }
  39. @RequestMapping(value="/findall")
  40. public ModelAndView findall(){
  41. ModelAndView mav=new ModelAndView("view");
  42. List<Student>stu=new ArrayList();
  43. stu=svc.findall();
  44. mav.addObject("stu",stu);
  45. return mav;
  46. }
  47. }

java.resources

application.properties

注意要换成自己的配置信息

  1. spring.datasource.url=jdbc:sqlserver://localhost;database=Student
  2. spring.datasource.username=sa
  3. spring.datasource.password=123
  4. spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
  5. server.port=8888

java.resources.templates

view.html

  1. <table border="1">
  2. <tr th:each="stu:${stu}">
  3. <td th:text="${stu.id}"></td> <td th:text="${stu.name}"></td><td
  4. th:text="${stu.email}"></td>
  5. </tr>
  6. </table>

创建数据库

  1. create database Student;
  2. use Student;
  3. create table Student(
  4. id varchar(255),
  5. name varchar(255),
  6. email varchar(255),
  7. constraint Student_Pri primary key (id)
  8. );
  9. create database Student;
  10. user Student;
  11. create table Student(
  12. id varchar(255),
  13. name varchar(255),
  14. email varchar(255),
  15. constraint Student_Pri primary key (id)
  16. );
  17. insert into Student(id, name, email)
  18. values ('20302200001', '张三', '20302200001@m.ldu.edu.cn'),
  19. ('20302200002', '李四', '20302200002@m.ldu.edu.cn'),
  20. ('20302200003', '王五', '20302200003@m.ldu.edu.cn'),
  21. ('20302200004', '李明', '20302200004@m.ldu.edu.cn');

运行springdemo

在浏览器中查看结果

输入地址127.0.0.1:8888/demo/findall

错误

数据库太老要用之前的包

点击Drivers

还是不行,修改我们的jdk文件

参考

找到我们jdk的安装文件夹

进入conf

进入security

记事本打卡java.security

cirl+f搜索“jdk.tls.disabledAlgorithms=

删除 TLSv1, TLSv1.1, 3DES_EDE_CBC(存在则删除,有的有,有的可能就只有其中一个)

保存再重连

还一样,更改idea中的该文件步骤一样,在idea安装目录下的\App\IntelliJ IDEA 2023.2.1\jbr\conf\security

连接成功

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/512446
推荐阅读
相关标签
  

闽ICP备14008679号