比如有一个 ResultSet类 对象rsStinrg s="";s=rs.getString("aaa"); // aaa字段里的值是 "abc"rs.close();s? 因为s接收的是地址,如果 rs.close() 关闭,会不会影响到s 的值,为保险起见 用 s = new String(rs.getString("aaa")); 这样的写法如何?
调试欢乐多
rs已经把值赋给了s
放在
s=rs.getString("aaa");
后面,就不会有什么影响。Stinrg s="";
s=rs.getString("aaa");
这两个语句不要分开写。
要写成Stinrg s=rs.getString("aaa"); 也不要
s = new String(rs.getString("aaa"));
这样写,会有两个对象产生,浪费空间,影响效率。
内存中已经生成了一个String对象
而s就指向这个对象
所以和rs关闭没有关系
rs关闭其实也就是让其指向null,好被垃圾回收
void close()
throws SQLException
Releases this ResultSet object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.
The closing of a ResultSet object does not close the Blob, Clob or NClob objects created by the ResultSet. Blob, Clob or NClob objects remain valid for at least the duration of the transaction in which they are creataed, unless their free method is invoked.When a ResultSet is closed, any ResultSetMetaData instances that were created by calling the getMetaData method remain accessible.Note: A ResultSet object is automatically closed by the Statement object that generated it when that Statement object is closed, re-executed, or is used to retrieve the next result from a sequence of multiple results.Calling the method close on a ResultSet object that is already closed is a no-op.Throws:
SQLException - if a database access error occurs
源码里关闭操作就是让ResultSet指向NULL
尽管你的s是一个CLASS变量,它也只是一个引用而已,而你的CLASS变量同样也是引用,都由jvm来决定会后谁,所以没有问题
如果 String s="";s=rs.getString("aaa");
这样的写法不好,那这样呢:String s;s=rs.getString("aaa");