中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > 第三方类库
Excel导入数据库工具包
作者:未知 时间:2005-07-24 21:26 出处:JR 责编:chinaitpower
              摘要:Excel导入数据库工具包
设计目的 
使用该工具包,能够将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

附件
工具包
关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有