Groovy unicode支持MySQL select

时间:2011-08-18 21:07:11

标签: mysql unicode groovy

我正在尝试修复UTF-8编码数据库中的问题。在迁移到当前数据库系统期间,看起来数据被压缩为Latin-1编码。我写了一个快速的Groovy脚本来查找和报告编码错误。但是,它无法匹配任何东西。

如果我用Java编写类似代码,报告工作正常!

所以,我认为作为Groovy的新秀,我做了一些愚蠢的事情。谁能看到我的方法中的错误?我已尝试在select语句中对字符进行硬编码,但我无法使任何特殊字符起作用。

当然先谢谢。

AH

Groovy代码

import groovy.sql.Sql

        def dias = ["è": "é", "…": "…"]

        def atk = Sql.newInstance(
            'jdbc:mysql://myURL:3306/myDB?useUnicode=true&characterEncoding=utf8',
            'user',
            'pass',
            'com.mysql.jdbc.Driver'
        )

        dias.each() {x ->
            println x.getKey()
            atk.eachRow("SELECT `noteContent` FROM `ArchDescriptionRepeatingData`"
            + "WHERE `noteContent` LIKE(\"%" + x.getKey() + "%\")"
            ) { y ->
                println y
            }
        }

这里要求的是java代码,我在其中添加了一些额外的功能:

import java.sql.*;
import java.util.*;
import java.util.regex.*;

    public class Diacritic {
        private Connection connection;
        private HashMap<String, String> dia;

        Diacritic() throws SQLException, ClassNotFoundException {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(
                "jdbc:mysql://myURL:3306/myDB"
                + "?user=user&password=pass");
            dia = new HashMap<String, String>();

            populateArray();

            System.out.println("Component Titles");

            for(Map.Entry e : dia.entrySet()) {
                String st = e.getKey().toString();
                Pattern p = Pattern.compile(st);
                System.out.println();
                System.out.println(st + "\t\t->\t" + e.getValue().toString());
                System.out.println();
                Statement s = connection.createStatement();
                ResultSet r = s.executeQuery("SELECT `itemValue` FROM `ListOrderedItems`"
                    + "WHERE `itemValue` LIKE(\"%" + st + "%\")"
                    //+ "LIMIT 0, 100000"
                );

                int x = 0;

                while (r.next()) {
                    x++;
                    Matcher m = p.matcher(r.getString(1));
                    if(m.find()) {

                    int start = 0;
                    int mstart = m.start();
                    int mend = m.end();
                    int end = r.getString(1).length();
                    if (mstart - start <= 10 || end - mend <= 10) {
                        System.out.println(x + ": " + r.getString(1)
                            + "\t->\t"
                            + "\t"  + r.getString(1).substring(0,mstart)
                            + e.getValue().toString()
                            + r.getString(1).substring(mend)
                            );

                    }
                    else {
                        System.out.println(x + ": " + r.getString(1).substring((mstart - 10), (mend + 10))
                            + "\t->\t"
                            + "\t"  + r.getString(1).substring((mstart - 10),mstart)
                            + e.getValue().toString()
                            + r.getString(1).substring(mend, (mend + 10))
                        );
                    }

                    }
                }

                s.close();
            }


        }

        public static void main (String[] args) throws SQLException, ClassNotFoundException {
            Diacritic d = new Diacritic();
        }

        public void populateArray() {
            dia.put("è", "é");
            dia.put("…", "…");
            dia.put("—", "");
            dia.put("‑”", "-");
            dia.put("é", "é");
            dia.put("æ", "æ");
            dia.put("ë", "ë");
            //etc...
        }   
    }

0 个答案:

没有答案