Forum Discussion

manthena2020's avatar
2 years ago

Reading custom data sources error Messages

Hi please find the below code

import groovy.sql.Sql
import java.sql.SQLException

// Register the Sybase driver
Class.forName("com.sybase.jdbc4.jdbc.SybDriver")

// Get basic info to connect
def hostname = hostProps.get("system.hostname")
def user = hostProps.get("sybase.user")
def pass = hostProps.get("sybase.pass")
def port = 21000
def wildvalue = instanceProps.get("wildvalue")
def url = "jdbc:sybase:Tds:" + hostname + ":21000"
def driver = "com.sybase.jdbc4.jdbc.SybDriver"

def sql = Sql.newInstance(url, user, pass, driver)

// Check Sybase server status
try {
sql.getConnection()
println("status:0") // Server is online
} catch (SQLException e) {
if (e.getMessage().contains("Login failed")) {
println("status:1") // Invalid password or login failed
} else {
println("status:2") // Other connection or server error, including server offline
}
} finally {
sql.close()
}



when i run this code . iam getting status 0 as my output. i want to test how this script works for login failed. i have tried this code with wrong credentials then it is not printing any thing its showing error message

 

code:

The script failed, elapsed time: 0 seconds - JZ00L: Login failed.  Examine the SQLWarnings chained to this exception for the reason(s).java.sql.SQLException: JZ00L: Login failed.  Examine the SQLWarnings chained to this exception for the reason(s).	at com.sybase.jdbc4.jdbc.ErrorMessage.raiseError(ErrorMessage.java:775)	at com.sybase.jdbc4.tds.Tds.processLoginAckToken(Tds.java:5382)	at com.sybase.jdbc4.tds.Tds.doLogin(Tds.java:725)	at com.sybase.jdbc4.tds.Tds.login(Tds.java:579)	at com.sybase.jdbc4.jdbc.SybConnection.tryLogin(SybConnection.java:422)	at com.sybase.jdbc4.jdbc.SybConnection.handleHAFailover(SybConnection.java:3335)	at com.sybase.jdbc4.jdbc.SybConnection.<init>(SybConnection.java:348)	at com.sybase.jdbc4.jdbc.SybConnection.<init>(SybConnection.java:253)	at com.sybase.jdbc4.jdbc.SybDriver.connect(SybDriver.java:232)	at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)	at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)	at groovy.sql.Sql.newInstance(Sql.java:422)	at groovy.sql.Sql.newInstance(Sql.java:466)	at groovy.sql.Sql$newInstance.call(Unknown Source)	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:152)	at Script272.run(Script272.groovy:16)


so please help to how to avoid the error mesage and print status 2

  • Anonymous's avatar
    Anonymous

    Please insert code blocks using the code block option in the toolbar.

    You should be setting the return code in a variable in your try/catch/finally block and returning that error code. Then you can just have a datapoint that looks at the return code. That also solves the problem of your current code not having a return code, which will cause LM to ignore the output.

    return_code = 0
    try {
    sql.getConnection()
    } catch (SQLException e) {
    if (e.getMessage().contains("Login failed")) {
    return_code = 1 // Invalid password or login failed
    } else {
    return_code = 2 // Other connection or server error, including server offline
    }
    } finally {
    sql.close()
    return return_code
    }
  • Anonymous's avatar
    Anonymous

    No need for a print statement unless you have something other than the return code you want to return.

    The toolbar is right above the text you’re typing. It has bold, italic, underline, etc. On the right, there are three dots and that has a code widget you can insert.

  • import groovy.sql.Sql
    import java.sql.SQLException

    // Register the Sybase driver
    Class.forName("com.sybase.jdbc4.jdbc.SybDriver")

    // Get basic info to connect
    def hostname = hostProps.get("system.hostname")
    def user = hostProps.get("sybase.user")
    def pass = hostProps.get("sybase.pass")
    def port = 21000
    def wildvalue = instanceProps.get("wildvalue")
    def url = "jdbc:sybase:Tds:" + hostname + ":21000"
    def driver = "com.sybase.jdbc4.jdbc.SybDriver"

    def sql = Sql.newInstance(url, user, pass, driver)

    // Check Sybase server status
    return_code = 0
    try {
    sql.getConnection()
    } catch (SQLException e) {
    if (e.getMessage().contains("Login failed")) {
    return_code = 1 // Invalid password or login failed
    } else {
    return_code = 2 // Other connection or server error, including server offline
    }
    } finally {
    sql.close()
    return return_code
    }

    i am getting 0 , even for Login failed as well as. Please look into it

  • Anonymous's avatar
    Anonymous

    The only thing I can guess is that when groovy tries the sql.getConnection(), it’s resolving to true even if login fails. You’ll need to figure out something additional that determines if login was successful or not.