문제

I am trying to execute a sql statement in groovy by passing the statement as a string variable rather than a string literal. The reason I am using a string variable is because I am reading the sql statement from a file.

For example (assuming sql is a valid Instance of groovy.sql.Sql - which I have verified):

sql.execute("insert into table(id) values(1)")

Works just fine.

However, the following does not:

def str = "insert into table(id) values(1)"
sql.execute(str)

The last example just hangs when I run it. No SQL errors, it just stalls. I tried putting a println after the execute and it never reaches the println statement.

So, I tried the following variations:

sql.execute("$str")

and

sql.execute("${str}")

and even

sql.execute("?", [str])

for curiosity's sake, but all give the following error:

Mar 21, 2013 6:28:16 PM groovy.sql.Sql execute
WARNING: Failed to execute: ? because: Invalid SQL type: sqlKind = 0
Caught: java.sql.SQLException: Invalid SQL type: sqlKind = 0
java.sql.SQLException: Invalid SQL type: sqlKind = 0
        at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:77)
        at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:1010)
        at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1315)
        at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3576)
        at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:3677)
        at oracle.jdbc.driver.OraclePreparedStatementWrapper.execute(OraclePreparedStatementWrapper.java:1374)
        at runSqlFile.run(runSqlFile.groovy:40)

I am admittedly new to groovy, but I can't figure out why a string literal works but the variable will not. Any help here would be greatly appreciated. Let me know if more information is needed.

Also, the reason I am grabbing individual sql statements from a file is because I could not find a way to execute an entire .sql file using groovy. If there is a simple way to do that which I haven't found, that would also solve my problem. Thanks in advance.

도움이 되었습니까?

해결책

Figured out the reason: I had SQL Developer open connected with the same credentials that I was using in my groovy script. I had executed a few commands in SQL Developer and neglected to commit them, which caused a conflict when running the groovy script. This was the cause of the stalling. As soon as I committed the changes made in SQL Developer, the script ran fine.

다른 팁

Well, this works fine with mySql, so my guess is that it's an issue with the Oracle JDBC driver. I've no experience with it myself, but I understand it's not the most reliable piece of software you can find.

This is a bit of a shot in the dark, but:

sql.execute("$str")
sql.execute("${str}")

In both these calls, the value passed to sql.execute isn't actually a String but a GStringImpl, which might have something to do with your issue.

About your last example:

sql.execute("?", [str])

This just isn't valid. ? will be interpreted as a value, not an SQL statement. What you're doing is passing a string, which happens to contain valid sql. The result if it contained "foobar" would be the same.

The only example for which I can't come up with anything is:

def str = "insert into table(id) values(1)"
sql.execute(str)

Are you sure you're not doing anything to str before passing it to sql.execute? Can you make sure it doesn't somehow become wrapped in a specialised groovy class such as GStringImpl ?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top