从数据库读取时,我的dao类中的日志显示垃圾字符代替unicode字符。 Sql开发人员从oracle数据库显示正确的值也正确在数据库上设置了NLS语言编码。
以下代码适用于标准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,它显示了垃圾字符,I 上传到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
答案 0 :(得分:3)
在?useUnicode=yes&characterEncoding=UTF-8
末尾添加spring.datasource.url
。
在spring.datasource.sqlScriptEncoding=UTF-8
application.properties
醇>
1本身应解决问题,2可能没有必要。
答案 1 :(得分:2)
特别是对于Oracle。至少我处于同样的情况。
似乎,您的数据库使用了一些国家(非Unicode)编码。因此JDBC无法将其转换为UNICODE。
默认情况下,Oracle JDBC仅支持以下几个字符集:US7ASCII
,WE8DEC
,WE8ISO8859P1
,WE8MSWIN1252
和UTF8
。因此,以不同编码编码的所有字符串都将显示为问题。要增加对所有其他字符集的支持,请将文件orai18n.jar
添加到应用程序类路径中。
在此处查看更多信息:https://docs.oracle.com/database/121/JJDBC/global.htm#JJDBC28643
答案 2 :(得分:0)
这个问题似乎与jdbc数据源无关,我将为springboot创建一个简单的github项目,并在此问题上跟进以下问题:
spring boot commandline runner is using windows default character encoding
答案 3 :(得分:0)
记录: DB2,jdb的有效URL
jdbc:db2://x.x.x.x:5010 / xxxx:useUnicode = yes; characterEncoding = UTF-8;