情况描述:在Navicat中新建了一个数据库,然后导入了几个CSV文件,建立了几个表:
在这里插入图片描述
其中sno_info结构如下所示:
在这里插入图片描述
当我简单查询Sno的时候:
在这里插入图片描述
报错了:

1054 - Unknown column 'sno' in 'field list'

意思是说Sno不存在,但明明是存在的,而且当输入Sno的时候,Navicat也能够联想出来Sno,于是我试了下联想的:
在这里插入图片描述
竟然神奇的成功了!!!
于是联想到检查一下字符串的编码格式:

public static String getEncoding(String str) {
        String encode = "GB2312";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s = encode;
                return s;
            }
        } catch (Exception exception) {
        }
        encode = "ISO-8859-1";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s1 = encode;
                return s1;
            }
        } catch (Exception exception1) {
        }
        encode = "UTF-8";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s2 = encode;
                return s2;
            }
        } catch (Exception exception2) {
        }
        encode = "GBK";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s3 = encode;
                return s3;
            }
        } catch (Exception exception3) {
        }
        return "";
    }

运行:

getEncoding("Sno")
getEncoding("Sno")

其中第一个"Sno"是我把联想出来的复制上去的,第二个"Sno"是自己手敲的。 结果是:
第一个为UTF-8,第二个为GB2312!!!!问题就在这里!!!

解决办法:
在这里插入图片描述
把字段Sno删除然后手敲Sno上去,这样编码方式就统一了。

Logo

更多推荐