【问题标题】:Groovy: how to use Exceptions or <If> <else> construction with MySQL queries?Groovy:如何在 MySQL 查询中使用 Exceptions 或 <If> <else> 构造?
【发布时间】:2012-11-27 06:35:56
【问题描述】:

我有一个来自 MySQL 数据库表的查询,我想检查一下:我是否在表中得到了这个数字,如果没有 - println 这个数字不在数据库中,如果这个数字存在 - println 这个数字存在。我怎么能做到这一点,使用异常或 (If) (else) 构造?

【问题讨论】:

    标签: mysql exception groovy if-statement


    【解决方案1】:

    假设您使用的是内存中的 hsql db:

    def sql = Sql.newInstance( 'jdbc:hsqldb:mem:testDB',      // JDBC Url
                               'sa',                          // Username
                               '',                            // Password
                               'org.hsqldb.jdbc.JDBCDriver')  // Driver classname
    
    def tim = 'tim'
    
    def name = sql.firstRow( "SELECT name FROM users WHERE userid=$tim" )?.name
    
    if( name ) {
      println "Name was $name"
    }
    else {
      println "Name not found"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-08
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多