- public String getPassword(String userId)throws DataAccessException{
- String sql = “select password from userinfo where userid=’”+userId +”’”;
- String password = null;
- Connection con = null;
- Statement s = null;
- ResultSet rs = null;
- try{
- con = getConnection();//获得数据连接
- s = con.createStatement();
- rs = s.executeQuery(sql);
- while(rs.next()){
- password = rs.getString(1);
- }
- rs.close();
- s.close();
- }
- Catch(SqlException ex){
- throw new DataAccessException(ex);
- }
- finally{
- try{
- if(con != null){
- con.close();
- }
- }
- Catch(SQLException sqlEx){
- throw new DataAccessException(“关闭连接失败!”,sqlEx);
- }
- }
- return password;
- }
- public String getPassword(String userId){
- try{
- ……
- Statement s = con.createStatement();
- ……
- Catch(SQLException sqlEx){
- ……
- }
- ……
- }
- public String getPassword(String userId)throws SQLException{
- Statement s = con.createStatement();
- }
- String str = “123”;
- int value = Integer.parseInt(str);
- public static int parseInt(String s) throws NumberFormatException
- try{
- ……
- Statement s = con.createStatement();
- ……
- Catch(SQLException sqlEx){
- sqlEx.PrintStackTrace();
- }
- 或者
- try{
- ……
- Statement s = con.createStatement();
- ……
- Catch(SQLException sqlEx){
- //什么也不干
- }
- public void methodA()throws ExceptionA{
- …..
- throw new ExceptionA();
- }
- public void methodB()throws ExceptionB{
- try{
- methodA();
- ……
- }catch(ExceptionA ex){
- throw new ExceptionB(ex);
- }
- }
- Public void methodC()throws ExceptinC{
- try{
- methodB();
- …
- }
- catch(ExceptionB ex){
- throw new ExceptionC(ex);
- }
- }
如
- IllegalArgumentException, UnsupportedOperationException
- public void methodA()throws ExceptionA{
- …..
- throw new ExceptionA();
- }
- public void methodB()throws ExceptionB{
- try{
- methodA();
- ……
- }catch(ExceptionA ex){
- throw new ExceptionB(ex);
- }
- }
- public Class ExceptionB extends Exception{
- private Throwable cause;
- public ExceptionB(String msg, Throwable ex){
- super(msg);
- this.cause = ex;
- }
- public ExceptionB(String msg){
- super(msg);
- }
- public ExceptionB(Throwable ex){
- this.cause = ex;
- }
- }
- public void printStackTrace(PrintStrean ps){
- if(cause == null){
- super.printStackTrace(ps);
- }else{
- ps.println(this);
- cause.printStackTrace(ps);
- }
- }
- public NestedException extends Exception{
- private Throwable cause;
- public NestedException (String msg){
- super(msg);
- }
- public NestedException(String msg, Throwable ex){
- super(msg);
- This.cause = ex;
- }
- public Throwable getCause(){
- return (this.cause == null ? this :this.cause);
- }
- public getMessage(){
- String message = super.getMessage();
- Throwable cause = getCause();
- if(cause != null){
- message = message + “;nested Exception is ” + cause;
- }
- return message;
- }
- public void printStackTrace(PrintStream ps){
- if(getCause == null){
- super.printStackTrace(ps);
- }else{
- ps.println(this);
- getCause().printStackTrace(ps);
- }
- }
- public void printStackTrace(PrintWrite pw){
- if(getCause() == null){
- super.printStackTrace(pw);
- }
- else{
- pw.println(this);
- getCause().printStackTrace(pw);
- }
- }
- public void printStackTrace(){
- printStackTrace(System.error);
- }
- }
- public String getPassword(String userId)throws NoSuchUserException{
- UserInfo user = userDao.queryUserById(userId);
- If(user == null){
- Logger.info(“找不到该用户信息,userId=”+userId);
- throw new NoSuchUserException(“找不到该用户信息,userId=”+userId);
- }
- else{
- return user.getPassword();
- }
- }
- public void sendUserPassword(String userId)throws Exception {
- UserInfo user = null;
- try{
- user = getPassword(userId);
- //……..
- sendMail();
- //
- }catch(NoSuchUserException ex)(
- logger.error(“找不到该用户信息:”+userId+ex);
- throw new Exception(ex);
- }
- public Date getDate(String str){
- Date applyDate = null;
- SimpleDateFormat format = new SimpleDateFormat(“MM/dd/yyyy”);
- try{
- applyDate = format.parse(applyDateStr);
- }
- catch(ParseException ex){
- //乎略,当格式错误时,返回null
- }
- return applyDate;
- }
- try{
- ……
- String sql=”select * from userinfo”;
- Statement s = con.createStatement();
- ……
- Catch(SQLException sqlEx){
- Logger.error(“sql执行错误”+sql+sqlEx);
- }
- public class BusinessException extends Exception {
- private void logTrace() {
- StringBuffer buffer=new StringBuffer();
- buffer.append("Business Error in Class: ");
- buffer.append(getClassName());
- buffer.append(",method: ");
- buffer.append(getMethodName());
- buffer.append(",messsage: ");
- buffer.append(this.getMessage());
- logger.error(buffer.toString());
- }
- public BusinessException(String s) {
- super(s);
- race();
- }
- //
- public class UserSoaImpl implements UserSoa{
- public UserInfo getUserInfo(String userId)throws RemoteException{
- //……
- 远程方法调用.
- //……
- }
- }
- public interface UserManager{
- public UserInfo getUserInfo(Stirng userId)throws RemoteException;
- }
- public DataAccessException extends RuntimeException{
- ……
- }
- public interface UserDao{
- public String getPassword(String userId)throws DataAccessException;
- }
- public class UserDaoImpl implements UserDAO{
- public String getPassword(String userId)throws DataAccessException{
- String sql = “select password from userInfo where userId= ‘”+userId+”’”;
- try{
- …
- //JDBC调用
- s.executeQuery(sql);
- …
- }catch(SQLException ex){
- throw new DataAccessException(“数据库查询失败”+sql,ex);
- }
- }
- }
- public class BusinessException extends Exception{
- …..
- }
- public interface UserManager{
- public Userinfo copyUserInfo(Userinfo user)throws BusinessException{
- Userinfo newUser = null;
- try{
- newUser = (Userinfo)user.clone();
- }catch(CloneNotSupportedException ex){
- throw new BusinessException(“不支持clone方法:”+Userinfo.class.getName(),ex);
- }
- }
- }
- ModeAndView handleRequest(HttpServletRequest request,HttpServletResponse response)throws Exception{
- String ageStr = request.getParameter(“age”);
- int age = Integer.parse(ageStr);
- …………
- String birthDayStr = request.getParameter(“birthDay”);
- SimpleDateFormat format = new SimpleDateFormat(“MM/dd/yyyy”);
- Date birthDay = format.parse(birthDayStr);
- }
- ModeAndView handleRequest(HttpServletRequest request,HttpServletResponse response)throws Exception{
- String ageStr = request.getParameter(“age”);
- String birthDayStr = request.getParameter(“birthDay”);
- int age = 0;
- Date birthDay = null;
- try{
- age=Integer.parse(ageStr);
- }catch(NumberFormatException ex){
- error.reject(“age”,”不是合法的整数值”);
- }
- …………
- try{
- SimpleDateFormat format = new SimpleDateFormat(“MM/dd/yyyy”);
- birthDay = format.parse(birthDayStr);
- }catch(ParseException ex){
- error.reject(“birthDay”,”不是合法的日期,请录入’MM/dd/yyy’格式的日期”);
- }
- }
附上100%完整的系统项目源码:
JSP图书馆管理系统:http://blog.sina.com.cn/s/blog_4b5bc0110101a97e.html
JSP酒店宾馆管理系统:http://blog.sina.com.cn/s/blog_4b5bc0110101a97m.html
JSP学生信息管理系统:http://blog.sina.com.cn/s/blog_4b5bc0110101a97r.html
JSP房屋出售租赁管理系统:http://blog.sina.com.cn/s/blog_4b5bc0110101a98c.html
J2EE酒店在线预订系统:http://blog.sina.com.cn/s/blog_4b5bc0110101a97v.html
SSH/J2EE人力资源管理系统:http://blog.sina.com.cn/s/blog_4b5bc01101019ztu.html
ssh2图书管理系统(图书馆管理系统): http://blog.sina.com.cn/s/blog_4b5bc0110101adf0.html
毕业设计-JSP图书馆管理系统:http://blog.sina.com.cn/s/blog_4b5bc0110101a979.html
毕业设计-JSP酒店宾馆管理系统:http://blog.sina.com.cn/s/blog_4b5bc0110101a97z.html
毕业设计-JSP学生信息管理系统:http://blog.sina.com.cn/s/blog_4b5bc0110101a987.html
毕业设计-JSP房屋出售租赁管理系统:http://blog.sina.com.cn/s/blog_4b5bc0110101a98a.html
毕业设计-s2sh/j2ee图书管理系统 :http://blog.sina.com.cn/s/blog_4b5bc0110101ain5.html
ssh2图书管理系统(图书馆管理系统):http://blog.sina.com.cn/s/blog_4b5bc0110101adf0.html
毕业设计-jsp信息发布系统(信息供求网站系统):http://blog.sina.com.cn/s/blog_4b5bc0110101aiou.html
struts2+servlet+jsp信息发布系统:http://blog.sina.com.cn/s/blog_4b5bc0110101aiop.html
毕业设计-jsp电子商城 网上商城系统:http://blog.sina.com.cn/s/blog_4b5bc0110101aiof.html
jsp电子商城 网上商城系统(struts+servlet+jsp):http://blog.sina.com.cn/s/blog_4b5bc0110101aio9.html
毕业设计-图书管理系统 jsp图书馆系统Struts2+Spring+Ibatis+extjs(ssi):
http://blog.sina.com.cn/s/blog_4b5bc0110101ainu.html
Struts2+Spring+Ibatis+extjs(ssi)图书管理系统 jsp图书馆系统:
http://blog.sina.com.cn/s/blog_4b5bc0110101ainh.html
ssh2图书管理系统(图书馆管理系统):http://blog.sina.com.cn/s/blog_4b5bc0110101adf0.html
毕业设计-s2sh/j2ee图书管理系统 struts2+spring+hibernate:
http://blog.sina.com.cn/s/blog_4b5bc0110101ain5.html
jsp酒店在线预订系统 酒店客房预定系统:http://blog.sina.com.cn/s/blog_4b5bc0110101atb8.html
毕业设计 jsp酒店在线预订系统 酒店客房预定系统:http://blog.sina.com.cn/s/blog_4b5bc0110101atbb.html
人力资源管理系统 S2SH/J2EE/JAVA:http://blog.sina.com.cn/s/blog_4b5bc0110101azoz.html
毕业设计-人力资源管理系统 S2SH/J2EE/JSP:http://blog.sina.com.cn/s/blog_4b5bc0110101azp0.html
ssh网上商城 电子商城struts hibernate :http://blog.sina.com.cn/s/blog_4b5bc0110101b5gr.html
毕业设计 ssh网上商城 电子商城struts hibernate:http://blog.sina.com.cn/s/blog_4b5bc0110101b5h2.html
毕业设计 ssh电子相册管理系统:http://blog.sina.com.cn/s/blog_4b5bc0110101bkbl.html
实用技术:
J2EE/JSP应用技术70实例(源码)(实用): http://blog.sina.com.cn/s/blog_4b5bc0110101acms.html