背景:
測試某台機器能否連接sqlserver
1 首先安裝sqlserver 2008:
https://www.cnblogs.com/yzl050819/p/8284242.html
首次通過sql server 2008 managerment studio登錄服務器為 機器名\SQLEXPRESS
2 配置sqlserver登錄用戶(此前為windows)
https://www.cnblogs.com/chlyA-F/p/6075675.html
1)左側的對象資源管理器->安全性->登錄名,右擊sa->屬性,為sa用戶添加密碼,選擇sql server身份驗證,在“狀態”項中授予連接到數據庫和登錄啟用;
2)右擊對象資源管理器的根節點,選擇屬性->安全性->sql server和windows身份驗證模式,然后就這樣
3 配置遠程連接及端口
sql server configuration manage - sql server網絡適配-sqlexpress的協議-TCP/IP(啟用)-ip地址-IPALL-TCP端口1433
studio-根節點屬性-連接-允許遠程連接勾選
4 spring boot配置數據源
https://www.cnblogs.com/wang-yaz/p/9561188.html
spring.datasource.second.jdbc-url=jdbc:sqlserver://xxxxx:1433;DatabaseName=test spring.datasource.second.username=xxx spring.datasource.second.password=xxxx spring.datasource.second.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver 注意1:連接數據庫的方式不一樣,mysql是/test ,sqlServer是;DatabaseName=test spring.datasource.url=jdbc:mysql://xxxx/test spring.datasource.second.jdbc-url=jdbc:sqlserver://xxxxx:1433;DatabaseName=test
5 測試代碼
https://blog.csdn.net/TTTTTdzhao/article/details/81627023
try { JdbcTemplate jdbcTemplate = (JdbcTemplate)SpringUtil.getBean("secondaryJdbcTemplate"); Connection connection = jdbcTemplate.getDataSource().getConnection(); if(connection != null) { logger.info("sqlserver 數據庫連接成功!"); } else { logger.info("sqlserver 數據庫連接失敗!"); } List list = jdbcTemplate.queryForList("select * from master.dbo.MSreplication_options"); logger.info("sql 輸出{}", list); } catch (Exception e) { logger.error(e.getMessage()); }
代碼:
/** * Created by xxx on 19/7/11. */ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; /** * 取得spring上下文工具類 * Created by xxx on 18/6/21. */ @Component public class SpringUtil implements ApplicationContextAware { private static Logger LOGGER = LoggerFactory.getLogger(SpringUtil.class); private static ApplicationContext applicationContext = null; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { if(SpringUtil.applicationContext == null){ SpringUtil.applicationContext = applicationContext; } LOGGER.info("---------------Spring Application Context---------------"); } public static ApplicationContext getApplicationContext() { return applicationContext; } public static Object getBean(String name){ return getApplicationContext().getBean(name); } public static <T> T getBean(Class<T> clazz){ return getApplicationContext().getBean(clazz); } public static <T> T getBean(String name,Class<T> clazz){ return getApplicationContext().getBean(name, clazz); } }
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import javax.sql.DataSource; @Configuration public class SqlServerDataSourceConfig { @Bean(name = "sqlServerDataSource") @Qualifier("sqlServerDataSource") @ConfigurationProperties(prefix="spring.datasource.second") public DataSource getMyDataSource(){ return DataSourceBuilder.create().build(); } @Bean(name = "mysqlDataSource") @Qualifier("mysqlDataSource") @ConfigurationProperties(prefix="spring.datasource.out") public DataSource getOutDataSource(){ return DataSourceBuilder.create().build(); } @Bean(name = "secondaryJdbcTemplate") public JdbcTemplate secondaryJdbcTemplate( @Qualifier("sqlServerDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } @Bean(name = "mysqlJdbcTemplate") public JdbcTemplate mysqlJdbcTemplate( @Qualifier("mysqlDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } }
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.jdbc.core.JdbcTemplate; import java.sql.Connection; import java.sql.SQLException; import java.util.List; @SpringBootApplication public class UserApplication { private static Logger logger = LoggerFactory.getLogger(UserApplication.class); public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); try { JdbcTemplate jdbcTemplate = (JdbcTemplate)SpringUtil.getBean("secondaryJdbcTemplate"); Connection connection = jdbcTemplate.getDataSource().getConnection(); if(connection != null) { logger.info("sqlserver 數據庫連接成功!"); } else { logger.info("sqlserver 數據庫連接失敗!"); } List list = jdbcTemplate.queryForList("select * from vvvvv"); logger.info("sql 輸出{}", list); } catch (Exception e) { logger.error(e.getMessage()); } try { JdbcTemplate jdbcTemplate = (JdbcTemplate)SpringUtil.getBean("mysqlJdbcTemplate"); Connection connection = jdbcTemplate.getDataSource().getConnection(); if(connection != null) { logger.info("mysql 數據庫連接成功!"); } else { logger.info("mysql 數據庫連接失敗!"); } } catch (Exception e) { logger.error(e.getMessage()); } } }
spring.datasource.second.jdbc-url=jdbc:sqlserver://192.168.xx.xx:1433 spring.datasource.second.username=sa spring.datasource.second.password=xxxxxxx spring.datasource.second.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver spring.datasource.out.jdbcUrl=jdbc:mysql://192.168.xx.xx:3306?useUnicode=true&characterEncoding=utf8 spring.datasource.out.driverClassName=com.mysql.jdbc.Driver spring.datasource.out.username=xxx spring.datasource.out.password=xxx