当前位置:   article > 正文

访问SQLServer(3)-springboot自定义DBUtil(从druid连接池获取连接)操作SQLServer数据库_druid mssql

druid mssql

Spring-Boot通过druid连接池获取连接操作SQLServer数据库
总结:【共3步】
第1步:添加druid和MSSQL Driver的依赖项;
第2步:配置连接字符串;
第3步:创建数据库操作类,使用@Bean注入DruidDataSource,
      然后根据DruidDataSource获取到空闲连接操作数据库;


1. 创建spring-boot工程,选择Web依赖项、SQLServer Driver依赖项:

2. 手动添加druid的依赖项:

  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>druid</artifactId>
  4. <version>1.0.18</version>
  5. </dependency>

 3. application.properties配置:

  1. spring.datasource.url=jdbc:sqlserver://localhost:1451;instanceName=sql2008;DatabaseName=duliang_db
  2. spring.datasource.password=dul_lw
  3. spring.datasource.username=sa
  4. spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver

4. 创建全局上下文访问类:

  1. package com.cx.common;
  2. import org.springframework.context.ApplicationContext;
  3. public class SpringContextUtil {
  4. private static ApplicationContext applicationContext;
  5. public static void setApplicationContext(ApplicationContext context) {
  6. applicationContext = context;
  7. }
  8. public static Object getBean(String beanId) {
  9. return applicationContext.getBean(beanId);
  10. }
  11. }

5. 创建自定义数据库操作类DBUtilDruid:

  1. package com.cx.common;
  2. import com.alibaba.druid.pool.DruidDataSource;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.core.env.Environment;
  6. import org.springframework.stereotype.Component;
  7. import javax.sql.DataSource;
  8. import java.sql.*;
  9. @Component
  10. public class DBUtilDruid {
  11. @Autowired
  12. private Environment environment;
  13. @Bean(name = "getDataSource")
  14. public DataSource getDataSource() {
  15. DruidDataSource dataSource = new DruidDataSource();
  16. String surl = environment.getProperty("spring.datasource.url");
  17. System.out.println(surl);
  18. dataSource.setUrl(environment.getProperty("spring.datasource.url"));
  19. dataSource.setUsername(environment.getProperty("spring.datasource.username"));
  20. dataSource.setPassword(environment.getProperty("spring.datasource.password"));
  21. return dataSource;
  22. }
  23. //关闭资源
  24. public void close(Statement ps, Connection conn){
  25. try {
  26. if (ps != null && !ps.isClosed()) {
  27. ps.isClosed();
  28. }
  29. if (conn != null && !conn.isClosed()) {
  30. conn.close();
  31. }
  32. }catch (SQLException ex){
  33. System.out.println("[Error]" + ex.getMessage());
  34. }
  35. }
  36. public String executeScalar(String p_sql){
  37. String result="";
  38. Connection conn = null;
  39. Statement stmt = null;
  40. try {
  41. DataSource ds = (DataSource)SpringContextUtil.getBean("getDataSource");
  42. conn = ds.getConnection();
  43. stmt = conn.createStatement();
  44. ResultSet set = stmt.executeQuery(p_sql);
  45. if (set.next()){
  46. result = set.getString(1);
  47. }
  48. stmt.close();
  49. return result;
  50. } catch (SQLException e) {
  51. e.printStackTrace();
  52. } finally {
  53. close(stmt, conn);
  54. }
  55. return "";
  56. }
  57. public boolean executeSQL(String sql){
  58. Connection conn = null;
  59. Statement stmt = null;
  60. try {
  61. DataSource ds = (DataSource)SpringContextUtil.getBean("getDataSource");
  62. conn = ds.getConnection();
  63. stmt = conn.createStatement();
  64. stmt.execute(sql);
  65. stmt.close();
  66. conn.close();
  67. return true;
  68. }
  69. catch (Exception e){
  70. return false;
  71. }
  72. finally {
  73. close(stmt, conn);
  74. }
  75. }
  76. }

6. 创建控制器进行测试:

  1. package com.cx;
  2. import com.cx.common.DBUtilDruid;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7. @Controller
  8. public class MyController {
  9. @Autowired
  10. DBUtilDruid dbUtilDruid;
  11. @ResponseBody
  12. @RequestMapping("/getScalar")
  13. public String getScalar(){
  14. String sSql = "SELECT ActualString FROM dbo.DictTable WHERE D_ID = 1";
  15. return dbUtilDruid.executeScalar(sSql);
  16. }
  17. }

7. 在main函数中设置全局上下文:

8. 运行测试:

9. 遇到的问题,如果druid的版本不匹配将会有如下异常:

运行后产生异常:java.sql.SQLException: validateConnection false 恢复版本1.0.18即可正常运行;

附注,项目下载地址 访问SQLServer-springboot自定义DBUtil(druid连接池)操作SQLServer数据库-Java文档类资源-CSDN下载

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

闽ICP备14008679号