最近做一个导入进度条的功能,不是上传文件,是execl导入里面每条每行的数据,用jdbc 和servlet在做,
本来用ajax 做的,而且我还要把插入失败的信息和你一共插入了多少行,还有进度条一起展示在一个页面上,而且用ajax做进度条要循环调用自己的函数,才能循环在后台得到jdbc dao层的影响行数,这样又跟我servlet 导入有冲突,请问怎么解决这个疑难杂症。

解决方案 »

  1.   

    jsp 的代码<%@ page language="java" import="java.util.*" pageEncoding="gbk"%><%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
    + request.getServerName() + ":" + request.getServerPort()
    + path + "/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <base href="<%=basePath%>"> <title>My JSP 'execlload.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <script src="js/prototype.js" type="text/javascript"></script>
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    <script type="text/javascript">
      
     function getValueIE8(id){
         var ip = document.getElementById(id);
          ip.select();
          var foo1 =  document.selection.createRange().text;
        
          var myAjax = new Ajax.Request(
           'ajaxServlet/insertcustomers.do?filename='+encodeURIComponent(foo1),
           {
               method: 'post',
               parameters:Form.serialize('form1'),
               
               evalScripts: true,
            
               onComplete:reportError
            });
            }
            function reportError(originalRequest){
             var myobj = originalRequest.responseText.evalJSON(true);
            
             var str = "<table>";
             str = str + "<tr style='color:red'>";
             str = str + "<td>以下数据导入失败,请重清空execl数据,重新导入<td><br />";
             str = str + "</tr>"
               myobj.each(function(myobj1){
               str = str + "<tr style='color:red'>";
                str = str +"<td>"+myobj1+"<td><br />";
                str = str + "</tr>";
               });
               str = str + "</table>";
               $("reportError").innerHTML = str;
            }
            </script>
    </head>
    <body>
    <form name="form1" id="form1">
    <input type="file" name="foo" id="foo" />
    <input type="button" value="execl导入" onclick="getValueIE8('foo')" />
    <input type="hidden" id="percent" value="0" size="2">
    <div id="reportError"> </div>
    </form>
    </body></html>
      

  2.   

    servlet  代码 public void insertExecl(HttpServletRequest request,
    HttpServletResponse response) {
    IExeclDao iexecldao = new ExeclDao();
    int a = 0;
    List list = new ArrayList();
    Sheet sheet = null; try {
    request.setCharacterEncoding("gb2312");
    response.setContentType("text/html");
    response.setCharacterEncoding("gb2312");
    String filename = request.getParameter("filename");
    System.out.println(filename);
    File file = new File(filename); Workbook workbook = Workbook.getWorkbook(file);
    sheet = workbook.getSheet(0);
    int col = sheet.getColumns();
    System.out.println(col);
    int row = sheet.getRows();
    System.out.println(row);
    String result[][] = new String[col][row];
    Cell cell;
    for (int i = 0; i < col; i++) {
    for (int j = 0; j < row; j++) {
    try {
    cell = sheet.getCell(i, j);
    result[i][j] = cell.getContents();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    } Customers_company customers_company = null;
    Customers_company_contact customers_company_Contact = null;
    Customers_company_address customers_company_address = null;
    for (int n = 1; n < row; n++) { // 循环execl表里的每一行
    customers_company = new Customers_company(); customers_company_Contact = new Customers_company_contact(); customers_company_address = new Customers_company_address(); if (iexecldao.findcustomers_company(result[0][n]) > 0) { // 判断表里面的公司名称是否和数据库里面的重复 String mess = file.getName() + "表中第" + (n + 1) + "行公司名为:"
    + result[0][n] + "数据库中以存在";
    list.add(mess); } else { customers_company.setCompany(result[0][n]); customers_company.setCompany_simple(result[1][n]); customers_company.setCompany_phone(result[2][n]); customers_company.setFax(result[3][n]); if (result[4][n].trim().equals("")) {// 如果没空,且给个默认值为0
    result[4][n] = "0";
    customers_company.setCustomers_company_id(Integer
    .parseInt(result[4][n]));
    } else {
    customers_company.setCustomers_company_id(Integer
    .parseInt(result[4][n]));
    }
    customers_company.setPark(result[5][n]);
    customers_company_Contact.setContact_name(result[6][n]);
    if (result[7][n].trim().equals("")) { // 如果为空,则给个默认日期
    Date d = new Date(Date.parse("1/10/1988"));
    customers_company_Contact.setContact_birthday(d);
    } else {
    Date d = new Date(Date.parse(result[7][n])); // 将输入的表里的日期格式转型
    customers_company_Contact.setContact_birthday(d); } customers_company_Contact.setContact_address(result[8][n]);
    customers_company_Contact.setContact_zip(result[9][n]);
    customers_company_Contact
    .setContact_telephone(result[10][n]);
    customers_company_Contact.setContact_email(result[11][n]);
    customers_company_Contact.setContact_msn(result[12][n]);
    customers_company_Contact.setContact_qq(result[13][n]);
    customers_company_Contact
    .setContact_department(result[14][n]);
    customers_company_Contact.setContact_post(result[15][n]);
    customers_company_Contact.setSign(result[16][n]);
    customers_company_Contact.setAccount(result[17][n]);
    customers_company_Contact.setPassword(result[18][n]);
    customers_company_Contact.setContact_hphone(result[19][n]);
    customers_company_Contact.setActive(result[20][n]);
    customers_company_Contact.setSex(result[21][n]);
    customers_company_Contact.setContact_city(result[22][n]);
    customers_company_Contact.setContact_area(result[23][n]);
    customers_company_Contact.setPermission(result[24][n]);
    customers_company_address.setCity(result[25][n]);
    customers_company_address.setArea(result[26][n]);
    customers_company_address.setAddress(result[27][n]);
    customers_company_address.setZip(result[28][n]);
    customers_company_address.setSign(result[29][n]); if (result[30][n].trim().equals("")) { // 如果为空则给个默认值为0
    result[30][n] = "0";
    customers_company_address.setAddress_type_id(Integer
    .parseInt(result[30][n]));
    } else {
    customers_company_address.setAddress_type_id(Integer
    .parseInt(result[30][n]));
    } a = iexecldao.insertExecl(customers_company,
    customers_company_Contact,
    customers_company_address); }
    }     PrintWriter out = response.getWriter();   
    JSONArray jsonArray = JSONArray.fromObject(list);
    out.println(jsonArray);
    out.flush();
    out.close();
        workbook.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
      

  3.   

     dao 层的代码public int insertExecl(Customers_company customers_company,
    Customers_company_contact customers_company_Contact,
    Customers_company_address customers_company_address) { try {
    con = ConnectionFactory.getConnection();
    sql = "insert into customers_company (company,company_simple,company_phone,fax,customers_id,park) values('"
    + customers_company.getCompany()
    + "','"
    + customers_company.getCompany_simple()
    + "','"
    + customers_company.getCompany_phone()
    + "','"
    + customers_company.getFax()
    + "','"
    + customers_company.getCustomers_company_id()
    + "','"
    + customers_company.getPark() + "')";
    sql1 = "select max(customers_company_id) as customers_company_id from customers_company"; con.setAutoCommit(false);
    st = con.prepareStatement(sql);
    num = st.executeUpdate(); st1 = con.prepareStatement(sql1);
    rs = st1.executeQuery();
    while (rs.next()) {
    sql2 = "insert into customers_company_contact(contact_name,contact_birthday,contact_address,contact_zip,contact_telephone,contact_email,contact_msn,contact_qq,contact_department,contact_post,sign,customers_company_id,account,password,contact_hphone,active,sex,contact_city,contact_area,permission) "
    + "values('"
    + customers_company_Contact.getContact_name()
    + "','"
    + customers_company_Contact.getContact_birthday()
    + "','"
    + customers_company_Contact.getContact_address()
    + "','"
    + customers_company_Contact.getContact_zip()
    + "','"
    + customers_company_Contact.getContact_telephone()
    + "','"
    + customers_company_Contact.getContact_email()
    + "','"
    + customers_company_Contact.getContact_msn()
    + "','"
    + customers_company_Contact.getContact_qq()
    + "','"
    + customers_company_Contact.getContact_department()
    + "','"
    + customers_company_Contact.getContact_post()
    + "','"
    + customers_company_Contact.getSign()
    + "','"
    + rs.getInt("customers_company_id")
    + "','"
    + customers_company_Contact.getAccount()
    + "','"
    + customers_company_Contact.getPassword()
    + "','"
    + customers_company_Contact.getContact_hphone()
    + "','"
    + customers_company_Contact.getActive()
    + "','"
    + customers_company_Contact.getSex()
    + "','"
    + customers_company_Contact.getContact_city()
    + "','"
    + customers_company_Contact.getContact_area()
    + "','"
    + customers_company_Contact.getPermission() + "')";
    st2 = con.prepareStatement(sql2); num1 = st2.executeUpdate();
    sql3 = "insert into customers_company_address(city,area,address,zip,sign,customers_company_id,address_type_id) values('"
    + customers_company_address.getCity()
    + "','"
    + customers_company_address.getArea()
    + "','"
    + customers_company_address.getAddress()
    + "','"
    + customers_company_address.getZip()
    + "','"
    + customers_company_address.getSign()
    + "','"
    + rs.getInt("customers_company_id")
    + "','"
    + customers_company_address.getAddress_type_id() + "')"; st3 = con.prepareStatement(sql3);
    num2 = st3.executeUpdate();
    }
    if (num > 0 && num1 > 0 && num2 > 0) {
    System.out.println(num);
    System.out.println(num1);
    System.out.println(num2);
    flag = true;
    con.commit();
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    try {
    con.rollback();
    } catch (SQLException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    e.printStackTrace();
    } finally {
    ConnectionClose.close(rs, st, con);
    if (st1 != null) {
    try {
    st1.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    if (st2 != null) {
    try {
    st2.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    if (st3 != null) {
    try {
    st3.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    return 0;
    }