以下程序的返回类型?

时间:2015-10-30 12:14:38

标签: java servlets jdbc

这个程序的返回类型是什么?

我不知道必须给以下程序什么样的返回类型。它用于同时发送多行。

public class TimeSheetDao {
    public static final String sql= "insert into logintable values(?,?,?,?,?,?,?,?,?,?)";

    public static int insert(List<EmployeeBean> ebList) throws Exception {
        PreparedStatement ps=null;
        System.out.println("In TimesheetDao");
        Connection conn=ConnectionProvider.getConn();
        System.out.println("before sql");

        System.out.println("after sql");
        ps = conn.prepareStatement(sql);
        try { 
            System.out.println(" in try in Timesheetdao");
            conn.setAutoCommit(false);
            for  (EmployeeBean logintable: ebList){
                ps.setString(1,logintable.getEmpid());
                ps.setDate(2,new Date(logintable.getLogindate().getTime()));
                ps.setString(3,logintable.getLogintime());  
                ps.setString(4,logintable.getLogouttime()); 
                ps.setString(5,logintable.getLunch());
                ps.setString(6,logintable.getAfterlunchlogin());  
                ps.setString(7,logintable.getAfterlunchlogout()); 
                ps.setString(8,logintable.getTask());
                ps.setString(9,logintable.getTotal());
                ps.setString(10,logintable.getOvertime());
                ps.addBatch();
            }
            int i[]= ps.executeBatch();
            conn.commit();
        } 
        catch (SQLException e) {
            System.out.println(e.getMessage());
            conn.rollback();
        } 
        finally { 
            if (conn != null) {  
                try {  
                    conn.close();  
                } catch (SQLException e) {  
                    e.printStackTrace();  
                }  
            }
            if (ps != null) {  
                try {  
                    ps.close();  
                } catch (SQLException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  

        // What should I return here ?
        return  ?;
    }
}

我知道返回类型应该是int类型。但是int i [] = ps.executeBatch();不能归还所以我不知道。

3 个答案:

答案 0 :(得分:1)

为了帮助您入门,请阅读the docs关于i内容含义的内容:

  

包含一个元素的更新计数数组   对于批处理中的每个命令。   数组的元素按顺序排序   其中命令已添加到批处理中。

当您在方法的EmployeeBean参数中按ebList提交一个命令时,您可能希望在[{1}}上返回总和。

然而,您的班级设计可以给出唯一明确的答案

答案 1 :(得分:0)

返回类型应为int(整数)。您可以在public static int...声明

中看到这一点

答案 2 :(得分:0)

我会返回boolean,true表示成功插入,false表示失败。

public static boolean insert(List<EmployeeBean> ebList) throws Exception {
    ...
    try{
        ...
        int i[]= ps.executeBatch();
        conn.commit();

        return true;
    }
    catch (SQLException e) {
        System.out.println(e.getMessage());
        conn.rollback();
        return false;
    }
    finally {
        ...
    }
}