Java 从ResultSet读取空值
下表可能包含STA、ETA或ATA的空值:Java 从ResultSet读取空值,java,sql,datetime,null,resultset,Java,Sql,Datetime,Null,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
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;
查询是:
从中选择flightNum_-arr、STA、ETA、ATA、airlineICAO、aircraftType、pk_-arr
飞行时间表”
在Java代码中尝试从该表读取数据时,会出现以下错误:
java.sql.SQLException:无法从转换值“0000-00-00:00:00”
将第2列添加到时间戳
它出现在第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();
}
}
publicsvoidsetquery(字符串查询){
cache=新向量();
试一试{
//执行查询并存储结果集及其元数据
Connection con=getConnection();
语句Statement=con.createStatement();
ResultSet rs=语句.executeQuery(查询);
ResultSetMetaData meta=rs.getMetaData();
colCount=meta.getColumnCount();
//用新列名重新生成标题数组
headers=新字符串[colCount];
对于(int h=1;h我认为问题在于,您需要配置连接,以便根据以下帖子将零日期时间转换为null:
我认为问题在于,您需要配置连接,以便根据以下帖子将零日期时间转换为空:
您应该使用rs.getTimeStamp()或rs.getDate()代替DateTimeColum的rs.getString您应该使用rs.getTimeStamp()或rs.getDate()请提供整个堆栈跟踪,而不是DateTimeColum的rs.getString。在上面的哪一行代码处发生异常?您的查询是否包含使用时间戳列之一的WHERE
子句?@John B:请参阅更新的帖子。请提供整个堆栈跟踪。在上面的doe中的哪一行代码处s发生异常?您的查询是否包含使用时间戳列之一的WHERE
子句?@John B:请参阅更新的帖子。谢谢。我介绍了这个jdbc:mysql://yourserver:3306/yourdatabase?zeroDateTimeBehavior=convertToNull. 现在它就像一个符咒!!!谢谢。我介绍了这个jdbc:mysql://yourserver:3306/yourdatabase?zeroDateTimeBehavior=convertToNull。现在它就像一个符咒一样工作!!!