Access數據庫列名大小寫轉換(2012.11.29)

     這幾天做項目的時候,碰到了一個很蛋疼的問題,我們的客戶老外們,不會用mysql和oracle,就喜歡在office文檔裏面操作操作,看來他們認爲這個是非常爽的,任何的bug啊,數據庫啊什麼的都通過office的辦公文件給我們,每次過來的數據庫都需要我們轉換到自己的oracle裏面,更蛋疼的是,他們access數據庫裏面的table列的大小寫還個各不一致,奶奶的胸,看的十分的煩躁,就只能自己寫個東西轉換一下了,本里研究了一下用jackcess去處理這個東東,但是那個開源實在很難研究透,有些東西還不支持,我也寫了郵件和他們的developor做了一些交流。

     直接上代碼吧,菜鳥寫的代碼(大牛飄過,別笑 ,哈哈)。

  

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.lonnesol.dbmanager.uppAccessColumns;

import com.lonnesol.database.tools.SqlUtil;
import java.sql.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author hualun-sunny.jiang
 */
public class UppAccessColumns {

    public static void main(String[] args) {
        UppAccessColumns test = new UppAccessColumns();
        test.upperAccessColumns("D://daks122_20120928_carl_1015.mdb");
    }

    private void upperAccessColumns(String path) {

        Connection con = SqlUtil.getInstance("access", path, "", "").getConnection();
        try {
            Statement smt = con.createStatement();
            DatabaseMetaData dma = con.getMetaData();
            //將數據庫中的表的名稱轉儲出來
            String[] types = new String[1];
            types[0] = "TABLE"; //設置查詢類型
            //%匹配所有
            ResultSet rs = dma.getTables(null, null, "%", types);
            String regEx = "[a-z]";
            Pattern pat = Pattern.compile(regEx);
            Matcher mat = null;
            Set<String> tableNameList = new HashSet<String>();
            StringBuffer names = new StringBuffer();
            while (rs.next()) {
                String tableName = (String) rs.getObject("TABLE_NAME");
                LinkedHashMap<String, String> tableColumns = getTableColumns(con, tableName);
                Iterator<Map.Entry<String, String>> it = tableColumns.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry<String, String> next = it.next();
                    String columnName = next.getKey();
                    mat = pat.matcher(columnName);
                    boolean find = mat.find();
                    if (find) {
                        //保存有小寫列的表名
                        tableNameList.add(tableName);
                        if (!names.toString().contains(tableName)) {
                            names.append(tableName).append(" , ");
                        }
                    }
                }
            }
            Iterator<String> iterator = tableNameList.iterator();
            System.out.println("You access database have " + tableNameList.size() + " tables contain lower case in the column Name.");
            while (iterator.hasNext()) {
                String tableName = iterator.next();
                boolean bool = createTable(path, tableName);
                if (bool) {
                    System.out.println("Transform table " + tableName + " success !");
                } else {
                    System.out.println("Transform table " + tableName + " fail !");
                }
            }
            smt.close();
            con.close();

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    private boolean createTable(String path, String name) {
        boolean bo = false;
        Connection dstCon = SqlUtil.getInstance("access", path, "", "").getConnection();
        String sqlD = "drop table  if exists " + name;
        SqlUtil.executeUpdate(dstCon, sqlD);
        String tableName = ("`" + name + "`");
        String sql = "SELECT * FROM " + tableName;
        Statement stmt = null;
        try {
            String newTableName = "`" + name + "Temp`";
            String sqlTempCreate = "CREATE TABLE " + newTableName + "(";
            String sqlFinalCreate = "CREATE TABLE " + tableName + "(";
            String create = "";
            String columns = "";
            stmt = dstCon.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            rs = stmt.getResultSet();
            ResultSetMetaData rsmd = rs.getMetaData();
            int numcols = rsmd.getColumnCount();
            String colnames[] = new String[400];
            int coltypes[] = new int[400];
            int colsizes[] = new int[400];

            for (int i = 0; i < numcols; i++) {
                String colname = rsmd.getColumnName(i + 1);
                colname = colname.toUpperCase();
                int coltype = rsmd.getColumnType(i + 1);
                int colsize = rsmd.getColumnDisplaySize(i + 1);

                colnames[i] = colname;
                coltypes[i] = coltype;
                colsizes[i] = colsize;
                if (colname.equalsIgnoreCase("Key")) {
                    colname = "`KEY`";
                }
                if (i > 0) {
                    create += ", ";
                    columns += ",";
                }
                columns += colname;
                if (coltype == 4) {
                    create += colname + " int";
                } // 7 single and 8 double are both converted to real
                else if (coltype == 7 || coltype == 8) {
                    create += colname + " real";
                } else if (coltype == 12 && colsize < 255) {
                    create += colname + " varchar(" + colsize + ")";
                } else if (coltype == -1 || colsize > 255) {
                    create += colname + " text";
                } else {
                    create += colname + " varchar(255)";
                }
            }
            create += ");";

            //創建臨時的新表
            sqlTempCreate += create;
            SqlUtil.executeUpdate(dstCon, sqlTempCreate);
            dstCon.commit();
            dstCon.close();

            Connection con = SqlUtil.getInstance("access", path, "", "").getConnection();
            //將需要轉換的表數據存到臨時的新表中
            String copyTempDatasSql = "INSERT INTO " + newTableName + " SELECT * FROM " + tableName + ";";
            con.prepareStatement(copyTempDatasSql).execute();
            con.commit();
            con.close();

            Connection con1 = SqlUtil.getInstance("access", path, "", "").getConnection();
            //刪除原需要轉換的小寫表
            String dropOldSql = "DROP TABLE " + tableName + ";";
            con1.prepareStatement(dropOldSql).execute();
            con1.commit();
            con1.close();

            Connection con2 = SqlUtil.getInstance("access", path, "", "").getConnection();
            //創建目的大寫表
            sqlFinalCreate += create;
            SqlUtil.executeUpdate(con2, sqlFinalCreate);
            con2.commit();
            con2.close();

            Connection con3 = SqlUtil.getInstance("access", path, "", "").getConnection();
            //將臨時表數據存到大寫表
            String copyFinalDatasSql = "INSERT INTO " + tableName + " SELECT * FROM " + newTableName + ";";
            con3.prepareStatement(copyFinalDatasSql).execute();
            con3.commit();
            con3.close();

            Connection con4 = SqlUtil.getInstance("access", path, "", "").getConnection();
            //刪除臨時表
            String dropTempSql = "DROP TABLE " + newTableName + ";";
            con4.prepareStatement(dropTempSql).execute();
            con4.commit();
            con4.close();

            bo = true;
            return bo;
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println(ex.getMessage());
            return bo;
        }
    }

    private LinkedHashMap<String, String> getTableColumns(Connection connection, String tableName) {
        //用來保存每一列對應的類型
        LinkedHashMap<String, String> tableColumnsLinkedMap = new LinkedHashMap<String, String>();
        String sql = "";
        try {
            Statement statement = connection.createStatement();
            if (tableName.trim().contains(" ")) {
                tableName = "`" + tableName + "`";
            }
            sql = "SELECT top 1 * FROM " + tableName;
            ResultSet rsforTable = statement.executeQuery(sql);
            ResultSetMetaData rsTable = rsforTable.getMetaData();
            //表列數
            int numberOfColumn = rsTable.getColumnCount();
            for (int i = 1; i <= numberOfColumn; i++) {
                String columnName = rsTable.getColumnName(i);
                String columnType = rsTable.getColumnTypeName(i);
                int columnDisplaySize = rsTable.getColumnDisplaySize(i);
                String allType = "";
                if ("DOUBLE".equalsIgnoreCase(columnType) || "SMALLINT".equalsIgnoreCase(columnType) || "INTEGER".equalsIgnoreCase(columnType)) {
                    allType = columnType;
                } else {
                    allType = columnType + "(" + columnDisplaySize + ")";
                }

                tableColumnsLinkedMap.put(columnName, allType);
            }
            if ("M_FLD_DOC".equalsIgnoreCase(tableName) || "M_RVR_DOC".equalsIgnoreCase(tableName)) {
                if (rsforTable.next()) {
                    tableColumnsLinkedMap = null;
                }
            }
            rsforTable.close();
        } catch (SQLException ex) {
            System.out.println("ERROR SQL :" + sql);
            ex.printStackTrace();
        }
        return tableColumnsLinkedMap;
    }
}

 

發佈了54 篇原創文章 · 獲贊 61 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章