You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by dg...@apache.org on 2003/10/18 21:10:03 UTC
cvs commit: jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils ScalarHandler.java
dgraham 2003/10/18 12:10:03
Modified: dbutils/src/java/org/apache/commons/dbutils
ScalarHandler.java
Log:
Added constructors to allow configuration of the column number
or name to lookup instead of always returning the first column.
Revision Changes Path
1.7 +56 -8 jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/ScalarHandler.java
Index: ScalarHandler.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/ScalarHandler.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- ScalarHandler.java 16 Oct 2003 03:14:38 -0000 1.6
+++ ScalarHandler.java 18 Oct 2003 19:10:03 -0000 1.7
@@ -65,7 +65,7 @@
import java.sql.SQLException;
/**
- * <code>ResultSetHandler</code> implementation that converts the first
+ * <code>ResultSetHandler</code> implementation that converts one
* <code>ResultSet</code> column into an Object.
*
* @see ResultSetHandler
@@ -75,12 +75,51 @@
public class ScalarHandler implements ResultSetHandler {
/**
- * Returns the first <code>ResultSet</code> column as an object via the
+ * The column number to retrieve.
+ */
+ private int columnIndex = 1;
+
+ /**
+ * The column name to retrieve. Either columnName or columnIndex
+ * will be used but never both.
+ */
+ private String columnName = null;
+
+ /**
+ * Creates a new instance of ScalarHandler. The first column will
+ * be returned from <code>handle()</code>.
+ */
+ public ScalarHandler() {
+ super();
+ }
+
+ /**
+ * Creates a new instance of ScalarHandler.
+ *
+ * @param columnIndex The index of the column to retrieve from the
+ * <code>ResultSet</code>.
+ */
+ public ScalarHandler(int columnIndex) {
+ this.columnIndex = columnIndex;
+ }
+
+ /**
+ * Creates a new instance of ScalarHandler.
+ *
+ * @param columnName The name of the column to retrieve from the
+ * <code>ResultSet</code>.
+ */
+ public ScalarHandler(String columnName) {
+ this.columnName = columnName;
+ }
+
+ /**
+ * Returns one <code>ResultSet</code> column as an object via the
* <code>ResultSet.getObject()</code> method that performs type
* conversions.
*
- * @return The first column or <code>null</code> if there are no rows in
- * the <code>ResultSet</code>
+ * @return The column or <code>null</code> if there are no rows in
+ * the <code>ResultSet</code>.
*
* @throws SQLException
*
@@ -89,6 +128,15 @@
public Object handle(ResultSet rs, Object[] params, Object userObject)
throws SQLException {
- return rs.next() ? rs.getObject(1) : null;
+ if (rs.next()) {
+ if (this.columnName == null) {
+ return rs.getObject(this.columnIndex);
+ } else {
+ return rs.getObject(this.columnName);
+ }
+
+ } else {
+ return null;
+ }
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org