日期:2014-05-17 浏览次数:20658 次
/** * 返回查询结果集 * * @param SQL语句 * @param 附带参数 * @return 返回结果,用ArrayList包装Object数组 * @throws SQLException * * @example ArrayList list = * mysqlhelper.ExecuteReader("Select * from Documents",null); <br/> * for(int i = 0;i<list.size();i++) { <br/> Object[] * obs = (Object[])list.get(i); <br/> for(int j = * 0;j<obs.length;j++) { <br/> * out.print(obs[j].toString()); <br/> * } <br/> out.print("<br/>"); <br/> * } <br/> * */ public static ArrayList ExecuteReader(String cmdtext, Object[] parms) throws SQLException { PreparedStatement pstmt = null; Connection conn = null; ResultSet rs = null; try { conn = getConnection(); pstmt = conn.prepareStatement(cmdtext); prepareCommand(pstmt, parms); rs = pstmt.executeQuery(); ArrayList al = new ArrayList(); ResultSetMetaData rsmd = rs.getMetaData(); int column = rsmd.getColumnCount(); while (rs.next()) { Object[] ob = new Object[column]; for (int i = 1; i <= column; i++) { ob[i - 1] = rs.getObject(i); } al.add(ob); } return al; } catch (Exception ex) { } finally{ if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } return null; }
package com.mjrg.mjb2c.model.dao.impl; import com.mjrg.mjb2c.model.bean.Admin; public interface IAdminDao { public abstract Admin SelectByName(String aName); }
package com.mjrg.mjb2c.model.dao; import java.sql.SQLException; import java.util.ArrayList; import com.mjrg.mjb2c.db.MySqlHelper; import com.mjrg.mjb2c.model.bean.Admin; import com.mjrg.mjb2c.model.dao.impl.IAdminDao; public class AdminDao implements IAdminDao { /* (non-Javadoc) * @see com.mjrg.mjb2c.model.dao.IAdminDao#SelectByName(java.lang.String) */ public Admin SelectByName(String aName){ Admin admin=null; String sql = "SELECT * FROM admin WHERE aName=?"; String[] parms={aName}; try { ArrayList adLst = MySqlHelper.ExecuteReader(sql,parms); if(adLst.size()>0){ //空指针异常指向这句 admin=(Admin)adLst.get(0); } } catch (SQLException e) { // TODO Auto-generated catch block e.printS