java - Which Place to write connection.close() and preparedstatement.close() -



java - Which Place to write connection.close() and preparedstatement.close() -

i new in jdbc ...

student class has methods constructor, add(),update() , delete() etc ...

open connection in constructor. place write conn.close() , pstmt.close() in below code help me

class pupil { connection conn; preparedstatement pstmt; resultset rs; public student() { seek { class.forname("com.mysql.jdbc.driver"); conn=drivermanager.getconnection("jdbc:mysql://localhost:3306/test","root","root"); } catch(exception e) { system.out.println("error :"+e.getmessage()); } } public void add(int rollno,string name) { seek { pstmt = conn.preparestatement("insert pupil values (?, ?)"); pstmt.setint(1,rollno); pstmt.setstring(2, name); int = pstmt.executeupdate(); if (i != 0) { system.out.println("record inserted"); } else { system.out.println("record not inserted"); } } catch(exception e) { system.out.println("error :"+e.getmessage()); } } public void update(int rollno,string name) { seek { pstmt = conn.preparestatement("update pupil set name=? rollno=?"); pstmt.setstring(1, name); pstmt.setint(2,rollno); int = pstmt.executeupdate(); if (i != 0) { system.out.println("record updated"); } else { system.out.println("record not updated"); } } catch(exception e) { system.out.println("error :"+e.getmessage()); } } public void delete(int rollno) { seek { pstmt = conn.preparestatement("delete pupil rollno=?"); pstmt.setint(1,rollno); int = pstmt.executeupdate(); if (i != 0) { system.out.println("record deleted"); } else { system.out.println("record not deleted"); } } catch(exception e) { system.out.println("error :"+e.getmessage()); } } }

based on sample code have posted, seems operations trying add update , delete utilize same connection object created globally , initialized in constructor "student()". this not standard practice

as per standards should not that, should create new connections local variables on each of operations 'add', 'update' , 'delete' seperately

also maintain in mind if using 'bean managed transaction' need commit transaction before closing it.or else add together conn.commit(); in block

here quick illustration on how method should be

public void add(int rollno,string name) { connection conn = null; preparedstatement pstmt = null; seek { pstmt = conn.preparestatement("insert pupil values (?, ?)"); pstmt.setint(1,rollno); pstmt.setstring(2, name); int = pstmt.executeupdate(); if (i != 0) { system.out.println("record inserted"); } else { system.out.println("record not inserted"); } } catch(exception e) { system.out.println("error :"+e.getmessage()); } { if(pstmt!=null) { pstmt.close(); } if(conn!=null) { conn.comit(); // add together line if utilize bean managed transaction conn.close(); } } }

java jdbc database-connection prepared-statement

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -