import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import Handler.SqlConnection;
import Handler.SqlRegist;
import data.User;
public class RegistAction extends ActionSupport{
PreparedStatement presql;
private User user;
public void setUser(User user) {
this.user = user;
}
public User getUser() {
return user;
}
public String execute() throws Exception{
//new SqlRegist().insert();
SqlRegist sr=new SqlRegist();
sr.insert();
return SUCCESS;
}
}
这个是出现问题的类
package Handler;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import data.User;
import tao.min.qi.action.RegistAction;
//此类弃用!!!
//在action中调用此类出先空指针异常
//此类中亦出现空指针异常
//presql.setString(1, RA.getUser().getUsername());
//RA.getUser().getUsername()无法取得前台的值
//问题出现情况尚未解决!!
public class SqlRegist {
PreparedStatement presql;
RegistAction RA=new RegistAction();
public int insert() throws SQLException {
Connection con = new SqlConnection().getConnection();
try {
con.setAutoCommit(false);
String sql = "INSERT INTO user VALUES(?,?,?)";
presql = con.prepareStatement(sql);
presql.setString(1, RA.getUser().getUsername());
presql.setString(2, RA.getUser().getPass());
presql.setString(3, RA.getUser().getPhone());
presql.executeUpdate();
con.commit();
} catch (SQLException e) {
try {
System.out.println("事务操作失败");
con.rollback();
con.setAutoCommit(true);
} catch (SQLException e1) {
System.out.println("失败");
e1.printStackTrace();
}
}
return presql.executeUpdate();
}
}