繁体   English   中英

从ResultSet读取空值

[英]Reading null values from ResultSet

下表可能包含STA,ETA或ATA的空值:

CREATE TABLE `flightschedule` (
  `id` smallint(6) NOT NULL AUTO_INCREMENT,
  `flightNum_arr` varchar(40) DEFAULT NULL,
  `from_ICAO` varchar(20) DEFAULT NULL,
  `STA` datetime DEFAULT NULL,
  `ETA` datetime DEFAULT NULL,
  `ATA` datetime DEFAULT NULL,
  `pk_arr` varchar(10) DEFAULT NULL
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5611 DEFAULT CHARSET=latin1;

查询是:

SELECT flightNum_arr,STA,ETA,ATA,airlineICAO,aircraftType,pk_arr FROM flightchedule“;

当我尝试从Java代码中的该表读取数据时,发生以下错误:

java.sql.SQLException:无法将值'0000-00-00 00:00:00'从第2列转换为TIMESTAMP。

它发生在record[i] = rs.getString(i + 1);record[i] = rs.getString(i + 1);

我介绍了以下检查,但它似乎不起作用:

if(record[i] == null)
{
   record[i]= "";
}

编码:

public void setQuery(String query) {
        cache = new Vector();
        try {
          // Execute the query and store the result set and its metadata
          Connection con = getConnection();
          Statement statement = con.createStatement();
          ResultSet rs = statement.executeQuery(query);
          ResultSetMetaData meta = rs.getMetaData();
          colCount = meta.getColumnCount();
          // Rebuild the headers array with the new column names
          headers = new String[colCount];
          for (int h = 1; h <= colCount; h++) {
            headers[h - 1] = meta.getColumnName(h);
          }
          while (rs.next()) {
            String[] record = new String[colCount];
            for (int i = 0; i < colCount; i++) {
              record[i] = rs.getString(i + 1);
              if(record[i] == null)
              {
                  record[i]= "";
              }
            }
            cache.addElement(record);
          }
          fireTableChanged(null);         
          rs.close();
          if (con.getAutoCommit() != false) {
            con.close();
          }         
        } catch (Exception e) {
          cache = new Vector(); // blank it out and keep going.
          e.printStackTrace();
        }
      }

我相信问题是您需要配置连接,以便根据以下帖子将零日期时间转换为null:

在JDBC中处理DATETIME值0000-00-00 00:00:00

对于DateTimeColum,应该使用rs.getTimeStamp()或rs.getDate()而不是rs.getString。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM