mysql field to take the solution can not be resolved alias jdbc

With the project Spring JDBC, it is to call a specific database via JDBC interfaces ResultSetMetaData implementation class to retrieve the database returns a result set.

In project development, found that the use of aliases in MySQL in no way been resolved properly, meaning that, in addition to the field is given an alias is not in force, take the field is the original field name. For example, user_name specifically to take the individual named user_name_old, the final result is not returned alias user_name_old, but formerly known as user_name.

Tracking code found ResultSetMetaData.getColumnName (int column) the presence of some specific process logic implemented in a JDBC in MySQL.

public String getColumnName(int column) throws SQLException {
  if (this.useOldAliasBehavior) { // false
    return this.getField(column).getName();
  } else {
    String name = this.getField(column).getNameNoAliases(); // 取非别名
    return name != null && name.length() == 0 ? this.getField(column).getName() : name;
  }
}

Because this.useOldAliasBehavior property is false, so the final taking else logic is also left Field.getNameNoAliases () method.

public String getNameNoAliases() throws SQLException {
  if (this.useOldNameMetadata) { // false
    return this.getName();
  } else {
    return this.connection != null && this.connection.versionMeetsMinimum(4, 1, 0) ? this.getOriginalName() : this.getName();
  }
}

So we finally locate the problem to the this.useOldAliasBehavior property, which can be configured through JDBC drivers address.

useOldAliasMetadataBehavior=true

Complete drive address should look like this:

jdbc:mysql://localhost/testDB?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useOldAliasMetadataBehavior=true

Add on the property, you will find that the problem has been resolved.

 

"I can not sad to sit by you."

Guess you like

Origin www.cnblogs.com/yanggb/p/11427715.html