關於java執行緒釋放資源問題

晨曦發表於2004-12-31
我於執行執行緒的時間出現這個錯誤
從一些資料上看到說是超過最大遊標,需要修改程式碼
把迴圈改成外迴圈,可改成外迴圈的話,那我連線是無法獲取資料庫的資料

出現ORA-01000
java.sql.SQLException: ORA-01000: maximum open cursors exceeded

at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:289)
at oracle.jdbc.ttc7.Oopen.receive(Oopen.java:120)
at oracle.jdbc.ttc7.TTC7Protocol.open(TTC7Protocol.java:611)
at oracle.jdbc.driver.OracleStatement.open(OracleStatement.java:575)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:2803)
at oracle.jdbc.driver.OracleStatement.executeQuery(OracleStatement.java:640)
at cn.soyu.sender.SendMailTest_G.sendMail(SendMailTest_G.java:107)
at cn.soyu.sender.SendMailTest_G.main(SendMailTest_G.java:269)

下面是我的程式碼,
請大家幫我看看
紅字的部分資料說要改成外迴圈

程式碼
/*
* Created on 2004-12-28
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package cn.soyu.sender;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import cn.soyu.sender.comm.Email_Autherticatorbean;

/**
* @author singlebin
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class SendMailTest_G
{
public void sendMail(int year,int month)
{
System.out.println("Action start at:" + new Date().getTime());

String riqi1 = ""; //要查詢的月份時間
if (month < 10)
{
riqi1 = riqi1.valueOf(year) + "0" + riqi1.valueOf(month);
}
else
{
riqi1 = riqi1.valueOf(year) + riqi1.valueOf(month);
}

String riqi2 = ""; //要查詢的月份時間的下一個月的時間
if (month > 0 & month < 9)
{
riqi2 = riqi2.valueOf(year) + "0" + riqi2.valueOf(month + 1);
}
else if (month > 9 & month < 12)
{
riqi2 = riqi2.valueOf(year) + riqi2.valueOf(month + 1);
}
else
{
riqi2 = riqi2.valueOf(year + 1) + "0" + riqi2.valueOf(1);
}
try
{
//連線資料庫,得到連線
Connection conn = null;
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn = DriverManager
.getConnection("jdbcracle:thin:@127.0.0.1:1521:billing",
"billing", "abcdef");
System.out.println("getConnection");
//按年月得到該月中使用過業務的每個使用者的手機號
String sqlMobile = "select distinct acct_mobile_num from g_gd165_local where"
+ " charge_date > '" + riqi1 + "' and charge_date < '" + riqi2 + "'"
+ " and rownum<1000000000 "+" and acct_locate_code = 756 "
+" and acct_mobile_num>'13150000000' and acct_mobile_num<'13200000001'" ;
System.out.println("sqlMobile = " +"\n"+ sqlMobile);

Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sqlMobile);
//rs:手機號
while (rs.next())
{
String mobile = null;
mobile = rs.getString("acct_mobile_num");
//得到彙總
StringBuffer sqlBufAccount = new StringBuffer();
//輸出彙總資訊
StringBuffer account = new StringBuffer();
account
.append("使用者:").append("\n")
.append(" 如下").append("\n")
.append(mobile).append("\n")
.append("---------------------------------------------------------------------------").append("\n")
.append("使用者:").append(mobile).append(" 手機號碼:").append(mobile)
.append(" 計費月份:").append(riqi1).append("\n");
//得到詳單
StringBuffer sqlBufDetail = new StringBuffer();
sqlBufDetail.append("select rownum,oppose_number,")
.append("org_trm_id ,charge_date,charge_time,call_duration,")
.append("charge,").append("oppose_area_code from g_gd165_local_756 where charge_date > '")
.append(riqi1).append("'and charge_date < '").append(riqi2).append("' and acct_mobile_num ='")
.append(mobile).append("'").append(" and rownum<5000");
System.out.println("得到詳單 = "+"\n"+sqlBufDetail.toString());
Connection con = null;
con = DriverManager
.getConnection("jdbcracle:thin:@127.0.0.1:1521:billing",
"billing", "abcdef");
Statement stm = conn.createStatement();
ResultSet rs3 = null;
rs3 = stm.executeQuery(sqlBufDetail.toString());
//顯示詳單資訊
StringBuffer detail = new StringBuffer();
detail
.append("-------------------------------------------------------------------------------").append("\n")
.append(" NO. ").append(" ").append("對方號碼 ").append(" ").append("型別 ").append(" ").append("日 期")
.append(" ").append("時 間").append(" ").append("時長(秒)").append(" ").append("話費(元) ").append(" ")
.append("通話地").append("\n")
.append("-------------------------------------------------------------------------------").append("\n");
while(rs3.next())
{
String org1=null;
if(rs3.getString("oppose_number")==null)
org1 = "0";
else
org1=rs3.getString("oppose_number");

detail
.append(StringUtil.padString(rs3.getString("rownum"),' ',3,StringUtil.LEFT))
.append(" ")
.append(StringUtil.padString(org1,' ',18,StringUtil.LEFT))
.append(" ")
.append(StringUtil.padString(org,' ',4,StringUtil.LEFT)).append("")
.append(" ")
.append(StringUtil.padString(rs3.getString("charge_date"),' ',8,StringUtil.LEFT))
.append(" ")
.append(StringUtil.padString(rs3.getString("charge_time"),' ',4,StringUtil.LEFT))
.append(" ")
.append(StringUtil.padString(String.valueOf(rs3.getInt("call_duration")),' ',4,StringUtil.LEFT))
.append("\t")
.append(StringUtil.padString(String.valueOf(rs3.getFloat("charge")),' ',4,StringUtil.LEFT))
.append("\t")
.append(StringUtil.padString(rs3.getString("oppose_area_code"),' ',4,StringUtil.LEFT))
.append("\n");
}
if(rs3 != null)
rs3 = null;
detail
.append("-------------------------------------------------------------------------------").append("\n") System.out.println("詳單資訊 = "+"\n"+detail.toString());
sendMain(account.toString()+detail.toString(),mobile+"@gd165.com");
}

下面這段程式碼有人說需要改成外迴圈
《《

if(rs!=null)
{
rs.close();
rs = null;
}
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (java.sql.SQLException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
》》
System.out.println("Action end at:" + new Date().getTime());
}

public static void sendMain(String content1, String to1)
{
//收件人
String to = null;
//發件人
String from = null;
//主題
String subject = null;
//抄送人
String cc = null;
//暗抄送
String bcc = null;
// mail 主機
String mailhost = null;
// mail 內容
String content = null;
//MIME郵件物件
MimeMessage mimeMsg = null;
//郵件會話物件
Session session = null;
//************ 不同之處 *************/
String user = null;
String password = null;
try
{
mailhost = "mail";
from = "singlebin@163.com";
to = to1;
subject = "";
content = content1;
//content = "這是個帶身份驗證的JavMail!"+"\n"+"This is a test";
user = "support";
password = "mail";

Properties props = System.getProperties(); //獲得系統屬性
props.put("mail.smtp.host", mailhost); //設定SMTP主機
props.put("mail.smtp.auth", "true"); //設定身份驗證為真,若須身份驗證則必須設為真

//獲得郵件會話物件
//session = Session.getDefaultInstance(props,null);
//第二個引數為身份驗證
session = Session
.getDefaultInstance(props, new Email_Autherticatorbean(user, password));

//建立MIME郵件物件
mimeMsg = new MimeMessage(session);
//設定發信人
mimeMsg.setFrom(new InternetAddress(from));

//設定收信人
if (to != null)
{
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
}

//設定抄送人
if (cc != null)
{
mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
}

//設定暗送人
if (bcc != null)
{
mimeMsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc));
}

//設定郵件主題
mimeMsg.setSubject(subject, "GBK");

//設定郵件內容
mimeMsg.setText(content, "GBK");
//傳送日期
mimeMsg.setSentDate(new Date());
//傳送郵件
Transport.send(mimeMsg);
//System.out.println( "Email send!");

}
catch (Exception e)
{
e.printStackTrace();
}
}

public static void main(String[] args)
{
SendMailTest_G test = new SendMailTest_G();
test.sendMail(2004,11);
}
}








相關文章