如何使用groovy在连接字符串中使用全局属性值 [英] How to use global property value in connection string using groovy

查看:139
本文介绍了如何使用groovy在连接字符串中使用全局属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在连接中使用全局属性值,这样我就可以从一个地方改变它,它将应用于我的所有脚本中。
这是我的连接字符串,它可以正常工作,当我把IP直接放入字符串中时, .expand('$ {DB_Name}')
def dbUser = context.expand('$ {DB_Username}')
def dbPass = context.expand('$ {DB_Password}')
def con = Sql.newInstance(jdbc:sqlserver://192.168.111.111:1433;+databaseName =+ dbName,dbUser,dbPass,'com.microsoft.sqlserver.jdbc.SQLServerDriver')

但是,当我把全局属性,它会抛出错误TCP / IP连接到主机'192.168.111.111',端口1433失败。错误:null。验证连接属性。确保主机上正在运行SQL Server实例并接受端口上的TCP / IP连接。 。

  def dbServer = context.expand(' $ {DB_Server}')
def dbPort = context.expand('$ {DB_Port}')
def dbName = context.expand('$ {DB_Name}')
def dbUser = context .expand('$ {DB_Username}')
def dbPass = context.expand('$ {DB_Password}')
def con = Sql.newInstance(jdbc:sqlserver://'$ dbServer' :1433;+databaseName =+ dbName,dbUser,dbPass,'com.microsoft.sqlserver.jdbc.SQLServerDriver')


解决方案

不知道为什么你把 $ dbServer 地址放在引号中,它应该是

  def con = Sql.newInstance(jdbc:sqlserver:// $ dbServer:1433;+databaseName =+ dbName,dbUser ,dbPass,'com.microsoft.sqlserver.jdbc.SQLServerDriver')

或者(模板化所有的东西,而不仅仅是服务器)

  def co n = Sql.newInstance(jdbc:sqlserver:// $ dbServer:1433; databaseName = $ dbName,dbUser,dbPass,'com.microsoft.sqlserver.jdbc.SQLServerDriver')


I tried to use global property value in connection so that i can change it from one place and it will apply in all my script. This is my connection string and it works fine when i put the IP directly in the string

def dbName = context.expand( '${DB_Name}' )
def dbUser = context.expand( '${DB_Username}' )
def dbPass = context.expand( '${DB_Password}' )
def con = Sql.newInstance("jdbc:sqlserver://192.168.111.111:1433;" + "databaseName=" + dbName, dbUser, dbPass, 'com.microsoft.sqlserver.jdbc.SQLServerDriver')

But when i put the global properties, it throws error "The TCP/IP connection to the host '192.168.111.111', port 1433 has failed. Error: "null. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall."."

def dbServer = context.expand( '${DB_Server}' )
def dbPort = context.expand( '${DB_Port}' )
def dbName = context.expand( '${DB_Name}' )
def dbUser = context.expand( '${DB_Username}' )
def dbPass = context.expand( '${DB_Password}' )
def con = Sql.newInstance("jdbc:sqlserver://'$dbServer':1433;" + "databaseName=" + dbName, dbUser, dbPass, 'com.microsoft.sqlserver.jdbc.SQLServerDriver')

解决方案

Not sure why you've put the $dbServer address in quotes, it should be

def con = Sql.newInstance("jdbc:sqlserver://$dbServer:1433;" + "databaseName=" + dbName, dbUser, dbPass, 'com.microsoft.sqlserver.jdbc.SQLServerDriver')

Or (templating all the things, not just the server)

def con = Sql.newInstance("jdbc:sqlserver://$dbServer:1433;databaseName=$dbName", dbUser, dbPass, 'com.microsoft.sqlserver.jdbc.SQLServerDriver')

这篇关于如何使用groovy在连接字符串中使用全局属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆