在处理事务的时候,出错,希望高手指点,在线急等!

hanxu2008 2004-08-11 08:18:19
TestDA为底层bean!

package emp;
import java.sql.*;
import java.util.*;
public class TestDa{
private Connection conn=null;
private ResultSet result=null;
private PreparedStatement ps=null;
public Connection getCon(){
openConnection();
return conn;
}

public boolean openConnection(){
try{
Class.forName("org.gjt.mm.mysql.Driver");
}
catch(ClassNotFoundException e){
System.out.print("jdbc error");
return false;
}
try{
this.conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1/test","root","");
}
catch(SQLException ex){
System.out.print("connection db error");
return false;
}

return true;
}
public PreparedStatement createPreparedStatement(String queryString)throws SQLException{
ps=conn.prepareStatement(queryString);
return ps;
}

public void close() throws SQLException{
if(conn!=null)
conn.close();
}

public void finalize()throws Throwable{
this.close();
}

public ResultSet executeQuery(String sql){
openConnection();
/*try{
Class.forName("org.gjt.mm.mysql.Driver");
}catch(ClassNotFoundException e){
System.out.println ("jdbc driver error");

}*/
try{
// conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1/test","root","");
Statement stmt=conn.createStatement();
result=stmt.executeQuery(sql);
}
catch(SQLException e){
System.out.println ("connection error");
}
return result;

}



//磅︽穝ㄧ计
public int executeUpdate(String sql){
int result=0;
try{
Class.forName("org.gjt.mm.mysql.Driver");
}catch(ClassNotFoundException e){
System.out.println ("jdbc driver error");

}
try{
conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1/test","root","");
Statement stmt=conn.createStatement();
result=stmt.executeUpdate(sql);
}
catch(SQLException e){
System.out.println ("connection error");
}
return result;
}
//代刚
public static void main(String s[]){
TestDa a=new TestDa();
if(!a.openConnection()){
System.out.println("connection error!");
}
else{
System.out.println("connection ok!");

}


}



}

...全文
94 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
chancelin 2004-08-11
  • 打赏
  • 举报
回复
出错提示呢?
hanxu2008 2004-08-11
  • 打赏
  • 举报
回复
怎么没人来,难道没人能帮我么?
hanxu2008 2004-08-11
  • 打赏
  • 举报
回复

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();

}
}


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

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧