Java JDBC 使用情境 1 -- (1). getColumnName(), getObject()
在新的專案中,我的Leader 較熟 {Adobe}/Flex 的開發,目前,我正學習中。不過,我的Java比較熟,所以他今天Pass給我一個問題,我很快就為他解決了:
一個功能中用來查詢的SQL發生了Exception:
Invalid parameter 0: Parameter index is out of range. ERRORCODE=-4461, SQLSTATE=42815
我發現是SUN Java JDBC API getColumnName(), getObject() index 的問題 --
紫色的部份是程式出錯的地方:
private List<Map<String, Object>> exeQuery(PreparedStatement psmt) throws Exception {
ResultSet rs = null;
List<Map<String,Object>> result = new ArrayList<Map<String, Object>>();
try{
rs = psmt.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
while(rs.next()){
Map<String, Object> map = new HashMap<String, Object>();
for(int i = 0; i < rsmd.getColumnCount(); i++){
map.put(rsmd.getColumnName(i), rs.getObject(i));
}
result.add(map);
}
} catch(Exception e){
throw new Exception(e);
} finally {
if(rs != null) rs.close();
}
return result;
}
SUN Java JDBC API :
private List<Map<String, Object>> exeQuery(PreparedStatement psmt) throws Exception {
ResultSet rs = null;
List<Map<String,Object>> result = new ArrayList<Map<String, Object>>();
try{
rs = psmt.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
while(rs.next()){
Map<String, Object> map = new HashMap<String, Object>();
for(int i = 0; i < rsmd.getColumnCount(); i++){
map.put(rsmd.getColumnName(i+1), rs.getObject(i+1));
}
result.add(map);
}
} catch(Exception e){
throw new Exception(e);
} finally {
if(rs != null) rs.close();
}
return result;
}
這很容易。但,似乎困擾他很久。
2018/06/15 今天問Leader,Leader(Rufus)說,這個問題已經解決了,以前客戶從未成功測通這個功能,現在,反而造成End User每 30 分鐘就收到派件通知,請客戶麻煩頻率調低一點 !!。
2018/06/15 今天看前一個專案的Line,該專案的派工功能還是常常出錯,由大陸的公司開發的 Platform,綁定JBPM,常常派錯工,一會是該公司派工Platform底層出錯、一會是JBPM的問題。還好我已經離開。我可以鑽研更多新的 Framework。^_*
在新的專案中,我的Leader 較熟 {Adobe}/Flex 的開發,目前,我正學習中。不過,我的Java比較熟,所以他今天Pass給我一個問題,我很快就為他解決了:
一個功能中用來查詢的SQL發生了Exception:
Invalid parameter 0: Parameter index is out of range. ERRORCODE=-4461, SQLSTATE=42815
我發現是SUN Java JDBC API getColumnName(), getObject() index 的問題 --
紫色的部份是程式出錯的地方:
private List<Map<String, Object>> exeQuery(PreparedStatement psmt) throws Exception {
ResultSet rs = null;
List<Map<String,Object>> result = new ArrayList<Map<String, Object>>();
try{
rs = psmt.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
while(rs.next()){
Map<String, Object> map = new HashMap<String, Object>();
for(int i = 0; i < rsmd.getColumnCount(); i++){
map.put(rsmd.getColumnName(i), rs.getObject(i));
}
result.add(map);
}
} catch(Exception e){
throw new Exception(e);
} finally {
if(rs != null) rs.close();
}
return result;
}
SUN Java JDBC API :
getColumnName
String getColumnName(int column) throws SQLException
Get the designated column's name.
- Parameters:
column- the first column is 1, the second is 2, ...- Returns:
- column name
- Throws:
SQLException- if a database access error occurs
getObject
Object getObject(int columnIndex) throws SQLException
Gets the value of the designated column in the current row of this
This method will return the value of the given column as a Java object. The type of the Java object will be the default Java object type corresponding to the column's SQL type, following the mapping for built-in types specified in the JDBC specification. If the value is an SQL
This method may also be used to read database-specific abstract data types. In the JDBC 2.0 API, the behavior of method
If
ResultSet object as an Object in the Java programming language.This method will return the value of the given column as a Java object. The type of the Java object will be the default Java object type corresponding to the column's SQL type, following the mapping for built-in types specified in the JDBC specification. If the value is an SQL
NULL, the driver returns a Java null.This method may also be used to read database-specific abstract data types. In the JDBC 2.0 API, the behavior of method
getObject is extended to materialize data of SQL user-defined types.If
Connection.getTypeMap does not throw a SQLFeatureNotSupportedException, then when a column contains a structured or distinct value, the behavior of this method is as if it were a call to: getObject(columnIndex, this.getStatement().getConnection().getTypeMap()). If Connection.getTypeMap does throw a SQLFeatureNotSupportedException, then structured values are not supported, and distinct values are mapped to the default Java class as determined by the underlying SQL type of the DISTINCT type.- Parameters:
columnIndex- the first column is 1, the second is 2, ...- Returns:
- a
java.lang.Objectholding the column value - Throws:
SQLException- if the columnIndex is not valid; if a database access error occurs or this method is called on a closed result set
private List<Map<String, Object>> exeQuery(PreparedStatement psmt) throws Exception {
ResultSet rs = null;
List<Map<String,Object>> result = new ArrayList<Map<String, Object>>();
try{
rs = psmt.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
while(rs.next()){
Map<String, Object> map = new HashMap<String, Object>();
for(int i = 0; i < rsmd.getColumnCount(); i++){
map.put(rsmd.getColumnName(i+1), rs.getObject(i+1));
}
result.add(map);
}
} catch(Exception e){
throw new Exception(e);
} finally {
if(rs != null) rs.close();
}
return result;
}
這很容易。但,似乎困擾他很久。
2018/06/15 今天問Leader,Leader(Rufus)說,這個問題已經解決了,以前客戶從未成功測通這個功能,現在,反而造成End User每 30 分鐘就收到派件通知,請客戶麻煩頻率調低一點 !!。
2018/06/15 今天看前一個專案的Line,該專案的派工功能還是常常出錯,由大陸的公司開發的 Platform,綁定JBPM,常常派錯工,一會是該公司派工Platform底層出錯、一會是JBPM的問題。還好我已經離開。我可以鑽研更多新的 Framework。^_*