JR 精品文章 - Excel导入数据库工具包
AD: jr (at) javaresearch.org


首页 | 动态 | 文章 | FAQ  | 新闻 | 下载 | 代码 | 工作 | 调查 | 术语 | 站点 | 图书 | 论坛 | 帮助 | 全部  

TOP | 交流 | 软件 | 专栏 | 开源 | 译/著 | 源码 | API  | 推荐 | FTP  | 积分 | 统计 | 搜索 | Blog | 我们  
首页 » 研究文集 » 第三方类库 搜索标题相关文章 搜索标题相关文章    评论此文章 发表评论     开始监控此文章 开始监控   加入收藏夹  加入收藏夹
Excel导入数据库工具包
efly 原创   更新:2002-11-04 09:36:49  版本: 1.0   

设计目的 
使用该工具包,能够将MS Excel表格数据导入到具有相应结构的关系型数据库表格。通过扩展接口能对Excel表格的每一列或行的数据进行操作,剔除坏数据,修复或者替换特定单元的数据,从而实现了Excel数据自由导入具有外键关联或引用的表格。该工具包在导入数据的同时还能生成结构简单的Xml数据文件。

类结构

 
使用说明
扩展接口com.uncnet.dbexcel.Flopper,实现相应的方法
方法:String conversionOp(
        Connection connection,
        String arg,
        int index,
        Document doc,
        Element table,
        Element row)
        throws Exception;
描述:对Excel列的单元进行数据转换并操作数据库表格。
参数:Connection connection,数据库连接;
      String arg,Excel列单元的数据;
      int index,Excel表格列序号,从1开始;
      Document doc,UNC Xml的Document元素;
      Element table,UNC Xml的元素;
      Element row,UNC Xml的元素。
返回:经过从Excel Xml到UNC Xml转换的数据。
代码示例:
/**
     * @see com.uncnet.dbexcel.Flopper#conversionOp(Connection, String, int, Document, Element, Element)
     */
    public String conversionOp(
        Connection connection,
        String arg,
        int index,
        Document doc,
        Element table,
        Element row) {
        String text = null;
        if (arg == null)
            arg = "";
        try {
            if (connection == null)
                return "No Connection";
            stmt = connection.createStatement();
            if (col_num++ % COLUMNS == 0)
                ps_in = new String[COLUMNS + 1];

            switch (index) {
                case 1 :
                    {
                        text = arg;
                        ps_in[0] = arg;
                        break;
                    }
                case 8 :
                    {
                        text = arg;
                        ps_in[8] = findForDict(null, arg, stmt, 8);
                        break;
                    }
                case 10 :
                    {
                        text = arg;
                        if (!arg.equals(""))
                            ps_in[10] = arg.substring(1);
                        else
                            ps_in[10] = arg;
                        break;
                    }
                default :
                    {
                        text = arg;
                        ps_in[index] = arg;
                    }
            }
            if (col_num % COLUMNS == 0)
                isToCmt = true;
            else
                isToCmt = false;
            if (isToCmt) {
                pstmt = connection.prepareStatement(sqlscript);
                connection.setAutoCommit(false);
                row_num++;
                boolean isToExe = true;
                try {
                    Integer.parseInt(ps_in[0]);
                    Integer.parseInt(ps_in[1]);
                } catch (NumberFormatException nfe) {
                    isToExe = false;
                }
                StringTokenizer ster = null;
                Date date = null;
                int year, month, day;
                for (int i = 0; i < COLUMNS + 1; i++) {
                    if (i == 12 || i == 13) {
                        ster = new StringTokenizer(ps_in[i], "-");
                        year = Integer.parseInt(ster.nextToken());
                        month = Integer.parseInt(ster.nextToken());
                        day = Integer.parseInt(ster.nextToken());
                        date = new Date(year, month, day);
                        pstmt.setDate(i + 1, date);
                    } else
                        pstmt.setString(i + 1, ps_in[i]);
                    System.err.print(ps_in[i] + " ");
                }
                System.err.println();
                if (isToExe) {
                    pstmt.execute();
                    connection.commit();
                } else
                    System.err.print("Encountering null.");
                connection.setAutoCommit(true);
                if (pstmt != null)
                    pstmt.close();
            }
        } catch (Exception e) {
            e.printStackTrace(System.err);
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
                if (isToCmt && pstmt != null)
                    pstmt.close();
            } catch (Exception e) {
                System.err.println(e);
            }
        }
        if (text == null) {
            table.removeChild(row);
        }

        return text;
    }

方法:void setParameter(String[] para);
描述:设置程序参数
参数:String[] para

方法:Connection getConnection();
描述:建立数据库连接
返回:数据库连接

方法:void closeConnection(Connection conn);
描述:关闭数据库连接
参数:Connection conn,数据库连接

方法:void beginConvert();
描述:开始转换前的操作

方法:void endOfConvert();
描述:转换完成后的操作

方法:void afterConnected(Connection conn);
描述:数据库建立连接后的操作

方法:void beforeClosed(Connection conn);
描述:关闭数据库连接前的操作

接口Flopper扩展类
类:com.uncnet.dbexcel.flopper.DBFlopper
描述:实现了数据库的连接与关闭和参数的设置

类:com.uncnet.dbexcel.flopper.DictFlopper
描述:字典类,实现了从文件查找字典表的对应数据

程序的执行
主程序入口:com.uncnet.dbexcel.TextController的main方法
调用参数:Excel Xml文件路径,输出UNC Xml文件路径,Flopper扩展类名字空间,其它扩展参数...
参数示例:
F:\work4\9-23\expert.xml F:\work4\9-23\_expert2.xml com.uncnet.dbexcel.flopper.YpzbExpertFp oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@127.0.0.1:1521:oracle scott tiger F:\work4\9-23\data\diploma.txt F:\work4\9-23\data\hospital.txt

附件
工具包


版权声明   给作者写信
本篇文章对您是否有帮助?  投票:         投票结果:     21       2
作者其它文章: 作者全部文章
评论人:讨厌注册 发表时间: Wed Nov 20 10:20:22 CST 2002
?////////
评论人:讨厌注册 发表时间: Wed Nov 20 10:22:06 CST 2002
不好用阿
评论人:efly 发表时间: Wed Nov 20 11:37:14 CST 2002
TO:楼上兄弟
这个包是我工作中用到并开发的,能满足我工作的需求。
如果需要帮助,请跟我联系MSN:jndi@163.com
评论人:looya 发表时间: Wed Dec 01 14:21:50 CST 2004
搞个说明文档和示例也好!
评论人:帅哥王 发表时间: Mon Feb 21 16:03:40 CST 2005
楼主,您好!我是新注册的会员,下载不了你的工具包,您能发送到我的邮箱来吗?谢谢!
grant1999@163.com
评论人:hujb 发表时间: Wed Feb 23 10:04:17 CST 2005
我也是新会员,e-mail:hujianbang2000@sohu.com ,谢谢了!
评论人:Bruce.Zhong 发表时间: Thu Mar 17 13:33:49 CST 2005
功能还是很实在的,不过还没有用过
评论人:anday7423 发表时间: Tue Aug 16 06:08:47 CST 2005
老大,我是新人,能不能发一个工具包给我anday7423@yahoo.com,谢谢
评论人:tenyee21cn 发表时间: Thu Aug 25 18:38:30 CST 2005
楼主,好不容易baidu到此,结果不能下?
能发一个到我邮箱吗?君子成人之美!多谢了!
tenyee@21cn.com[:P]
评论人:yangchunlei8ii 发表时间: Wed Nov 08 19:27:52 CST 2006
好[cop]
评论人:qianliangmin 发表时间: Wed May 30 20:29:24 CST 2007
不知道要有什么条件才能下载兄弟的代码

这个文章共有 11 条评论
主题: 克隆对象 上一篇文章
返回文章列表 返回〔第三方类库〕
下一篇文章 主题: JBoss EJB CMP2性能测试


文字广告链接
        自主、快速定制基于JAVA的B/S业务系统          重量级企业在线自定义WEB报表平台
        Excel制表、零代码发布、打印、图表结合——快逸报表,免费、稳定、功能强大的java工具
        技术圈: 关于Java、dotNet、PHP、Ruby、奇客、Web2.0等更多资讯博客精选文章

关于 JR  |  版权声明  |  联系我们 

©2002-2006 JR 版权所有 沪ICP备05019622号