2016-01-22 66 views
1

我想讀一些屬性通過以下命令MSSQL和JDBC讀DBCC USEROPTIONS

DBCC USEROPTIONS 

如何在JDBC使用下面的命令返回?

List<ResultVO> list = getJdbcTemplate().query(sql, new ParameterizedBeanPropertyRowMapper<ResultVO>() { 
     @Override 
     public ResultVO mapRow(ResultSet rs, int rowNumber) throws SQLException { 
      System.out.println(rowNumber); 
      ResultVO v = new ResultVO(); 
      v.setSetOption(rs.getString(0)); 
      v.setValue(rs.getString(1)); 
      //-- 
      return v; 
     } 
    }, (List<String>) null); 

它拋出異常如下:

Caused by: org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback; SQL [SELECT DBCC USEROPTIONS]; The index 1 is out of range.; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: The index 1 is out of range. 
    at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:106) 
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72) 

回答

3

錯誤消息的第一行表明你的代碼試圖執行SQL語句

SELECT DBCC USEROPTIONS 

但無效ŧ -SQL語法。在T-SQL語句

DBCC USEROPTIONS 

不會返回一個有效的ResultSet,通過下面的香草JDBC代碼所示:

try (Connection conn = DriverManager.getConnection(connectionUrl)) { 
    try (
      Statement s = conn.createStatement(); 
      ResultSet rs = s.executeQuery("DBCC USEROPTIONS")) { 
     ResultSetMetaData rsmd = rs.getMetaData(); 
     for (int i = 1; i <= rsmd.getColumnCount(); i++) { 
      System.out.printf("[%s]: %s%n", rsmd.getColumnName(i), rsmd.getColumnTypeName(i)); 
     } 
     System.out.println(); 
     while (rs.next()) { 
      System.out.printf("%s: %s%n", rs.getString(1), rs.getString(2)); 
     } 
    } 
} catch (Exception e) { 
    e.printStackTrace(System.err); 
} 

產生

[Set Option]: nvarchar 
[Value]: nvarchar 

textsize: -1 
language: us_english 
dateformat: mdy 
datefirst: 7 
lock_timeout: -1 
quoted_identifier: SET 
ansi_null_dflt_on: SET 
ansi_warnings: SET 
ansi_padding: SET 
ansi_nulls: SET 
concat_null_yields_null: SET 
isolation level: read committed 

看起來你需要調整你的Spring JDBC代碼只能執行DBCC USEROPTIONS