java使用第三方工具BeanUtils

学习开发过程中有的代码分享下:供大家参考

使用SimpleDateFormat来进行日期格式的转换

  1. import java.text.SimpleDateFormat;

    import com.dao.UserDao;
    import com.domain.user;
    import com.mysql.jdbc.Connection;
    import com.mysql.jdbc.PreparedStatement;
    import com.utils.DBUtils;

    public class UserDaoImpl implements UserDao {

        public void addUser(user user) throws Exception {
            // TODO Auto-generated method stub
            Connection conn = null;
            PreparedStatement ps = null;
            
            try {
                conn = (Connection) DBUtils.getConnection();
                ps = (PreparedStatement) conn.prepareStatement("insert into users (username,password,email,birthday) values(?,?,?,?,)");
                
                ps.setString(1, user.getUsername());
                ps.setString(2,user.getPassword());
                ps.setString(3,user.getEmail());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
                String date = sdf.format(user.getBirthday());
                ps.setString(4,date);
                
                int i = ps.executeUpdate();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                throw new RuntimeException("添加失败");
            }finally {
                DBUtils.closeAll(null, ps, conn);
            }
            
        }

    }
  2. 看到有博主也是这样使用的
    1. @Test  
    2. public void test2() throws Exception{  
    3.     User newUser=new User();  
    4.     String birthString="1993-09-30";  
    5.     ConvertUtils.register(new Converter(){  
    6.         @Override  
    7.         public Object convert(Class type, Object value) {  
    8.             if( type != Date.class) return null;  
    9.             if(value == null || "".equals(value.toString().trim())) return null;  
    10.             try {  
    11.                 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
    12.                 return sdf.parse(value.toString());  
    13.             } catch (ParseException e) {  
    14.                 throw new RuntimeException(e);  
    15.             }  
    16.         }  
    17.     },Date.class);  
    18.     BeanUtils.copyProperty(newUser, "birth", birthString);  
    19.     System.out.println(newUser.getBirth());  
    20. }

    beanUtils的使用

    1.         try {
                  BeanUtils.populate(user, request.getParameterMap());
                  
                  //调用业务逻辑
                  UserService us = new UserServiceImpl();
                  try {
                      us.register(user);
                  } catch (Exception e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              } catch (IllegalAccessException | InvocationTargetException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
             

       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值