从数据库从取数据后自动封装为对象

Code:
  1. package test.ORMapping;  
  2.   
  3. import java.lang.reflect.InvocationTargetException;  
  4. import java.lang.reflect.Method;  
  5. import java.sql.ResultSet;  
  6. import java.sql.ResultSetMetaData;  
  7. import java.sql.SQLException;  
  8. import java.util.ArrayList;  
  9. import java.util.List;  
  10.   
  11. /** 
  12.  * 这是一个用来解决在查询过程 
  13.  * 那些自动设置 
  14.  * <pre> 
  15.  *  Eg1: 
  16.  *      ptmt.setString(1,vo.getString(xxx)); 
  17.  *      ptmt.setInt(2,vo.getInt(xxx)) 
  18.  *  Eg 2: 
  19.  *      ptmt.setName(rs.getString(xxx)); 
  20.  * </pre> 
  21.  * 用来解决这些设置时麻烦的事,动态自动实例化成对象, 
  22.  * 在下面的事例中Eg1没有给出. 
  23.  * <code> 
  24.  *      特别注明:在查询过程所要使用的sql语句有一定的限制: 
  25.  *      必需是如:select id as Id ,name as Name from test where id = 2; 
  26.  * </code> 
  27.  * @author xiangyuan 
  28.  * @version 1.0 
  29.  */  
  30. public class AutoMapping {  
  31.   
  32.     /** 
  33.      * 得到所有的表中字段名称 
  34.      * @param rs 
  35.      * @return 
  36.      * @throws SQLException  
  37.      */  
  38.     public String[] getColumns(ResultSet rs) throws SQLException {  
  39.         ResultSetMetaData rmeta = rs.getMetaData();  
  40.         int columns = rmeta.getColumnCount();  
  41.         String[] colNames = new String[columns];  
  42.         for(int index = 1;index < columns;index ++) {  
  43.             colNames[index - 1] = rmeta.getColumnLabel(index);  
  44.         }  
  45.         return colNames;  
  46.     }  
  47.       
  48.     /** 
  49.      * 根据传入的sql语句与类与封装对应的对象 
  50.      * @param rs 
  51.      * @param clazz 
  52.      * @return 
  53.      * @throws SQLException  
  54.      * @throws IllegalAccessException  
  55.      * @throws InstantiationException  
  56.      * @throws InvocationTargetException  
  57.      * @throws IllegalArgumentException  
  58.      */  
  59.     @SuppressWarnings("unchecked")  
  60.     public Object setObjectValues(ResultSet rs,Class clazz) throws SQLException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {  
  61.         String[] colNames = getColumns(rs);  
  62.         Object obj = null;  
  63.         Method[] methods = clazz.getMethods();  
  64.         if (rs.next()) {  
  65.             obj = clazz.newInstance();  
  66.             invokeMethod(obj,rs,colNames,methods);  
  67.         }  
  68.         return obj;  
  69.     }  
  70.       
  71.     /** 
  72.      * 得到一个集合对象 
  73.      * @param rs 
  74.      * @param clazz 
  75.      * @return 
  76.      * @throws SQLException 
  77.      * @throws InstantiationException 
  78.      * @throws IllegalAccessException 
  79.      * @throws IllegalArgumentException 
  80.      * @throws InvocationTargetException 
  81.      */  
  82.     @SuppressWarnings("unchecked")  
  83.     public List<Object> getAllObjects(ResultSet rs, Class clazz)  
  84.             throws SQLException, InstantiationException,  
  85.             IllegalAccessException, IllegalArgumentException, InvocationTargetException {  
  86.         String[] colNames = getColumns(rs);  
  87.         Object obj = null;  
  88.         Method[] methods = clazz.getMethods();  
  89.         List<Object> list = new ArrayList<Object>();  
  90.         while (rs.next()) {  
  91.             obj = clazz.newInstance();  
  92.             invokeMethod(obj,rs,colNames,methods);  
  93.             list.add(obj);  
  94.         }  
  95.         return list;  
  96.     }  
  97.       
  98.     /** 
  99.      * 动态加载值 
  100.      * @param colNames 
  101.      * @param methods 
  102.      * @throws SQLException  
  103.      * @throws InvocationTargetException  
  104.      * @throws IllegalAccessException  
  105.      * @throws IllegalArgumentException  
  106.      */  
  107.     private void invokeMethod(Object obj,ResultSet rs,String[] colNames,Method[] methods) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SQLException {  
  108.         for (int i = 0; i < colNames.length; i++) {  
  109.             String column = colNames[i];  
  110.             String method = "set" + column;  
  111.             for(Method m : methods) {  
  112.                 if (m.getName().equals(method)) {  
  113.                     m.invoke(obj,rs.getObject(column));  
  114.                     break;  
  115.                 }  
  116.             }  
  117.         }  
  118.     }  
  119. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python Scrapy是一种优秀的开源网络爬虫框架,可以用于从网页中爬取数据。借助其强大的功能,我们可以轻松地将爬取到的数据写入数据库。 首先,我们需要创建一个Scrapy项目并配置好爬虫。在项目中,我们可以定义Item类来表示我们需要提取的数据字段。通过编写爬虫规则,我们可以指定要爬取的网页、需要提取的数据字段以及数据的处理方式。 在编写完爬虫规则后,Scrapy会自动将爬取到的数据封装成Item对象。我们可以在爬虫的回调函数中对这些Item对象进行处理,例如将数据写入数据库。 为了将数据写入数据库,我们可以使用Python的数据库操作库,如MySQLdb或者pymysql。首先,我们需要连接到数据库,并创建一个数据库连接对象。然后,我们可以将爬取到的数据逐条插入到数据库中。 插入数据的具体步骤如下: 1. 导入数据库操作库 2. 连接到数据库 3. 创建游标对象 4. 遍历爬取到的数据 5. 构造插入语句 6. 执行插入操作 7. 提交事务 8. 关闭游标和数据库连接 通过以上步骤,我们可以将爬取到的数据成功写入数据库。 值得注意的是,在爬取大量数据时,为了提高性能和效率,我们可以使用异步IO库,如aiomysql或aiopg,来实现异步插入操作。 总而言之,Python Scrapy可以轻松实现数据的网页爬取,并通过数据库操作库将数据写入数据库。这样,我们可以方便地对爬取到的数据进行存储和管理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值