PreparedStatement stmtDelete = null;
PreparedStatement stmt = null;try {
stmtDelete = conn.prepareStatement(DETAIL_DELETE);
stmtDelete.executeUpdate();
} catch (SQLException e1) {
e1.printStackTrace();
}
for (int i = 0; i < rowCount; i++) {
try {
stmt = conn.prepareStatement(SQL_INSERT); stmt.executeUpdate();
System.out.println("inserted Yes!");
} catch (SQLException e) {
throw new DataAccessException(e.getMessage(), e);
} finally {
try {
if (stmt != null) {
stmt.close();
}
}
}
}//////////////////////////////////////
stmtDelete是删除,stmt是插入
执行stmt时出错了,stmtDelete的语句也没有提交
默认是自动提交的,它是在什么时候提交的?
/////////////////////////////////////
再加个
for(int i=0;;i++){
List list=new ArrayList();
list.add(new String("abc"));
list=null;
}
这个循环会不会内存泄露?
PreparedStatement stmt = null;try {
stmtDelete = conn.prepareStatement(DETAIL_DELETE);
stmtDelete.executeUpdate();
} catch (SQLException e1) {
e1.printStackTrace();
}
for (int i = 0; i < rowCount; i++) {
try {
stmt = conn.prepareStatement(SQL_INSERT); stmt.executeUpdate();
System.out.println("inserted Yes!");
} catch (SQLException e) {
throw new DataAccessException(e.getMessage(), e);
} finally {
try {
if (stmt != null) {
stmt.close();
}
}
}
}//////////////////////////////////////
stmtDelete是删除,stmt是插入
执行stmt时出错了,stmtDelete的语句也没有提交
默认是自动提交的,它是在什么时候提交的?
/////////////////////////////////////
再加个
for(int i=0;;i++){
List list=new ArrayList();
list.add(new String("abc"));
list=null;
}
这个循环会不会内存泄露?
解决方案 »
- struts2出现java.lang.OutOfMemoryError: PermGen space!
- 求:string中截取数字
- 求解一个多线程问题
- 我写入随即产生的数字进入TXT文件中 但我打开TXT中读不出来读出来的是乱码
- java 动态生成insert文
- 面试了两次,都是考基础题,我没有把基础题复习好,我真是失败。明天我又到一家公家面试,求祝福.........
- 请教
- 怎么读取ini文件啊
- 为什么我用flowlayout的时候,用setMinimumSize无效?
- [急]JSP+Applet出错...
- 为什么论坛的一些不错的贴子的网页保存不了呢?就是保存到24%的时候说无法保存网页.
- JAVA各个版本的开发环境是不是一样的?
conn.setAutoCommit(false),比如:public void insertUser(User user) {
Connection conn=null;
try
{
conn=JdbcUtil.getConnection();
conn.setAutoCommit(false);
PreparedStatement ps=conn.prepareStatement("insert into user(username,fullname,password)values(?,?,?)");
ps.setString(1, user.getUserName());
ps.setString(2, user.getFullName());
ps.setString(3, user.getPwd());
ps.executeUpdate();
conn.commit();
}
catch(SQLException e)
{
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
catch(Exception e)
{
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
finally
{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
如果不这样,系统自动提交
stmtDelete = conn.prepareStatement(DETAIL_DELETE);
提交一次
stmtDelete.executeUpdate();
又提交一次
for(int i=0;;i++){
List list=new ArrayList();
list.add(new String("abc"));
list=null;
}
不会内存泄露
是个死循环,JAVA垃圾回收器会进行垃圾回收,但是这是在系统内存耗量过大或者式没有足够内存
分配时才会调用垃圾回收,这个语句会造成大量垃圾
insert,update,delete三种是在executeUpdate()或execute()返回时 jammychen:
建议conn.setAutoCommit(false),然后手动提交
如果不这样,系统自动提交
stmtDelete = conn.prepareStatement(DETAIL_DELETE);
提交一次
stmtDelete.executeUpdate();
又提交一次那么stmtDelete应该是提交了,可数据库里数据没有改变,所以比较奇怪