【问题标题】:How to save Japanese characters into Mysql by java spring and hibernate?java spring和hibernate如何将日文字符保存到Mysql中?
【发布时间】:2019-08-13 04:43:25
【问题描述】:

我有几个文件原始数据保存为 .DAT,这个文件有日文字符。 我想通过 java spring boot 将日文字符保存到数据库中。 我正在使用:

  • Ubuntu 18.04
  • 智能
  • 休眠
  • 弹簧靴
  • 分级

设置数据库:

url: jdbc:mariadb://localhost:3306/msf?useUnicode=yes&characterEncoding=UTF-8

读取文件并保存到数据库中:

StringBuilder sb = new StringBuilder();
    File file = ResourceUtils.getFile("classpath:sources.data/DATADEMO");

    if (file.exists()){
        try (BufferedReader br = new BufferedReader(new FileReader(file.getPath()))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] str = line.split(",");
                MsPaymentSake msPaymentSake = new MsPaymentSake();
                msPaymentSake.setStoreCode(str[0]);
                msPaymentSake.setType(str[1]);
                msPaymentSake.setClassification(str[2]);
                msPaymentSake.setCapacity(str[3]);
                msPaymentSake.setStatsDate(str[4]);
                msPaymentSake.setPartner(str[5].trim());
                msPaymentSake.setProductName(str[6].trim());
                msPaymentSake.setFrequencyAll(str[7]);
                msPaymentSake.setQuantity(str[8]);
                msPaymentSake.setPrice(str[9]);
                msPaymentSake.setTotalPrice(str[10]);
                msPaymentSake.setFrequency(str[11]);
                msPaymentSake.setCreated(LocalDateTime.now());
                paymentSakeRepository.save(msPaymentSake);
            }
        } catch (IOException e) {
            System.err.format("IOException: %s%n", e);
        }
        file.delete();
    } else{
        System.out.println("can't find file!");
    }

【问题讨论】:

  • 你的问题是什么?您已经可以通过将编码设置为 utf8 来做到这一点。
  • 是的,我知道,但是当我将文件“DATADEMO”设置为utf8时,结果仍然没有改变!
  • 不会改变是什么意思?是否有错误,或者实际结果究竟是什么?
  • 这是结果:“����������(�"
  • 您的数据库、表和列的字符集是 utf8 吗? MySQL 默认是 latin1。

标签: java mysql spring-boot


【解决方案1】:

在你的 application.properties 中使用这些:

spring.datasource.hikari.data-source-properties.useUnicode=true
spring.datasource.hikari.data-source-properties.characterEncoding=UTF-8

【讨论】:

  • 很伤心,我添加了它,所以它没有产生我想要的结果
  • 当您打印 msPaymentSake 对象时,它是否显示正确的 unicode 字符串?
  • 是的,我找到了问题的解决方案BufferedReader bufRdr = new BufferedReader(new InputStreamReader(new FileInputStream(file), "Shift_JIS"))
猜你喜欢
  • 2019-05-16
  • 2017-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多