部分原码:
String strSQL1="select * from WLAN_ALARM_FUFU";
Statement stmt1=conn.createStatement();
ResultSet pwrResultSet=stmt1.executeQuery(strSQL1);
while (pwrResultSet.next())
{
ID=pwrResultSet.getString("ID");
IP=pwrResultSet.getString("IP");
Alarm_Time = pwrResultSet.getString("时间");
Alarm_Info = pwrResultSet.getString("Info");
Alarm_Type = pwrResultSet.getString("告警");
int alarmTimeInt = Alarm_Time.lastIndexOf(".");
Alarm_Time = Alarm_Time.substring(0, alarmTimeInt);
Alarm_Time = Alarm_Time.replace(':', '-');
if(Alarm_Info.trim().equals(""))
{
NeName="";
Alarm_Case="";
}
else
{
String[] infos = Alarm_Info.split("[ ]+");
String withoutTime = infos[2].trim();
NeName = withoutTime.substring(0,withoutTime.lastIndexOf("-"));
Alarm_Case=withoutTime.substring(withoutTime.lastIndexOf("-")+1,withoutTime.length());
}
expValue ="ALARMBEGIN : Alarm_State=";
expValue += Alarm_Type+" : ID=";
expValue += ID+" : Alarm_Time=";
expValue += Alarm_Time+" : IP=";
expValue += IP+" : NeName=";
expValue += NeName+" : Alarm_Case=";
expValue += Alarm_Case+" : ALARMEND";
expValue += "\r\n";
SendAlarm.setAlarmPool(expValue);
logger.info(new String(expValue.getBytes("GBK")));
String strSQL2="delete WLAN_ALARM_FUFU where ID="+ID;
stmt1.executeUpdate(strSQL2);
}
}
catch(Exception ex)
{
logger.error(ex.getLocalizedMessage(),ex);
}
}
下一次循环时抛出下面异常:
2007-03-13 17:43:08 [Thread-1] ERROR emersonalarm.EmersonAlarm - [Microsoft][SQLServer 2000 Driver for JDBC]Object has been closed.
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Object has been closed.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseResultSet.validateClosedState(Unknown Source)
at com.microsoft.jdbc.base.BaseResultSet.commonFetchInitialize(Unknown Source)
at com.microsoft.jdbc.base.BaseResultSet.next(Unknown Source)
at emersonalarm.EmersonAlarmDeal.alarmDeal(EmersonAlarmDeal.java:79)
at emersonalarm.EmersonAlarmDeal.timerEvent(EmersonAlarmDeal.java:25)
at emersonalarm.TimerTask.test(TimerTask.java:34)
at emersonalarm.TimerTasks.scan(TimerTasks.java:153)
at emersonalarm.TimerTasks.run(TimerTasks.java:65)
该怎么解决?
參考答案:因该是你重复进行sql操作,当你用完一次数据库的操作的时候应该即时关闭 下次用的时候再打开连接 继续使用!