我有
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:cities.db");
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select * from cities;");+
while (rs.next()) {
byte[] bytes = rs.getBytes("country");
String country = new String(bytes, "utf-8");
System.out.println(country);
}
rs.close();
conn.close();
我的输出显示 � 表示 ä、ö、ü 等字符。
如果我从命令行运行 sqlite3
并从那里查询表,字符没问题...
“编译指示编码;”显示 utf-8...
如果我调试,字节数组在“ü”的位置有“-127”...
如何解决? 是什么导致了这个问题?
链接到示例 SQLite 文件(只有一行):
最佳答案
你能试试吗
Class.forName(org.sqlite.JDBC.class.getCanonicalName());
String url = "jdbc:sqlite:";
SQLiteConfig config = new SQLiteConfig();
config.setEncoding(SQLiteConfig.Encoding.UTF8);
Connection conn = DriverManager.getConnection(url, config.toProperties());
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select * from cities;");+
while (rs.next()) {
String country = rs.getString("country");
System.out.println(country);
}
关于java - SQLite 数据库中的奇怪编码 : How to retrieve string values with JDBC?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34674367/