我正在写一个应用程序 consumes json
由另一个Web应用程序提供。使用 org.apache.http.client.HttpClient
检索数据,然后将相关数据存储到mysql表中。
像这样:
URI uri = builder.build();
HttpGet httpget = new HttpGet(uri);
HttpResponse httpResponse = client.execute(httpget);
rd = new BufferedReader(new InputStreamReader(httpResponse
.getEntity().getContent(), "UTF-8"));
String line = "";
while ((line = rd.readLine()) != null) {
//process it
}
响应标头:
HTTP/1.1 200 OK [Server: Apache-Coyote/1.1, Cache-Control: no-cache,no-store,private,must-revalidate,max-stale=0,post-check=0,pre-check=0,
Expires: Thu, 01 Jan 1970 00:00:00 GMT,
Pragma: no cache,
Set-Cookie: JSESSIONID=26E4AC5C0EF37BA53B736DF9B0F903FD;
Path=/,
Content-Type: application/x-javascript;
charset=UTF-8,
Transfer-Encoding: chunked,
Date: Thu, 06 Jun 2013 12:10:06 GMT,
Connection: close]
代码可以很好地处理响应中的正常数据,并填充相应的列。但在某些情况下,响应包含无法插入到数据库表中的垃圾字符。垃圾数据的一个例子:
P . 1989-1990 ݇�� ���� �� ����J� ᣇ¹I� ������K� �PM�H�� ��옉� �I� ���� ܬ� �E� �������� �� ���J� ��I� �E� �� ��K� ܬ�芪�� ��A ��A�. �E����� ��� ��F�� ��M� ����� �������� �I� �E� �� ��K�, ������ ����O� ��� ...
插入失败,出现异常:
java.sql.SQLException: Incorrect string value: '\xF1\xA3\x87\xB9I\xEF...' for column 'dataSet' at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2019)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:233)
at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:818)
at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:1)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:587)
... 5 more
关于如何将这些数据输入数据库表有什么建议吗?请给我建议。
一些平台信息:
OS: Ubuntu 12.04 LTS
MYSQL Version: 5.5.32
//DB Details
character_set_client:latin1
character_set_connection:latin1
character_set_database:latin1
character_set_filesystem:binary
character_set_results:latin1
character_set_server:latin1
character_set_system:utf8
编辑
我想强调的是,如果将相同的代码和数据库服务器移到windows环境中,它们就可以正常工作。没有错误。但是,它们在*nix环境中失败:(
1条答案
按热度按时间c2e8gylq1#
数据库将您的“垃圾数据”解释为文本,而不是任意二进制数据。您可以通过为
dataSet
列,例如VARBINARY
而不是VARCHAR
,或BLOB
而不是TEXT
.另请参见http://dev.mysql.com/doc/refman/5.5/en/binary-varbinary.html