【发布时间】:2013-03-14 14:11:08
【问题描述】:
我刚刚在尝试修改 MySQL 数据库时在 Groovy 中遇到了一个令人困惑的问题。除非我的 GroovyString 显式转换为 java.lang.String,否则看似相同的代码会引发异常:
import groovy.sql.Sql
def sql = Sql.newInstance('jdbc:mysql://localhost/test?useUnicode=yes&characterEncoding=UTF-8', 'user', 'pass', 'com.mysql.jdbc.Driver')
def tableName = 'my_table'
sql.execute "truncate $tableName"
抛出:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''my_table'' at line 1
而以下工作没有问题:
sql.execute "truncate $tableName".toString()
这令人惊讶。我是否应该预料到这个问题,如果是这样,在什么情况下GroovyString 和String 实例可能会被区别对待?
【问题讨论】:
标签: mysql groovy mysql-connector groovy-sql