春季启动应用程序显示???字符而不是unicode [英] spring boot application showing ??? characters instead of unicode

查看:233
本文介绍了春季启动应用程序显示???字符而不是unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从数据库中读取我的dao类中的日志时,显示的是垃圾字符而不是unicode字符. Sql开发人员显示来自oracle数据库的正确值,并且在数据库上设置了正确的NLS语言编码.

While reading from database the logs in my dao class shows junk characters in place of unicode characters. Sql developer shows correct values from oracle database also correct NLS language encoding is set on the database.

以下代码适用于标准jdbc:

Below code works for standard jdbc:

connection = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe", "adminuser", "oracle");
Statement st=connection.createStatement();
ResultSet res=st.executeQuery("SELECT menu_item_name from pending_menu_item 
where menu_item_id=6062");

    while(res.next()){
        System.out.println("itemName: "+res.getString(1));
    }

下面是springboot项目的网址,其中显示了垃圾字符,我 https://github.com/AyubOpen/spring-boot-jdbc/

Below is the url for springboot project which shows junk characters, I uploaded to git hub.https://github.com/AyubOpen/spring-boot-jdbc/

package com.mkyong;

import com.mkyong.dao.CustomerRepository;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
import java.util.List;
import static java.lang.System.exit;

@SpringBootApplication
public class SpringBootConsoleApplication implements CommandLineRunner {

@Autowired
DataSource dataSource;

@Autowired
private CustomerRepository customerRepository;

@Autowired
private JdbcTemplate jdbcTemplate;

  public static void main(String[] args) throws Exception {
      SpringApplication.run(SpringBootConsoleApplication.class, args);
}

@Override
public void run(String... args) throws Exception {

// If you want to check the HikariDataSource settings
 HikariDataSource newds = (HikariDataSource)dataSource;
  System.out.println("getMaximumPoolSize = " + ((HikariDataSource) 
  dataSource).getMaximumPoolSize());
  System.out.println("DATASOURCE = " + 
  newds.getDataSourceProperties().getProperty("hikari.useUnicode"));

  if (args.length <= 0) {
    System.err.println("[Usage] java xxx.jar {display}");
   } else {
        if (args[0].equalsIgnoreCase("display")) {
        System.out.println("Display items...");
        List<String> list = customerRepository.findAll();
        list.forEach(x -> System.out.println(x));
      }
      System.out.println("Done!");
   }
  exit(0);
 }
}

package com.mkyong.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public class CustomerRepository {

   @Autowired
   private JdbcTemplate jdbcTemplate;

   public List<String> findAll() {

    List<String> result = jdbcTemplate.query(
        "SELECT menu_item_name from pending_menu_item where 
            menu_item_id=6062",
        (rs, rowNum) -> rs.getString("menu_item_name")
    );
  return result;
  }
}

application.properties  
----------------------
spring.main.banner-mode=off
spring.datasource.initialize=true
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.url=jdbc:oracle:thin:@localhost:1521/xe
spring.datasource.username=jahezdbapp
spring.datasource.password=oracle
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver 
spring.datasource.hikari.useUnicode=true
spring.datasource.hikari.characterEncoding=utf-8 
spring.datasource.hikari.characterSetResults=utf8 

# HikariCP settings
#60 sec
spring.datasource.hikari.connection-timeout=60000
# max 5
spring.datasource.hikari.maximum-pool-size=5

推荐答案

  1. spring.datasource.url的末尾添加?useUnicode=yes&characterEncoding=UTF-8.

application.properties

1本身应解决此问题,而2则不必要.

1 itself should solve the issue, 2 may not be necessary.

这篇关于春季启动应用程序显示???字符而不是unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆