2017-07-09 100 views
0

從數據庫讀取數據庫時,我的dao類中的日誌顯示垃圾字符以代替unicode字符。 Sql開發人員顯示來自oracle數據庫的正確值,還在數據庫上設置了正確的NLS語言編碼。春季啓動應用程序顯示???字符而不是unicode

下面的代碼適用於標準的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項目的URL,顯示亂碼,我 上傳到git的樞紐。 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 
+0

可能重複的[HikariConfig春季啓動應用程序顯示???字符而不是unicode](https://stackoverflow.com/questions/44985168/hikariconfig-spring-boot-application-showing-characters-instead-of-unicode) –

+0

@AbhijitSarkar有沒有辦法刪除以前的問題?我正在尋找與HikariCP春季啓動與unicode字符工作 – Ayub

+0

https://meta.stackexchange.com/questions/96281/how-to-close-a-question-in-stackoverflow-com –

回答

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

  2. spring.datasource.sqlScriptEncoding=UTF-8application.properties

1本身應該解決這一問題,2可能沒有必要。

+0

試過(1)spring啓動失敗,初始化連接spring.datasource。 url = jdbc:oracle:thin:@localhost:1521/xe?useUnicode = yes & characterEncoding = UTF-8 – Ayub

+0

檢查URL ** **,這不是我說的 –

+0

我試過 &需要轉義爲我在.propeties文件中定義它們,並使用屬性佔位符來替換它們。 spring.datasource.url =的jdbc:神諭:薄:@localhost:1521/XE我們eUnicode = YES &茶racterEncoding = UTF-8和 spring.datasource.url =的jdbc:神諭:薄:@ localhost:1521/xe?us eUnicode = yes&cha racterEncoding = UTF-8 – Ayub

相關問題