好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java中String的JdbcTemplate连接SQLServer数据库的方法

  很久没写文章了,一方面是最近几个月比较忙,没太多时间,另一方面是最近拖延症严重,写文章的想法总是一拖再拖。今天找一个小案例写一下,与懒惰对抗一下。

  首先说一下背景,我们在项目中做数据持久化一般都是用mybatis或者hibernate开发框架,进行数据库连接和操作,最近做GIS仿真产品研发,根据需求需要保存三部分数据:1、业务数据,数据量比较小;2、GIS数据,需要用到空间关系;3、物联数据,数据量大,在我们开发自测阶段数据量就可以达到每天百万以上。根据以上数据特点,我们使用了传统的MySQL数据库、空间数据库PostgreSQL、TD engine时序数据库,项目中做了spring boot多数据源动态切换。今天的重点不是多数据源的实现,这个应用以后会在另外一篇文章中介绍;在研发过程中我们需要与其他系统对接的场景,连接SQLServer拉取数据,项目本身已经做了多数据源,如果继续添加数据源就加大了系统的难度,所以就用jdbc连接数据库的方式连接外部数据源,下面看代码。

一、引入依赖jar包

  项目用的是Spring Boot,创建好项目以后,引入下面依赖:

?

1

2

3

4

5

6

7

8

9

<dependencies>

        

         <dependency>

             <groupId>com.microsoft.sqlserver</groupId>

             <artifactId>mssql-jdbc</artifactId>

             <scope>runtime</scope>

         </dependency>

 

     </dependencies>

二、Utils开发

  1、创建实体类,实现org.springframework.jdbc.core.RowMappe接口的mapRow(ResultSet rs, int rowNum)方法。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

package com.johan.handler.task.iotSync.bean;

import lombok.Data;

import org.apache测试数据mons.lang3.StringUtils;

import org.springframework.jdbc.core.RowMapper;

 

import java.sql.ResultSet;

import java.sql.SQLException;

 

/**

  * @author: Johan

  * @date: 2021/10/18

  * @desc: IOT信息

  */

@Data

public class IotDG implements RowMapper {

     /**

      * 标签名称

      */

     private String tagName;

 

     /**

      * 标签描述

      */

     private String tagDesc;

 

     /**

      * 标签值

      */

     private Double tagVal;

 

     /**

      * 标签单位

      */

     private String tagUnit;

 

     /**

      * 类型,0 压力,1 流量

      */

     private String type;

 

 

     @Override

     public Object mapRow(ResultSet rs, int rowNum) throws SQLException {

         IotDG iotDG = new IotDG();

         iotDG.setTagName(rs.getString( "TagName" ));

         iotDG.setTagDesc(rs.getString( "TagDesc" ));

         iotDG.setTagVal(rs.getDouble( "Value" ));

         iotDG.setTagUnit(rs.getString( "TagUnit" ));

         iotDG.setType(rs.getString( "Type" ));

 

         return iotDG;

     }

}

  2、连接数据库,读取表数据

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

package com.johan.handler.task.iotSync.iotConvert;

 

import com.johan.handler.task.iotSync.bean.IotDG;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.jdbc.datasource.DriverManagerDataSource;

 

import java.sql.*;

import java.util.List;

 

/**

  * @author johan

  * @Description SQL server

  * @time 2021/10/18 18:26

  */

public class JDBCUtils {

 

     private static JdbcTemplate jdbcTemplate;

     static {

         String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver" ;

         String url = "jdbc:sqlserver://10.25.23.172:1433;databaseName=SCADA_Data_3040" ;//连接地址

         String user = "SLSL" ; //用户

         String password = "Admin@3040" ; //密码

 

         DriverManagerDataSource dataSource= new DriverManagerDataSource();

         dataSource.setUrl(url);

         dataSource.setDriverClassName(driver);

         dataSource.setUsername(user);

         dataSource.setPassword(password);

 

         jdbcTemplate= new JdbcTemplate(dataSource);

     }

 

 

     public static List<IotDG> listAll( int type){

         String sql = "SELECT * FROM RealData where Type=" + type;

 

//        System.out.println(iotDGList);

         return jdbcTemplate.query(sql, new IotDG());

     }

 

 

}

  3、测试

  方法是静态的,直接调用即可。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

package com.johan.domain.iot;

 

import com.johan.handler.task.iotSync.bean.IotDG;

import com.johan.handler.task.iotSync.iotConvert.JDBCUtils;

import lombok.extern.slf4j.Slf4j;

import org.junit.jupiter.api.Test;

import org.springframework.boot.test.context.SpringBootTest;

 

import java.util.List;

 

/**

  * @author johan

  * @time 2021/10/22 17:57

  */

@SpringBootTest

@Slf4j

public class IotDomainTest {

 

     @Test

     public void jdbcTest(){

         List<IotDG> iotDGList = JDBCUtils.listAll( 0 );

         System.out.println(iotDGList);

     }

 

}

  不只是SQLServer,我们常用的MySQL、Oracle等都可以用JdbcTemplate连接。

到此这篇关于Java中String的JdbcTemplate连接SQLServer数据库的文章就介绍到这了,更多相关java中JdbcTemplate连接SQLServer数据库内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://HdhCmsTestcnblogs测试数据/JohanChan/p/15469453.html

查看更多关于Java中String的JdbcTemplate连接SQLServer数据库的方法的详细内容...

  阅读:20次