jdbc 不得不说的几句话

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

今天girlfriend到公司交作业(还是培训期),她们的pm让她们在数据读取的使用FOR循环(如例test1),问之,为啥,说pm说了,这样好,还拿了一本pm给的Effective Java,说看了就明白,废话不多说,看之,原来就这样一条,假如在循环体外不需要继续使用的变量建议使用FOR循环,并且在for的第一部分初始化,这样一来循环结束以后就会自动回收,但是大家仔细思考一下,这地方能利用这条原则??,RESULTSET

是跟着statement走的,当你关闭statement,resultset会被关闭,使你使用FOR循环没有任何好处,相反,WHILE要更快,使用for是弄巧成拙.例子如下,大家也看到while要比for快将近3倍,记录为1000条左右.写这个例子,希望大家不要犯类似的错.....

package jp.gibraltar.bnas.branch.accountclose.closecheck;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Vector;

import jp.gibraltar.util.DBUtil;

import junit.framework.TestCase;

/**

* @author sfluo

*

* TODO To change the template for this generated type comment go to Window -

* Preferences - Java - Code Style - Code Templates

*/

public class CloseCheckInputActionTest extends TestCase {

public static void main(String[] args) {

junit.textui.TestRunner.run(CloseCheckInputActionTest.class);

}

/*

* @see TestCase#setUp()

*/

protected void setUp() throws Exception {

super.setUp();

}

/*

* @see TestCase#tearDown()

*/

protected void tearDown() throws Exception {

super.tearDown();

}

public final void testExecuteCheckInput() {

CloseCheckInputActionTest test = new CloseCheckInputActionTest();

System.out.println(System.currentTimeMillis());

test.test1();

System.out.println(System.currentTimeMillis());

test.test2();

System.out.println(System.currentTimeMillis());

}

void test1() {

Connection conn = DBUtil.getConnection();

Vector vs = new Vector();

try {

Statement stmt = conn.createStatement();

for (ResultSet rs = stmt

.executeQuery("select * from Accesslog_bnas "); rs.next();) {

vs.add(rs.getString(1));

}

stmt.close();

conn.close();

}

catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

void test2() {

Connection conn = DBUtil.getConnection();

Vector vs = new Vector();

try {

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery("select * from accesslog_bnas ");

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航