TestBu是操作bean
在执行事务的时候出错了,希望高说能帮助改改,程序有不妥的地方还望指出,谢谢
package emp;
import java.sql.*;
import java.util.Vector;
public class TestBu{
private String empNo="";
private int deptNo=0;
private String empName="";
private String empCDate="";
private int empPhone=0;
private String empAddr="";
private String empSex="";
private String empGrade="";
private int empSalary=0;
private String empLDate="";
private int historyIndex=0;
private String deptName="";
TestDa cb=new TestDa();
Connection con=cb.getCon();
private PreparedStatement ps;
private PreparedStatement ps1;
ResultSet rs;
ResultSet result;
public void setEmpNo(String empNo){
this.empNo=empNo;
}
public void setDeptNo(int deptNo){
this.deptNo=deptNo;
}
public void setEmpName(String empName){
this.empName=empName;
}
public void setEmpCDate(String empCDate){
this.empCDate=empCDate;
}
public void setEmpPhone(int empPhone){
this.empPhone=empPhone;
}
public void setEmpAddr(String empAddr){
this.empAddr=empAddr;
}
public void setEmpSex(String empSex){
this.empSex=empSex;
}
public void setEmpGrade(String empGrade){
this.empGrade=empGrade;
}
public void setEmpSalary(int empSalary){
this.empSalary=empSalary;
}
public void setEmpLDate(String empLDate){
this.empLDate=empLDate;
}
public void setHistoryIndex(int index){
this.historyIndex=index;
}
public boolean isExist(String sql){
boolean b1=false;
try{
cb.openConnection();
rs=cb.executeQuery(sql);
if(rs.next()){
b1=true;
}
}
catch(Exception e){
System.out.println ("DateBase connection error");
}
finally{
try{
rs.close();
}
catch(Exception es){
System.out.println ("close error");
}
return b1;

}
}
//糤癘魁侗
public int addEmp(){
int n=0;
if(!cb.openConnection()){
System.out.println ("connection error");
}
try{
String sql="insert into employee(empno,deptno,empname,empcdate,empphone,empaddr,empsex,empgrade,empsalary,empldate) values(?,?,?,?,?,?,?,?,?,?)";

ps=cb.createPreparedStatement(sql);
ps.setString(1,this.empNo);
ps.setInt(2,this.deptNo);
ps.setString(3,this.empName);
ps.setString(4,this.empCDate);
ps.setInt(5,this.empPhone);
ps.setString(6,this.empAddr);
ps.setString(7,this.empSex);
ps.setString(8,this.empGrade);
ps.setInt(9,this.empSalary);
ps.setString(10,this.empLDate);
//System.out.println (sql);
ps.executeUpdate();
 
}
catch(Exception se){
System.out.println ("add error");
}
finally{
try{
cb.close();
}
catch(Exception es){
System.out.println ("close error");
}
System.out.println ("add succeed!");
return n ;
}
}
//糤癘魁菌
public int addHistory(){
int num=0;
if(!cb.openConnection()){
System.out.println ("connection error");
}
try{
ps1=cb.createPreparedStatement("insert into employee_history(historyindex,empno,deptno,empcdate,empgrade,empldate) values(?,?,?,?,?,?)");
ps1.setInt(1,this.historyIndex);
ps1.setString(2,this.empNo);
ps1.setInt(3,this.deptNo);
ps1.setString(4,this.empCDate);
ps1.setString(5,this.empGrade);
ps1.setString(6,this.empLDate);
ps1.executeUpdate();
}
catch(Exception se){
System.out.println ("add error");
}
finally{
try{
cb.close();
}
catch(Exception es){
System.out.println ("close error");
}
System.out.println ("add succeed!");
return num ;
}
}
//糤场?
public String addDept(){
if(!cb.openConnection()){
    System.out.println ("connection error");
   }
try{
    ps1=cb.createPreparedStatement("insert into deptno(deptno,deptname) values(?,?)");
    ps1.setInt(1,this.deptNo);
    ps1.setString(2,this.deptName);
    ps1.executeUpdate();
    }
catch(Exception se){
    System.out.println ("addDept error");
    }
finally{
 try{
     cb.close();
     }
      catch(Exception es){
System.out.println ("close error");
}
System.out.println ("adddept succeed!");
return "succeed!";
}
}               
//э獺
public boolean modify(){
boolean b1=false;
cb=new TestDa();
if(!cb.openConnection()){
System.out.println ("connection error");
}
try{
String query="update employee set deptno=?,empname=?,empcdate=?,empphone=?,empaddr=?,empsex=?,empgrade=?,empsalary=?,empldate=? where empno="+"'"+this.empNo+"'";
ps=cb.createPreparedStatement(query);
ps.setInt(1,this.deptNo);
ps.setString(2,this.empName);
ps.setString(3,this.empCDate);
ps.setInt(4,this.empPhone);
ps.setString(5,this.empAddr);
ps.setString(6,this.empSex);
ps.setString(7,this.empGrade);
ps.setInt(8,this.empSalary);
ps.setString(9,this.empLDate);
ps.executeUpdate();
b1=true;
}
catch(Exception se){
System.out.println (" Employee modify error");
}
finally{
try{
cb.close();
}
catch(Exception es){
System.out.println ("close error");
}
System.out.println ("modify succeed!");
return b1;
     }



//琩高ㄧ计
public ResultSet select(String sql){
TestDa cb=new TestDa();
    cb.openConnection();
result=cb.executeQuery(sql);
return result;
}

//ㄏノㄆ叭
public void begin(){

try{
con.setAutoCommit(false);
if(addEmp()>0 && addHistory()>0)
con.commit();
else
            con.rollback();

}catch(Exception ex){
try{
con.rollback();
}catch(Exception e){
System.out.println ("Rollback failed");
}
System.out.println ("Transaction failed");
}
try{
con.close();
}catch(Exception e){}
}

public static void main(String s[]){
TestBu a=new TestBu();
a.setEmpNo("556");
a.setDeptNo(21);
    a.setEmpName("1asf55");
    a.setEmpCDate("2004-05-06");
// a.setdeptName("dkjalsjfls");
    //a.setdeptname("dsfsd")
//a.addDept();
//a.addEmp();
//a.begin();

// a.modify();

}
}
解决立即结贴,谢谢,急啊!