连接到db:
public DBSource(ConnectionInfo ci) throws
ClassNotFoundException, InstantiationException,
IllegalAccessException, SQLException
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String dbPath = String.format(
"jdbc:mysql://%s:%d/%s?user=%s&password=%s&characterEncoding=utf-8&" +
"useUnicode=true", ci.host, ci.port, ci.dbName, ci.user, ci.password);
conn = java.sql.DriverManager.getConnection(dbPath);
prepareTables();
}
Run Code Online (Sandbox Code Playgroud)
表创建代码:
private void prepareTables() throws SQLException
{
java.sql.Statement stat = conn.createStatement();
String query = "set names utf8";
stat.execute(query);
query = "set character set utf8";
stat.execute(query);
query = "show variables like '%char%'";
stat.execute(query);
java.sql.ResultSet rs = stat.getResultSet();
while (rs.next())
{
String k = rs.getString(1);
String v = rs.getString(2);
System.out.println(k + " - " …Run Code Online (Sandbox Code Playgroud) 我有下表:
create table test
(
fname char(20) character set utf8 collate utf8_turkish_ci,
id int primary key
);
Run Code Online (Sandbox Code Playgroud)
我按如下方式插入数据:
resultSet.executeQuery("set namee utf8");
preparedStatement = connection.prepareStatement("insert into test(fname) values(?)");
preparedStatement.setstring(1,"alis");
preparedStatement.executeUpdate();
Run Code Online (Sandbox Code Playgroud)
但是当我检索数据时,它们就像是?????.
什么是问题,我该如何解决?