KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > teamkonzept > lib > TKDBTemplate


1 /*
2  * $Header: /cvsroot/webman-cms/source/webman/com/teamkonzept/lib/TKDBTemplate.java,v 1.7 2001/11/09 12:53:55 markus Exp $
3  *
4  */

5 package com.teamkonzept.lib;
6
7 import java.sql.*;
8
9 /**
10  *
11  * @author
12  * @version
13  */

14  public class TKDBTemplate {
15
16     /**
17      *
18      * @param resultSet
19      * @param templateName
20      */

21     public static boolean prepareTemplate(ResultSet resultSet, TKTemplate template)
22     {
23
24         boolean boolResultSet=false;
25         TKHashtable result = getHashFromResult(resultSet);
26
27                 if(result != null)
28                 {
29             template.set( result );
30             boolResultSet=true;
31                 }
32         else {
33             boolResultSet=false;
34         }
35         return boolResultSet;
36     }
37
38     public static boolean prepareTemplate(TKDBResultRow row, TKTemplate template)
39     {
40         TKHashtable result = getHashFromResult(row);
41         if (result != null)
42         {
43             template.set( result );
44             return true;
45         }
46         return false;
47     }
48
49     public static TKHashtable getHashFromResult(ResultSet resultSet)
50     {
51         TKHashtable result = null;
52         try
53         {
54              // Get the ResultSetMetaData. This will be used for
55
// the column headings
56
ResultSetMetaData rsmd = resultSet.getMetaData();
57
58              // Get the number of columns in the result set
59
int numCols = rsmd.getColumnCount();
60
61              if(resultSet.next())
62              {
63                 result = new TKHashtable();
64                   for (int x=1; x<=numCols; x++)
65                   {
66                     String JavaDoc str = resultSet.getString(x);
67                     if( str == null ) str = "";
68                     result.put( rsmd.getColumnLabel(x).toUpperCase(), str );
69                   }
70             }
71         }
72         catch (SQLException ex) {
73             TKDBLogger.logSQLException( ex );
74         }
75         return result;
76     }
77
78     public static TKHashtable getHashFromResult(TKDBResultRow row)
79     {
80         if( !row.hasResult() ) return null;
81
82         int numCols = row.getColumnCount();
83         TKHashtable result = new TKHashtable();
84
85         for( int i=0; i<numCols; i++ ) {
86             result.put( row.getColumnLabel(i).toUpperCase(), row.getColumn( i ) );
87         }
88         return result;
89     }
90
91
92     /**
93      *
94      *
95      * @param resultSet
96      * @param template
97      * @param listName
98      */

99     public static void prepareListTemplate( ResultSet resultSet, TKTemplate template, String JavaDoc listName )
100     {
101         TKResultSetIterator iterator = new TKResultSetIterator( resultSet, template.getListIterator(), listName );
102         template.setListIterator( iterator );
103     }
104
105     /**
106      *
107      *
108      * @param resultSet
109      * @param template
110      * @param listName
111      */

112     public static void prepareListTemplate( TKDBResult result, TKTemplate template, String JavaDoc listName )
113     {
114         TKDBResultIterator iterator = new TKDBResultIterator( result, template.getListIterator(), listName );
115         template.setListIterator( iterator );
116     }
117
118 }//end class
119

120
Popular Tags