问题
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