这是实际使用中的项目

源代码在线查看: dao.java~4~

软件大小: 6467 K
上传用户: sheng_xia
关键词: 项目
下载地址: 免注册下载 普通下载 VIP

相关代码

				//---------------------------------------------------------
				// Application: equipment of  System
				// Author     : eSingle
				// File       : DAO.java
				//
				// Copyright 2002 LandSoft Corp.
				// Generated at Mon Nov 18 20:13:57 CST 2002
				// Created by caoguangxin
				// mailto:gxcao@mail.tsinghua.edu.cn
				//---------------------------------------------------------
				
				package com.landsoft.equipment.dao;
				
				import java.io.*;
				import java.sql.*;
				import java.util.*;
				import javax.sql.*;
				
				import java.lang.IllegalAccessException;
				import java.lang.reflect.InvocationTargetException;
				
				import org.apache.commons.beanutils.BeanUtils;
				
				public class DAO {
				
				  protected DataSource ds;
				
				  protected void populate(Object bean, ResultSet rs) throws SQLException {
				    ResultSetMetaData metaData = rs.getMetaData();
				    int ncolumns = metaData.getColumnCount();
				    String tmp;
				
				    HashMap properties = new HashMap();
				    // Scroll to next record and pump into hashmap
				    for (int i=1; i				      tmp = rs.getString(i);
				      properties.put(sql2javaName(metaData.getColumnName(i)), tmp);
				    }
				    // Set the corresponding properties of our bean
				    try {
				      BeanUtils.populate(bean, properties);
				    } catch (InvocationTargetException ite) {
				      throw new SQLException("BeanUtils.populate threw " + ite.toString());
				    } catch (IllegalAccessException iae) {
				      throw new SQLException("BeanUtils.populate threw " + iae.toString());
				    }
				  }
				
				  public int getSize(String tableName, String condition) throws SQLException {
				    Connection conn = null;
				    PreparedStatement pstmt = null;
				    ResultSet rs = null;
				    try {
				      String sql = "SELECT count(*) FROM "+tableName+" "+condition;
				      conn = ds.getConnection();
				      pstmt = conn.prepareStatement(sql);
				      rs = pstmt.executeQuery();
				      rs.next();
				      int size = rs.getInt(1);
				      close(rs);
				      close(pstmt);
				      return size;
				    } catch (SQLException sqle) {
				      close(rs);
				      close(pstmt);
				      rollback(conn);
				      sqle.printStackTrace();
				      throw sqle;
				    } finally {
				    	close(conn);
				    }
				  }
				
				  public DAO(DataSource ds) {
				  	this.ds = ds;
				  }
				
				  public void setDataSource(DataSource ds) {
				    this.ds = ds;
				  }
				
				  protected void close(ResultSet rs) {
				    if (rs != null) {
				    	try {
				    		rs.close();
				    	} catch (SQLException e) {
				    	}
				    	rs = null;
				    }
				  }
				
				  protected void close(PreparedStatement pstmt) {
				    if (pstmt != null) {
				    	try {
				    		pstmt.close();
				    	} catch (SQLException e) {
				    	}
				    	pstmt = null;
				    }
				  }
				
				  protected void close(Connection conn) {
				    if (conn != null) {
				    	try {
				    		conn.close();
				    	} catch (SQLException e) {
				    		e.printStackTrace();
				    	}
				    	conn = null;
				    }
				  }
				
				  protected void rollback(Connection conn) {
				    if (conn != null) {
				    	try {
				    		conn.rollback();
				    	} catch (SQLException e) {
				    		e.printStackTrace();
				    	}
				    	conn = null;
				    }
				  }
				
				  protected static String java2sqlName(String name) {
				    String column = "";
				    for (int i = 0; i < name.length(); i++) {
				      if (i < name.length() - 1 && (name.charAt(i) >= 'a' && name.charAt(i) 				          (name.charAt(i + 1) >= 'A' && name.charAt(i + 1) 				        column += name.charAt(i) + "_";
				      } else {
				        column += name.charAt(i);
				      }
				    }
				    return column.toLowerCase();
				  }
				
				  protected static String sql2javaName(String name) {
				    String column = "";
				    for (int i = 0; i < name.length(); i++) {
				      if (name.charAt(i) == '_') {
				        column += ++i				      } else {
				        column += name.charAt(i);
				      }
				    }
				    return column;
				  }
				
				
				}
							

相关资源