需求:往表中插入数据,如果插入成功,返回 1
      如果插入失败返回 2我对插入代码进行了try catch在try中return 1在catch中return 2这样操作失败...请问该如何操作?

解决方案 »

  1.   


    public int executeUpdate(String sql) throws Exception 
        { 
            try{ 
                dbConn = getConn() ;   //get connection
                if( dbConn == null) 
                    return -1; 
                if( stmt != null )   { 
                    stmt.close() ; 
                    stmt = null ; 
                } 
                stmt= dbConn.createStatement();  //get statement
                if(stmt == null) 
                    return -2; 
                stmt.executeUpdate(sql);    //execute
                return 0; 
            } catch(Exception e) { 
                System.out.println( "executeUpdate[ "+ sql + "] fail. " + e.getMessage() ); 
                return -99; 
            } finally { 
                try { 
                    if(stmt!=null){ 
                        stmt.close(); 
                        stmt = null; 
                    } 
                }catch(Exception e) { 
                    System.out.println( "Err: executeUpdate() close stmt fail. ") ; 
                    return -88; 
                } 
            } 
        } 与这类似,看书看着怎么修改好。
      

  2.   

    定义一个int flag,在需要的位置修改这个flag, 最后在方法最再return flag
      

  3.   

    代码:
                      int flag;
    try {
    this.insertObj(patient);
    flag = 1;
    } catch (Exception e) {
    System.out.println(e.toString());
    flag = 2;
    }
    return flag;
    有异常后程序就不再运行了 怎样可以满足我的需求?
      

  4.   


    你可以在catch里进行返回的。
      

  5.   


    public static int insert() {
    int flag = 1;
    try
    {
    /**
    /* 相关操作
     */
     throw new Exception("asdf");
    }
    catch (Exception e)
    {
    flag = 2;
    }
    return flag;


    }
      

  6.   


    有异常之后,try块中的其他代码就不再运行,直接跳到catch块中,catch块中的代码还是可以运行的,所以这个方法是没有问题的
      

  7.   

    有没有debug一下啊? 感觉不可能没有返回,应该是insertObj方法里面死循环了吧?