【发布时间】:2017-03-25 08:17:53
【问题描述】:
显示创建表人员;
CREATE TABLE `persons` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name_first` char(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '{search_person_txtP_mv}',
`name_middle` char(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '{search_person_txtP_mv}',
`name_last` char(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '{search_person_txtP_mv}',
`name_unstructured` char(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'the entire string from {search_person_txtP_mv}, but only if the strong does not follow the "normal" logic',
`added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name_first`,`name_middle`,`name_last`,`name_unstructured`)
) ENGINE=InnoDB AUTO_INCREMENT=3795096 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Persons are typically authors of documents but also users or editors are persons. '
我看到 name_first、name_last 是用来处理 utf8_unicode_ci 的。然而,当我尝试导入数据时,我遇到了 e.printStackTrace() 指向 name_last 和 name_first 的 java.lang.NullPointerException 错误。
奇怪的是,没有插入服务器数据库的记录被插入本地数据库而没有任何错误。
姓名插入代码:
public Long addPersonToDbIfNotExists(JSONDocument document, Person author) throws SQLException {
PreparedStatement stateAuthorExists = null;
PreparedStatement stateInsertAuthor = null;
ResultSet rs = null;
Long authorKey = null;
int count = 1;
String queryAuthorExists = "SELECT " + constants.getPersonID() + " FROM " + constants.getPersons() + " WHERE "
+ constants.getFirstname() + (firstname == null ? " is null" : " = ? ") + " AND "
+ constants.getMiddlename() + (middlename == null ? " is null" : " = ? ") + " AND "
+ constants.getSurname() + (surname == null ? " is null" : " = ? ") + " AND "
+ constants.getUnstructured() + (unstructured == null ? " is null" : " = ? ");
try {
stateAuthorExists = conn.prepareStatement(queryAuthorExists, Statement.RETURN_GENERATED_KEYS);
// if not null insert true value for the corresponding ? (which is
// represented by count)
if (firstname != null)
stateAuthorExists.setString(count++, firstname);
if (middlename != null)
stateAuthorExists.setString(count++, middlename);
if (author.getFirstname() != null)
stateAuthorExists.setString(count++, surname);
if (unstructured != null)
stateAuthorExists.setString(count++, unstructured);
rs = stateAuthorExists.executeQuery();
// if it is a new author not already present in the database
if (!rs.next()) {
ResultSet rs2 = null;
String queryAuthor = "INSERT INTO " + constants.getPersons() + " (" + constants.getFirstname() + ", "
+ constants.getMiddlename() + ", " + constants.getSurname() + ", " + constants.getUnstructured()
+ ")" + "VALUES (?, ?, ?, ?)";
try {
stateInsertAuthor = conn.prepareStatement(queryAuthor, Statement.RETURN_GENERATED_KEYS);
// handle null values and other preprocessing stuff
SetIfNull(document, stateInsertAuthor, author.getFirstname(), 1, "string",
constants.getFirstname());
SetIfNull(document, stateInsertAuthor, author.getMiddlename(), 2, "string",
constants.getMiddlename());
SetIfNull(document, stateInsertAuthor, author.getSurname(), 3, "string", constants.getSurname());
SetIfNull(document, stateInsertAuthor, author.getUnstructured(), 4, "string",
constants.getUnstructured());
stateInsertAuthor.executeUpdate();
// get the autogenerated key back
rs2 = stateInsertAuthor.getGeneratedKeys();
if (rs2.next())
authorKey = rs2.getLong(1);
} catch (SQLException e) {
System.out.println(document.getDocumentPath() + ": " + document.getIdentifier() + "SetIfNulladdPersonToDB");
e.printStackTrace();
throw e;
} finally {
rs.close();
rs2.close();
stateInsertAuthor.close();
}
} else
authorKey = (long) rs.getInt(constants.getPersonID());
} catch (SQLException sqle) {
System.out.println(document.getDocumentPath() + ": " + document.getIdentifier() + "addPersonToDB1");
throw sqle;
} catch (Exception e) {
System.out.println(document.getDocumentPath() + ": " + document.getIdentifier() + "addPersonToDB2");
e.printStackTrace();
throw e;
} finally {
try {
stateAuthorExists.close();
if (rs != null)
rs.close();
} catch (SQLException e) {
throw e;
}
}
return authorKey;
}
【问题讨论】:
-
请出示您正在使用的代码。
-
这是字符集问题,不是排序规则问题。哪行代码抛出 NPE?您向我们展示了您的验证代码,但没有向我们展示执行插入的代码。看看你的 JDBC 连接字符串的
characterSetEncoding参数;有时默认设置因服务器而异。阅读此dev.mysql.com/doc/connector-j/5.1/en/… -
@O.Jones 添加了插入代码
-
@O.Jones 您提供的链接是否也适用于 tomcat 服务器?
-
是的,tomcat 和其他 servlet 容器使用 JDBC 连接字符串。