Topic: java中,向数据库的两个表中插入数据有没有更好的方法?

  Print this page

1.java中,向数据库的两个表中插入数据有没有更好的方法? Copy to clipboard
Posted by: mengxiangjava
Posted on: 2008-09-11 13:06

我用得比较笨的方法是:
public int InsertProductData(ProductBean product)
  {
    logger.debug("call InsertProductData method......");
    String sql = "insert into t_product_info(product_name,body_info,num,province,city,area,product_type,product_property,img_01) values(?,?,?,?,?,?,?,?,?)";
    String sql2 = "insert into t_product(user_id,start_date,end_date) values(?,?,?)";
    logger.debug("_____sql_____:"+sql);        
      
    int rows=0;
    Connection conn = null;    
    PreparedStatement pstmt = null;  
    PreparedStatement pstmt2 = null;  
    try
    {
      conn=Pool.getConnection();
      //插入表t_product信息
      pstmt2 = conn.prepareStatement(sql2);
      pstmt2.setString(1,product.getUser_id());
      pstmt2.setString(2, product.getStart_date());
      pstmt2.setString(3, product.getEnd_date());
      pstmt2.executeUpdate();
      //插入表t_product_info信息
      pstmt=conn.prepareStatement(sql);
      pstmt.setString(1, product.getProduct_name());      
      pstmt.setString(2, product.getBody_info());
      pstmt.setString(3, product.getNum());
      pstmt.setString(4, product.getProvince());
      pstmt.setString(5, product.getCity());
      pstmt.setString(6, product.getArea());
      pstmt.setString(7, product.getProduct_type());
      pstmt.setString(8, product.getProduct_property());
      pstmt.setString(9, product.getImg_01());
      rows = pstmt.executeUpdate();              
    }
    catch(SQLException sqle)
    {
      logger.debug("Insert t_product table of data",sqle);
    }
    finally
    {
      try
      {        
        if (pstmt != null ) pstmt.close();
        if (conn != null ) conn.close();
      }
      catch (SQLException sqle){}    
    }
    return rows;
  }

2.Re:java中,向数据库的两个表中插入数据有没有更好的方法? [Re: mengxiangjava] Copy to clipboard
Posted by: scottding
Posted on: 2008-09-11 13:29

尝试一些ORM框架把,比如iBatis,Hibernate,或者Apache的OJB。


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923