【问题标题】:Java compile error for a switch statementswitch 语句的 Java 编译错误
【发布时间】:2011-11-04 12:50:57
【问题描述】:

我已经写好了

public static void setIsolationLevel(Isolev level) {
    try{
        switch(level){
            case READ_UNCOMMITTED;
            conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
            break;
        }
    }catch (SQLException e){
        throw new ServiceException(e.getMessage());
    }
}

但它不能编译。它说它缺少:。问题是什么?我试图把“:”放在每个地方。

【问题讨论】:

  • 案例READ_UNCOMMITTED:

标签: java compilation


【解决方案1】:

你的问题是你有

case READ_UNCOMMITTED;

用分号。你需要一个冒号

case READ_UNCOMMITTED:

【讨论】:

  • 谢谢 :) 我一定是看太多了,因为我看不到 :)
【解决方案2】:

我相信编译器也会给你一个行号:

public static void setIsolationLevel(Isolev level) {
    try{
        switch(level){
        case READ_UNCOMMITTED: // <-------- here, replace ; with :
           conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
           break;
        }
    }catch (SQLException e){
        throw new ServiceException(e.getMessage());
    }

}

【讨论】:

    【解决方案3】:

    Java 中的 switch 语句如下:

    switch(thingy){
        case CASE1: 
           ...
        break;
        ...
    }
    

    【讨论】:

      【解决方案4】:

      case READ_UNCOMMITTED; 行需要更改为case READ_UNCOMMITTED:。注意冒号而不是分号。

      【讨论】:

        【解决方案5】:

        如果它只是 switch 中的一个子句,则不需要 switch - 你可以简单地使用'if'语句,这将使代码更清晰。

        public static void setIsolationLevel(Isolev level) {
            try{
                if(READ_UNCOMMITTED.equals(level)){
                    conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
                }
            }catch (SQLException e){
                throw new ServiceException(e.getMessage());
            }
        }
        

        【讨论】:

        • 谢谢 :) 我不知道
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-05-27
        • 1970-01-01
        • 2016-07-31
        • 2013-06-13
        • 2016-04-16
        • 2014-08-20
        • 2014-06-21
        相关资源
        最近更新 更多