jdbc 通过 colName 读取结果集的别名问题
jdbc reading resultSet by colName issue for aliases
我有一个通用存储库,其方法为:
object Queries {
def getByFieldId(field: String, id: Int): String = {
s"""
|SELECT
| DF.id AS fileId,
| DF.name AS fileName,
| AG.id AS groupId,
| AG.name AS groupName
|FROM $tableName DFG
|INNER JOIN directory_files DF on DF.id = DFG.file_id
|INNER JOIN ad_groups AG on AG.id = DFG.group_id
|WHERE DFG.$field = $id
|""".stripMargin
}
}
def getByFieldId(field: String, id: Int): Try[List[Object]] = {
try {
val sqlQuery = Queries.getByFieldId("ad_group", 1)
statement = conn.getPreparedStatement(sqlQuery)
setParameters(statement, params)
resultSet = statement.executeQuery()
val metadata = resultSet.getMetaData
val columnCount = metadata.getColumnCount
val columns: ListBuffer[String] = ListBuffer.empty
for (i <- 1 to columnCount) {
columns += metadata.getColumnName(i)
}
var item: List[Object] = List.empty
while (resultSet.next()) {
val row = columns.toList.map(x => resultSet.getObject(x))
item = row
}
Success(item)
} catch {
case e: Any => Failure(errorHandler(e))
} finally conn.closeConnection(resultSet, statement)
}
问题是我的结果集忽略了查询别名和 return 列作为 (id, name, id, name)
而不是 (fileId, fileName, groupId, groupName)
。
找到的一个解决方案是使用列索引而不是列名称,但我不确定此解决方案是否会覆盖整个应用程序并且不会破坏其他一些查询。
也许,另一个找到的解决方案是 here 如果我是对的,我仍然可以使用 colNames 但需要将它们与 colTypes 放在一起,然后在 resultSet.next()
中调用 getType
方法为:
// this part of code is not tested
// this idea came to me writing this topic
while (resultSet.next()) {
val row = columns.toList.map(x => {
x.colType match {
case "string" => resultSet.getString(x.colName)
case "integer" => resultSet.getInt(x.colName)
case "decimal" => resultSet.getDecimal(x.colName)
case _ => resultSet.getString(x.colName)
})
item = row
}
您可以尝试使用 getColumnLabel
而不是 getColumnName
Gets the designated column's suggested title for use in printouts and displays. The suggested title is usually specified by the SQL AS clause. If a SQL AS is not specified, the value returned from getColumnLabel will be the same as the value returned by the getColumnName method.
请注意,这在很大程度上取决于所使用的 RDBM
。
对于 Oracle
这两种方法 return 别名 并且没有机会获得 原始列名 .
我有一个通用存储库,其方法为:
object Queries {
def getByFieldId(field: String, id: Int): String = {
s"""
|SELECT
| DF.id AS fileId,
| DF.name AS fileName,
| AG.id AS groupId,
| AG.name AS groupName
|FROM $tableName DFG
|INNER JOIN directory_files DF on DF.id = DFG.file_id
|INNER JOIN ad_groups AG on AG.id = DFG.group_id
|WHERE DFG.$field = $id
|""".stripMargin
}
}
def getByFieldId(field: String, id: Int): Try[List[Object]] = {
try {
val sqlQuery = Queries.getByFieldId("ad_group", 1)
statement = conn.getPreparedStatement(sqlQuery)
setParameters(statement, params)
resultSet = statement.executeQuery()
val metadata = resultSet.getMetaData
val columnCount = metadata.getColumnCount
val columns: ListBuffer[String] = ListBuffer.empty
for (i <- 1 to columnCount) {
columns += metadata.getColumnName(i)
}
var item: List[Object] = List.empty
while (resultSet.next()) {
val row = columns.toList.map(x => resultSet.getObject(x))
item = row
}
Success(item)
} catch {
case e: Any => Failure(errorHandler(e))
} finally conn.closeConnection(resultSet, statement)
}
问题是我的结果集忽略了查询别名和 return 列作为 (id, name, id, name)
而不是 (fileId, fileName, groupId, groupName)
。
找到的一个解决方案是使用列索引而不是列名称,但我不确定此解决方案是否会覆盖整个应用程序并且不会破坏其他一些查询。
也许,另一个找到的解决方案是 here 如果我是对的,我仍然可以使用 colNames 但需要将它们与 colTypes 放在一起,然后在 resultSet.next()
中调用 getType
方法为:
// this part of code is not tested
// this idea came to me writing this topic
while (resultSet.next()) {
val row = columns.toList.map(x => {
x.colType match {
case "string" => resultSet.getString(x.colName)
case "integer" => resultSet.getInt(x.colName)
case "decimal" => resultSet.getDecimal(x.colName)
case _ => resultSet.getString(x.colName)
})
item = row
}
您可以尝试使用 getColumnLabel
而不是 getColumnName
Gets the designated column's suggested title for use in printouts and displays. The suggested title is usually specified by the SQL AS clause. If a SQL AS is not specified, the value returned from getColumnLabel will be the same as the value returned by the getColumnName method.
请注意,这在很大程度上取决于所使用的 RDBM
。
对于 Oracle
这两种方法 return 别名 并且没有机会获得 原始列名 .