Java tutorial
/** * Copyright (c) 2001-2012 "Redbasin Networks, INC" [http://redbasin.org] * * This file is part of Redbasin OpenDocShare community project. * * Redbasin OpenDocShare is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package dao; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types; import java.util.Vector; import javax.sql.DataSource; import model.Directory; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.jdbc.core.SqlParameter; // Certified to be thread-safe 2/19/2005 /** * This class is used by spring to populate a <code>Userpage</code> bean. * This class implements DirMemberAuthorQuery * * @author Smitha Gudur (smitha@redbasin.com) * @version $Revision: 1.1 $ */ class DirMemberAuthorQuery extends BasicQuery { // TODO: volatile or use our own connection private volatile Vector columnNames = null; protected final Log logger = LogFactory.getLog(getClass()); /** * This constructor is called when the collabrum messages method makes a * call to query.execute(). After the query is executed, the result set is * returned. For each row in the result set, the mapRow method is called by * Spring. In the very first call to mapRow() for the first row in the result * set, we make a call to RSMD to get columnNames and cache * them to a local array in this object. This way, we can avoid multiple calls * to RSMD since, spring calls mapRow many times (one per row in result set). * */ // dont use collmsgattr.rid as it may have null values and clash with rid of collmessages. DirMemberAuthorQuery(DataSource ds) { super(ds, "select c1.entryid from diradmin c1, diradmin c2 " + "where c1.directoryid=c2.directoryid and c1.ownerid=? " + "and c2.ownerid=? limit 1"); declareParameter(new SqlParameter("ownerid", Types.BIGINT)); declareParameter(new SqlParameter("ownerid", Types.BIGINT)); compile(); } /** * Spring calls this method for each record found in the resultset. * @param rs - result set * @param rowNum - number of rows * @returns Object * @throws SQLException */ protected Object mapRow(ResultSet rs, int rowNum) throws SQLException { if (rowNum == 0) { // get column names and types from rsmd and save to local array in object if (columnNames == null) { columnNames = dbutils.getColumnNames(rs); } } Directory dir = (Directory) eop.newObject("directory"); for (int j = 0; j < columnNames.size(); j++) { dir.setValue((String) columnNames.elementAt(j), (String) rs.getString((String) columnNames.elementAt(j))); } return dir; // we return becos spring requires it. } }