⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dccmarshal.java

📁 这是用于数据移植的原码(SYBASE-->DB2)
💻 JAVA
字号:
package com.pansky.dcc.core;

import java.io.*;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: 周宇
 * Date: 2005-6-30
 * Time: 13:55:04
 * To change this template use File | Settings | File Templates.
 */
public class DCCMarshal {

    public static void runGet(String table, Object obj) {

        ResourceBundle sqlRb = ResourceBundle.getBundle("SQL");
        ResourceBundle dccRb = ResourceBundle.getBundle("DCC");
        ResourceBundle tableRb = ResourceBundle.getBundle(table);
        String delimiter = dccRb.getString("DELIMETER");
        String tempPath = dccRb.getString("TEMP_DIR");
        String sql = sqlRb.getString(table);
        if (sql == null) {
            System.out.println("Table:".concat(table).concat(" was not set sql,do next!"));
        }
        try {
            Connection con = DbTool.getConnectionLink1();
            Statement st = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet rs = st.executeQuery(sql);
            rs.setFetchSize(1000);
            File file = new File(new StringBuffer(tempPath).append("/").append(table).append(".").append("dat").toString());
            FileWriter fw = new FileWriter(file);
            for (Enumeration en = tableRb.getKeys(); en.hasMoreElements();) {
                String key = (String) en.nextElement();
                fw.write(key);
                if (en.hasMoreElements()) {
                    fw.write(delimiter);
                } else {
                    //换行
                    fw.write("\r\n");
                }
            }
            int count = 0;
            int countALL = 0;
            while (rs.next()) {
                //ResultSetMetaData rsMeta =rs.getMetaData();
                count++;
                countALL++;
                for (Enumeration en = tableRb.getKeys(); en.hasMoreElements();) {
                    String key = (String) en.nextElement();
                    //warning,顺序问题
                    String value = tableRb.getString(key);

                    //如果是funcation,执行function
                    if (checkFunction(value)) {
                        fw.write(runFunction(value, rs, obj));
                    }
                    //如果是常量
                    else if (checkConstant(value)) {
                        fw.write(value.replace('\'', ' ').trim());
                    } else {
                        //处理null
                        if (rs.getObject(value) != null && rs.getObject(value).toString().length() != 0) {
                            fw.write(rs.getObject(value).toString());
                        } else {
                            fw.write("NULL");
                        }
                    }

                    if (en.hasMoreElements()) {
                        fw.write(delimiter);
                    } else {
                        //换行
                        fw.write("\r\n");
                    }
                }
                if (count % 1000 == 0) {
                    System.out.println(new StringBuffer(String.valueOf(count)).append(" rows get complete"));
                    count = 0;
                }
            }
            System.out.println(new StringBuffer(String.valueOf(count)).append(" rows get complete"));
            System.out.println(new StringBuffer(String.valueOf(countALL)).append(" rows get All"));
            //关闭
            fw.close();
            DbTool.disConnect(con);
        } catch (SQLException e) {
            System.out.println("get old data error!".concat(e.toString()));
        } catch (IOException e) {
            System.out.println("get old data error!".concat(e.toString()));
        } catch (Exception e) {
            System.out.println("get old data error!".concat(e.toString()));
        }
    }

    public static void runSet(String table) {
        ResourceBundle dccRb = ResourceBundle.getBundle("DCC");
        String delimiter = dccRb.getString("DELIMETER");
        String tempPath = dccRb.getString("TEMP_DIR");
        try {
            Connection con;
            if(table.equals("TMP_GRPETPSCH")){
                con=DbTool.getConnectionLink1();
            }else{
                con = DbTool.getConnectionLink2();
            }
            //类型
            Hashtable types = new Hashtable();
            StringBuffer sqlMeta = new StringBuffer("select * from ").append(reMoveSeq(table));

            Statement state = con.createStatement();
            ResultSet rs = state.executeQuery(sqlMeta.toString());
            ResultSetMetaData md = rs.getMetaData();
            for (int i = 1; i <= md.getColumnCount(); i++) {
                types.put(md.getColumnName(i), md.getColumnTypeName(i));
            }
            //关闭
            rs.close();
            state.close();
            File file = new File(new StringBuffer(tempPath).append("/").append(table).append(".").append("dat").toString());
            FileReader fr = new FileReader(file);
            java.io.BufferedReader br = new BufferedReader(fr);
            String line1 = br.readLine();
            //字段
            StringBuffer sbField = new StringBuffer();
            Vector fields = new Vector();
            for (StringTokenizer st = new StringTokenizer(line1, delimiter); st.hasMoreTokens();) {
                String fieldID = (String) st.nextToken();
                sbField.append(fieldID);
                fields.addElement(fieldID);
                if (st.hasMoreTokens()) {
                    sbField.append(",");
                }
            }
            String line = br.readLine();
            int count = 0;
            int countAll = 0;
            while (line != null) {
                count++;
                countAll++;
//               if(countAll==6172)

                StringBuffer sb = new StringBuffer();
                StringBuffer sbValues = new StringBuffer();
                //对字段循环
                sb.append("insert into ");
                sb.append(reMoveSeq(table));
                sb.append("(");
                sb.append(sbField.toString());
                Iterator it = fields.iterator();
                int iPos=0;
				int tmpint=0;
                while(true){
                    iPos = line.indexOf(delimiter);
                    String value = "";
                    if(iPos > -1){
                           value = line.substring(0,iPos);
                           line = line.substring(iPos+3);
                    }else{
                           value = line;
                    }
                    String type = (String) types.get(it.next());
                    if (type.equalsIgnoreCase("VARCHAR") || type.equalsIgnoreCase("CHAR") || type.equalsIgnoreCase("TEXT")) {
                        sbValues.append("'");
                        if (!value.equals("NULL")) {
                            sbValues.append(value);
                        }
                        sbValues.append("'");
                    } else {
                        sbValues.append(value);
                    }
                    if (iPos >-1) {
                        sbValues.append(",");
                    }
                    if(!(iPos > -1)){
                        break;
                    }
                }
/*                for (StringTokenizer st = new StringTokenizer(line, delimiter); st.hasMoreTokens();) {
                    String value = st.nextToken();
                    String type = (String) types.get(it.next());
                    if (type.equalsIgnoreCase("VARCHAR") || type.equalsIgnoreCase("CHAR") || type.equalsIgnoreCase("TEXT")) {
                        sbValues.append("'");
                        if (!value.equals("NULL")) {
                            sbValues.append(value);
                        }
                        sbValues.append("'");
                    } else {
                        sbValues.append(value);
                    }
                    if (st.hasMoreTokens()) {
                        sbValues.append(",");
                    }
                }*/
                sb.append(") values(");
                sb.append(sbValues.toString());
                sb.append(")");
                java.sql.Statement st = con.createStatement();
                //执行
                //System.out.println(sb.toString());
                st.execute(sb.toString());
                st.close();
                line = br.readLine();
                if (count % 1000 == 0) {
                    System.out.println(new StringBuffer(String.valueOf(count)).append(" rows set complete"));
                    //count = 0;
                }
            }
            System.out.println(new StringBuffer(String.valueOf(count)).append(" rows set complete"));
            System.out.println(new StringBuffer(String.valueOf(countAll)).append(" rows set All"));
            //关闭
            con.commit();
            DbTool.disConnect(con);
        } catch (SQLException e) {
            System.out.println("set old data error!".concat(e.toString()));
        } catch (FileNotFoundException e) {
            System.out.println("set old data error! table ".concat(e.toString()).concat(" not has the temp file"));
        } catch (Exception e) {
            System.out.println("set old data error! table ".concat(e.toString()));
        }
    }

    /**
     * 去除小序号 ,只在查表,插表使用
     *
     * @param table
     * @return
     */
    private static String reMoveSeq(String table) {
        int index = table.indexOf("(");
        if (index > 0) {
            return table.substring(0, index);
        } else {
            return table;
        }
    }

    public static void runGetSet(String table, Object obj) {

        runGet(table, obj);
        runSet(table);
    }

    /**
     * 检查是否函数
     *
     * @param value
     * @return
     */
    public static boolean checkFunction(String value) {
        if (value.indexOf("(") > 0 && value.indexOf(")") > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 检查是否常量
     *
     * @param value
     * @return
     */
    public static boolean checkConstant(String value) {
        if (value.startsWith("'") && value.endsWith("'")) {
            return true;
        } else {
            return false;
        }
    }

    public static String runFunction(String value, ResultSet rs, Object obj) throws Exception {
        String fun = value.substring(0, value.indexOf("("));
        StringTokenizer param = new StringTokenizer(value.substring(value.indexOf("(") + 1, value.indexOf(")")), ",");
        //ResultSetMetaData rsMD=rs.getMetaData();
        Class[] types = new Class[param.countTokens()];
        Object[] objects = new Object[param.countTokens()];

        int index = 0;
        for (; param.hasMoreTokens();) {
            String key = param.nextToken();
            //是栏位
            if (!key.startsWith("'")) {
               if (rs.getObject(key) == null) {
                    return "NULL";
                  }
                types[index] = rs.getObject(key).getClass();
                objects[index] = rs.getObject(key);
            } else {
                types[index] = String.class;
                objects[index] = key.replace('\'', ' ').trim();
            }
            index++;
        }
        try {
            try {
                Method method = obj.getClass().getMethod(fun, types);
                return method.invoke(obj, objects).toString();
            } catch (NoSuchMethodException e) {
                Object publicFun = Class.forName("com.pansky.dcc.trans.PublicFunction").newInstance();
                Method method = publicFun.getClass().getMethod(fun, types);
                return method.invoke(publicFun, objects).toString();
            }
        } catch (NoSuchMethodException e) {
            System.out.println("get old data error! trans function ".concat(fun).concat(" not exist"));
            throw e;
        } catch (Exception e) {
            System.out.println("get old data error! do function ".concat(fun).concat(" error"));
            throw e;
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -