2020.11.18收获

DaoFactory.java

package com.jaovo.msg.Util;
    import com.jaovo.msg.dao.UserDaoImpl;
    public class DaoFactory {
         public static UserDaoImpl getDaoImpl() {
            return new UserDaoImpl();
     }
}                                
 
DBUtil.java
package com.jaovo.msg.Util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
 //连接数据库
 public  static  Connection getConnection() {
  try {
   //1 加载驱动
   Class.forName("com.mysql.jdbc.Driver").newInstance();
  } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  String user = "root";
  String password = "root";
  String url = "jdbc:mysql://localhost:3306/t_user?&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
  Connection connection = null;
  try {
   //2 创建链接对象connection
    connection = DriverManager.getConnection(url,user,password);
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return connection;
 }
 
 //关闭资源的方法
 public static void close(Connection connection ) {
  try {
   if (connection != null) {
    connection.close();
   }
   
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public static void close(PreparedStatement preparedStatement ) {
  try {
   if (preparedStatement != null) {
    preparedStatement.close();
   }
   
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public static void close(ResultSet resultSet ) {
  try {
   if (resultSet != null) {
    resultSet.close();
   }
   
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
}
 
UserException.java
 
package com.jaovo.msg.Util;
@SuppressWarnings("serial")
public class UserException extends RuntimeException{
 public UserException() {
  super();
  // TODO Auto-generated constructor stub
 }
 public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
  super(message, cause, enableSuppression, writableStackTrace);
  // TODO Auto-generated constructor stub
 }
 public UserException(String message, Throwable cause) {
  super(message, cause);
  // TODO Auto-generated constructor stub
 }
 public UserException(String message) {
  super(message);
  // TODO Auto-generated constructor stub
 }
 public UserException(Throwable cause) {
  super(cause);
  // TODO Auto-generated constructor stub
 }
 
}
 
ValidateUtil.java
 
package com.jaovo.msg.Util;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
public class ValidateUtil {
 public static  boolean validateNull(HttpServletRequest request,String[] fileds) {
  boolean validate = true;
  //map对象用来装载不同的错误信息
  Map<String,String> errorMsg = new HashMap<String, String>();
  for(String filed :fileds) {
   String value = request.getParameter(filed);
   if (value == null || "".equals(value.trim())) {
    validate = false;
    errorMsg.put(filed, filed+"不能为空");
   }
   if (!validate) {
    request.setAttribute("errormsg", errorMsg);
   }
   
  }
  
  return validate;
 }
 public static String showError(HttpServletRequest request , String filed) {
  @SuppressWarnings("unchecked")
  Map<String, String> errorMsg = (Map<String,String>)request.getAttribute("errormsg");
  if (errorMsg == null) {
   return "";
  }
  String msg = errorMsg.get(filed);
  if (msg == null) {
   return "";
  }
  return msg;
 }
 
}
 
 
原文地址:https://www.cnblogs.com/ltw222/p/14067672.html