在处理事务的时候,出错,希望高手指点,在线急等!
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!");
}
}
}