遍历提交表单取值
1 package com.project.util; 2 3 import java.lang.reflect.Field; 4 import java.sql.Date; 5 import java.util.Enumeration; 6 import javax.servlet.http.HttpServletRequest; 7 8 public class FullBean { 9 public void fullBean(HttpServletRequest request,Object BeanObj){ 10 //得到表单元素所有名称枚举 11 Enumeration<String> em=request.getParameterNames(); 12 //遍历枚举 13 while (em.hasMoreElements()) { 14 //取出一个枚举元素 15 String formName=em.nextElement(); 16 //通过枚举元素取值 17 String value=request.getParameter(formName); 18 try { 19 //通过置顶属性名得到属性对象 20 Field beanField=BeanObj.getClass().getDeclaredField(formName); 21 //去掉访问修饰符 22 beanField.setAccessible(true); 23 //将BeanObj中指定的属性,设置value值(根据不同类型做转换) 24 if (beanField.getType()==String.class) { 25 beanField.set(BeanObj, value); 26 }else if (beanField.getType()==int.class||beanField.getType()==Integer.class) { 27 beanField.set(BeanObj, Integer.parseInt(value)); 28 }else if (beanField.getType()==double.class||beanField.getType()==Double.class) { 29 beanField.set(BeanObj, Double.parseDouble(value)); 30 }else if (beanField.getType()==Date.class) { 31 beanField.set(BeanObj, Date.valueOf(value)); 32 }else if (beanField.getType()==String[].class) { 33 beanField.set(BeanObj, request.getParameterValues(formName)); 34 } 35 } catch (Exception e) { 36 // TODO Auto-generated catch block 37 e.printStackTrace(); 38 } 39 40 41 } 42 } 43 }
表单文件提交
1 package com.project.util; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.Part; 5 6 public class FileUploadUtil { 7 public String uploadUtil(HttpServletRequest request,String formName,String dirName) { 8 String fieleName =null; 9 try { 10 Part part = request.getPart(formName); 11 // 得到请求头,从而得到上传文件名称 12 String head = part.getHeader("Content-Disposition"); 13 // 以时间毫秒数作为文件名,后缀不变,重命名上传文件名 14 fieleName = System.currentTimeMillis() + head.substring(head.lastIndexOf("."), head.length() - 1); 15 // 得到文件上传真实路径 16 String filePath = request.getServletContext().getRealPath("/"+dirName+"/" + fieleName); 17 // 将上传文件数据写入真实路径 18 part.write(filePath); 19 } catch (Exception e) { 20 // TODO Auto-generated catch block 21 e.printStackTrace(); 22 } 23 return fieleName; 24 } 25 }
过滤器修改编码集
1 package com.project.util; 2 3 import java.io.IOException; 4 5 import javax.servlet.Filter; 6 import javax.servlet.FilterChain; 7 import javax.servlet.FilterConfig; 8 import javax.servlet.ServletException; 9 import javax.servlet.ServletRequest; 10 import javax.servlet.ServletResponse; 11 import javax.servlet.annotation.WebFilter; 12 13 @WebFilter("/*") 14 public class CharFileter implements Filter{ 15 16 @Override 17 public void destroy() { 18 // TODO Auto-generated method stub 19 20 } 21 22 @Override 23 public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) 24 throws IOException, ServletException { 25 26 arg0.setCharacterEncoding("utf-8"); 27 arg1.setContentType("text/html;charset=utf-8"); 28 arg2.doFilter(arg0, arg1); 29 30 } 31 32 @Override 33 public void init(FilterConfig arg0) throws ServletException { 34 // TODO Auto-generated method stub 35 36 } 37 38 }
日期转换
package com.project.util; import java.sql.Date; /** * 日期转换工具类 * @author Administrator * */ public class DateChange { public static Date getDate(String str){ if (str!=null&&str.matches("\\d{4}-\\d{1,2}-\\d{1,2}")) { return Date.valueOf(str); } return null; } }
数据库持久层
1 package com.project.dao; 2 3 import java.lang.reflect.Field; 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.PreparedStatement; 7 import java.sql.ResultSet; 8 import java.sql.ResultSetMetaData; 9 import java.sql.SQLException; 10 import java.util.ArrayList; 11 import java.util.List; 12 13 import com.project.bean.UserBean; 14 import com.sun.org.apache.bcel.internal.generic.AALOAD; 15 16 /** 17 * 数据库持久接口 18 * 19 * @author Administrator 20 * 21 */ 22 public class BaseDao { 23 24 // 链接对象 25 protected Connection con; 26 // sql执行对象 27 protected PreparedStatement ps; 28 // 返回结果集对象 29 protected ResultSet rs; 30 31 /** 32 * 得到链接 33 */ 34 public void setConnection() { 35 try { 36 Class.forName("com.mysql.jdbc.Driver"); 37 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop?characterEncoding=utf-8", "root", 38 "jbl5201314"); 39 } catch (Exception e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } 43 } 44 45 /** 46 * 关闭链接 47 */ 48 public void closeConnection() { 49 try { 50 if (rs != null) { 51 rs.close(); 52 } 53 if (ps != null) { 54 ps.close(); 55 } 56 if (con != null) { 57 con.close(); 58 } 59 } catch (Exception e) { 60 // TODO Auto-generated catch block 61 e.printStackTrace(); 62 } 63 } 64 65 /** 66 * 增删改 67 * 68 * @param sql 执行sql语句 69 * @param vlaueArry 占位符数组 70 */ 71 public void updateDb(String sql, Object[] vlaueArry) { 72 this.setConnection(); 73 try { 74 ps = con.prepareStatement(sql); 75 //循环设置占位符 76 for (int i = 0; i < vlaueArry.length; i++) { 77 ps.setObject(i + 1, vlaueArry[i]); 78 } 79 ps.executeUpdate(); 80 } catch (SQLException e) { 81 e.printStackTrace(); 82 } finally { 83 this.closeConnection(); 84 } 85 } 86 87 /** 88 * 查询 89 * 90 * @param sql 执行sql语句 91 * @param vlaueArry 占位符数组 92 * @return 返回结果集 93 */ 94 public List selUser(String sql, Object[] valueArry, Class beanClass) { 95 this.setConnection(); 96 List list=new ArrayList(); 97 try { 98 ps = con.prepareStatement(sql); 99 //循环设置占位符 100 if (valueArry.length != 0) { 101 for (int i = 0; i < valueArry.length; i++) { 102 ps.setObject(i + 1, valueArry[i]); 103 } 104 } 105 rs = ps.executeQuery(); 106 //得到结果集审视 107 ResultSetMetaData sm=rs.getMetaData(); 108 //得到结果集列数 109 int columCount=sm.getColumnCount(); 110 while (rs.next()) { 111 // 每次循环,调用无参构造方法,产生实体类对象 112 Object beanObj = beanClass.newInstance(); 113 for (int i = 1; i <= columCount; i++) { 114 //循环取出查询列列名 115 String columName=sm.getColumnName(i); 116 //根据列名得到结果集所对应的值 117 Object value=rs.getObject(columName); 118 //得到属性名对应的属性对象 119 Field f=beanClass.getDeclaredField(columName); 120 f.setAccessible(true); 121 f.set(beanObj, value); 122 } 123 list.add(beanObj); 124 } 125 } catch (Exception e) { 126 e.printStackTrace(); 127 }finally { 128 this.closeConnection(); 129 } 130 return list; 131 } 132 }
时间: 2024-10-25 05:05:23