代码
import java.sql.*;public class getconnection{ public static void main(string[] args){ access2database adb=new access2database(); connection conn=adb.getconn(); //transaction dealing preparedstatement pstam=null; try{ conn.setautocommit(false); string sql=insert into student(name,major,score) values(?,?,?);; pstam=conn.preparestatement(sql); pstam.setstring(1, f); pstam.setstring(2,history); pstam.setint(3, 67); pstam.addbatch(); pstam.setstring(1, h); pstam.setstring(2, biology); pstam.setint(3, 85); pstam.addbatch(); pstam.executebatch(); conn.commit(); }catch(sqlexception e){ try { conn.rollback(); } catch (sqlexception e1) { // todo auto-generated catch block e1.printstacktrace(); } e.printstacktrace(); }finally{ try { conn.setautocommit(true); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } //release the resource of the program try{ pstam.close(); conn.close(); }catch(sqlexception e){ e.printstacktrace(); } }}
