| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| ca.spaz.sql.DBRow |
|
|
| 1 | /* |
|
| 2 | * Created on Apr 6, 2005 by davidson |
|
| 3 | */ |
|
| 4 | package ca.spaz.sql; |
|
| 5 | ||
| 6 | import java.lang.reflect.Field; |
|
| 7 | import java.sql.*; |
|
| 8 | ||
| 9 | /** |
|
| 10 | * Simple Java Object / Relational Database linker |
|
| 11 | * Uses reflection to map between simple objects and corresponding table rows. |
|
| 12 | * |
|
| 13 | * @author davidson |
|
| 14 | */ |
|
| 15 | 0 | public abstract class DBRow { |
| 16 | ||
| 17 | /** |
|
| 18 | * Attempts to load all fields in the object from a ResultSet row. |
|
| 19 | * @param row a row in a database that corresponds directly to the object |
|
| 20 | * @throws SQLException |
|
| 21 | * @throws IllegalArgumentException |
|
| 22 | * @throws IllegalAccessException |
|
| 23 | */ |
|
| 24 | public static void load(ResultSet row, Object obj) throws SQLException, IllegalArgumentException, IllegalAccessException { |
|
| 25 | 0 | int cols = row.getMetaData().getColumnCount(); |
| 26 | ||
| 27 | 0 | Field[] fields = obj.getClass().getFields(); |
| 28 | 0 | for (int i=0; i<fields.length; i++) { |
| 29 | 0 | String name = fields[i].getName(); |
| 30 | 0 | Class type = fields[i].getType(); |
| 31 | ||
| 32 | 0 | if (type == String.class) { |
| 33 | 0 | fields[i].set(obj, row.getObject(name)); |
| 34 | 0 | } else if (type == int.class) { |
| 35 | 0 | fields[i].setInt(obj, row.getInt(name)); |
| 36 | 0 | } else if (type == double.class) { |
| 37 | 0 | fields[i].setDouble(obj, row.getDouble(name)); |
| 38 | } |
|
| 39 | } |
|
| 40 | 0 | } |
| 41 | ||
| 42 | ||
| 43 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |