How to use global property value in connection string using groovy

折月煮酒 提交于 2019-12-25 05:13:07

问题


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')

回答1:


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')


来源:https://stackoverflow.com/questions/36858184/how-to-use-global-property-value-in-connection-string-using-groovy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!