Use Mapped Object after database Connection close : Database Utils : Apache Common : Java examples (example source code) Organized by topic

Java
C++
PHP
Java Home »  Apache Common   » [  Database Utils  ]  Screenshots 
 



Use Mapped Object after database Connection close



import org.apache.commons.dbcp.BasicDataSource;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.RowSetDynaClass;

import java.util.Iterator;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.PreparedStatement;

public class DynaBeansExampleV3 {
  public static void main(String args[]) throws Exception {

    Connection conn = getConnection();
    PreparedStatement ps =
      conn.prepareStatement(
        "SELECT * from movie, person " +
        "WHERE movie.director = person.Id");
    ResultSet rs = ps.executeQuery();

    RowSetDynaClass rsdc = new RowSetDynaClass(rs);

    conn.close();

    Iterator itr = rsdc.getRows().iterator();
    while(itr.hasNext()) {
      DynaBean bean = (DynaBean)itr.next();
      System.err.println(bean.get("title"));
    }


  }

  private static Connection getConnection() throws Exception {
    BasicDataSource bds = new BasicDataSource();
    bds.setDriverClassName("com.mysql.jdbc.Driver");
    bds.setUrl("jdbc:mysql://localhost/commons");
    bds.setUsername("root");
    bds.setPassword("");

//    bds.setInitialSize(5);

    return bds.getConnection();
  }
}
           
       
Download: BeanUtilsDynaBeansExampleV3.zip   ( 1,005  K )  
Related examples in the same category
1.  Map ResultSet to Object
2.  Clone ObjectHas Download File
























Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.