1
package
com.onlysoft.txl;
2
3 /** */ /**
4 * <p>Title: </p>
5 *
6 * <p>Description: </p>
7 *
8 * <p>Copyright: Copyright (c) 2006</p>
9 *
10 * <p>Company: fishsoft</p>
11 *
12 * @author Danny
13 * @version 1.0
14 */
15
16 import org.apache.struts.action.Action;
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpSession;
19 import java.sql. * ;
20 import java.util. * ;
21 import com.onlysoft.txl.util. * ;
22 import javax.sql.DataSource;
23 import org.apache.log4j.Logger;
24
25 public class BaseAction extends Action {
26 public Connection conn = null;
27 public String userid = "";
28 public String username = "";
29 public String loginpwd = "";
30 public String realname = "";
31 public String mphone = "";
32 public boolean isLogin = false;
33 public DBUtil db;
34 public HttpSession session;
35 protected static Logger log = Logger.getLogger(BaseAction.class);
36
37 public void init(HttpServletRequest request) {
38
39 session = request.getSession();
40 HashMap loginInfo = session.getAttribute("loginInfo")==null?null:(HashMap) session.getAttribute("loginInfo");
41 if (loginInfo != null && !loginInfo.isEmpty()) {
42 isLogin = true;
43 userid = (String) loginInfo.get("userid");
44 username = (String) loginInfo.get("username");
45 realname = (String) loginInfo.get("realname");
46 mphone = (String) loginInfo.get("mphone");
47 loginpwd = (String) loginInfo.get("loginpwd");
48 }
49 db = new DBUtil();
50 //log.info("test");
51 try {
52 DataSource dataSource = getDataSource(request, "dataSource");
53 conn = dataSource.getConnection();
54 } catch (Exception ioe) {
55 ioe.printStackTrace();
56 }
57 }
58
59 public Integer saveHelperMore(String tableName, String sOpType,
60 ObjectBean oPara, ObjectBean oWhere
61 ) throws Exception {
62 Integer iSize = new Integer(0);
63 try {
64 //System.out.println("sOpType==========:" + sOpType);
65 if (sOpType.equals("insert")) {
66 //System.out.println("oPara=========:" + oPara);
67 iSize = db.insOB(tableName, oPara, conn);
68 /**//*if (iSize == null || iSize.intValue() == 0) {
69 //this.toErrorInfo("表" + tableName + "保存失败!");
70 return;
71 }*/
72 } else if (sOpType.equals("update")) {
73 iSize = db.updOB(tableName, oPara, oWhere, conn);
74 } else if (sOpType.equals("delete")) {
75 iSize = db.delOB(tableName, oWhere, conn);
76 }
77 } catch (SQLException e) {
78 //this.toErrorInfo("数据保存失败");
79 //return;
80 }
81 return iSize;
82 }
83
84 public void saveHelperMore(String count,
85 String tableName, String sOpType,
86 ObjectBean oWhere,
87 HttpServletRequest request) throws
88 Exception {
89 Integer iSize;
90 try {
91 for (int i = 0; i < Integer.parseInt(count); i++) {
92 ObjectBean para = getPara(getTableColume(tableName),
93 Integer.toString(i), request);
94 if (sOpType.equals("insert")) {
95 iSize = db.insOB(tableName, para, conn);
96 if (iSize == null || iSize.intValue() == 0) {
97 //this.toErrorInfo("表" + tableName + "保存失败!");
98 return;
99 }
100 } else if (sOpType.equals("update")) {
101 db.updOB(tableName, para, oWhere, conn);
102 } else if (sOpType.equals("delete")) {
103 db.delOB(tableName, oWhere, conn);
104 }
105 }
106 } catch (SQLException e) {
107 //this.toErrorInfo("数据保存失败");
108 return;
109 }
110 }
111
112 protected ObjectBean getPara(HashMap mp, String suffix,
113 HttpServletRequest request) {
114 if (mp == null || mp.isEmpty()) {
115 return null;
116 }
117 ObjectBean para = new ObjectBean();
118 Vector data1 = mp.get("STRING") == null ? null :
119 (Vector) mp.get("STRING");
120 Vector data2 = mp.get("NOSTRING") == null ? null :
121 (Vector) mp.get("NOSTRING");
122 int count = 0;
123 if (data1 != null && !data1.isEmpty()) {
124 for (int i = 0; i < data1.size(); i++) {
125 if (request.getParameter(data1.elementAt(i) + suffix) != null) {
126 para.set(data1.elementAt(i),
127 request.getParameter(data1.elementAt(i) + suffix));
128 count++;
129 }
130 }
131 }
132 if (data2 != null && !data2.isEmpty()) {
133 for (int i = 0; i < data2.size(); i++) {
134 Object xxx = request.getParameter(data2.elementAt(i) + suffix);
135 if (xxx != null && !xxx.equals("")) {
136 para.set(data2.elementAt(i),
137 new StringBuffer("to_date('" +
138 xxx +
139 "','yyyy-mm-dd hh24:mi:ss')"));
140 count++;
141 }
142 }
143 }
144 if (count == 0) {
145 para = null;
146 }
147 return para;
148 }
149
150 protected HashMap getTableColume(String tableName) throws
151 SQLException {
152 HashMap para = new HashMap();
153 Vector para1 = new Vector(), para2 = new Vector();
154 ResultSetMetaData rs = null;
155 try {
156 rs = conn.createStatement().executeQuery(
157 "select * from " + tableName + " where 1=2 ").
158 getMetaData();
159 int r = rs.getColumnCount();
160 for (int i = 1; i < r + 1; i++) {
161 //System.out.println(rs.getColumnType(i));
162 if (rs.getColumnType(i) != 91) { //date
163 para1.add(rs.getColumnName(i));
164 } else {
165 para2.add(rs.getColumnName(i));
166 }
167 }
168 para.put("STRING", para1);
169 para.put("NOSTRING", para2);
170 } catch (SQLException e) {
171 e.printStackTrace();
172 throw e;
173 } finally {
174 }
175 return para;
176 }
177
178 protected String getNextID(String sFieldName, String sTableName) throws
179 Exception {
180 String sID = "";
181 try {
182 ResultSet rs = null;
183 String sSql = "select max(" + sFieldName + ")+1 as id from " + sTableName +
184 "";
185 //System.out.println("getNextID====sSql========:" + sSql);
186 rs = (ResultSet) db.doSql(sSql, db.QUERY_MODE, conn);
187 if (rs.next()) {
188 sID = rs.getString(1);
189 if (sID == null) {
190 sID = "1";
191 }
192 }
193 rs.close();
194 } catch (Exception ioe) {
195 ioe.printStackTrace();
196 }
197 return sID;
198 }
199
200 protected void destroy() {
201 try {
202 if (conn != null) {
203 conn.close();
204 conn = null;
205 }
206 } catch (Exception e) {
207 e.printStackTrace();
208 }
209 }
210
211
212
213
214
215
216
217
218
219
220}
221
数据库操作的!不多说!实在的代码!
2
3 /** */ /**
4 * <p>Title: </p>
5 *
6 * <p>Description: </p>
7 *
8 * <p>Copyright: Copyright (c) 2006</p>
9 *
10 * <p>Company: fishsoft</p>
11 *
12 * @author Danny
13 * @version 1.0
14 */
15
16 import org.apache.struts.action.Action;
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpSession;
19 import java.sql. * ;
20 import java.util. * ;
21 import com.onlysoft.txl.util. * ;
22 import javax.sql.DataSource;
23 import org.apache.log4j.Logger;
24
25 public class BaseAction extends Action {
26 public Connection conn = null;
27 public String userid = "";
28 public String username = "";
29 public String loginpwd = "";
30 public String realname = "";
31 public String mphone = "";
32 public boolean isLogin = false;
33 public DBUtil db;
34 public HttpSession session;
35 protected static Logger log = Logger.getLogger(BaseAction.class);
36
37 public void init(HttpServletRequest request) {
38
39 session = request.getSession();
40 HashMap loginInfo = session.getAttribute("loginInfo")==null?null:(HashMap) session.getAttribute("loginInfo");
41 if (loginInfo != null && !loginInfo.isEmpty()) {
42 isLogin = true;
43 userid = (String) loginInfo.get("userid");
44 username = (String) loginInfo.get("username");
45 realname = (String) loginInfo.get("realname");
46 mphone = (String) loginInfo.get("mphone");
47 loginpwd = (String) loginInfo.get("loginpwd");
48 }
49 db = new DBUtil();
50 //log.info("test");
51 try {
52 DataSource dataSource = getDataSource(request, "dataSource");
53 conn = dataSource.getConnection();
54 } catch (Exception ioe) {
55 ioe.printStackTrace();
56 }
57 }
58
59 public Integer saveHelperMore(String tableName, String sOpType,
60 ObjectBean oPara, ObjectBean oWhere
61 ) throws Exception {
62 Integer iSize = new Integer(0);
63 try {
64 //System.out.println("sOpType==========:" + sOpType);
65 if (sOpType.equals("insert")) {
66 //System.out.println("oPara=========:" + oPara);
67 iSize = db.insOB(tableName, oPara, conn);
68 /**//*if (iSize == null || iSize.intValue() == 0) {
69 //this.toErrorInfo("表" + tableName + "保存失败!");
70 return;
71 }*/
72 } else if (sOpType.equals("update")) {
73 iSize = db.updOB(tableName, oPara, oWhere, conn);
74 } else if (sOpType.equals("delete")) {
75 iSize = db.delOB(tableName, oWhere, conn);
76 }
77 } catch (SQLException e) {
78 //this.toErrorInfo("数据保存失败");
79 //return;
80 }
81 return iSize;
82 }
83
84 public void saveHelperMore(String count,
85 String tableName, String sOpType,
86 ObjectBean oWhere,
87 HttpServletRequest request) throws
88 Exception {
89 Integer iSize;
90 try {
91 for (int i = 0; i < Integer.parseInt(count); i++) {
92 ObjectBean para = getPara(getTableColume(tableName),
93 Integer.toString(i), request);
94 if (sOpType.equals("insert")) {
95 iSize = db.insOB(tableName, para, conn);
96 if (iSize == null || iSize.intValue() == 0) {
97 //this.toErrorInfo("表" + tableName + "保存失败!");
98 return;
99 }
100 } else if (sOpType.equals("update")) {
101 db.updOB(tableName, para, oWhere, conn);
102 } else if (sOpType.equals("delete")) {
103 db.delOB(tableName, oWhere, conn);
104 }
105 }
106 } catch (SQLException e) {
107 //this.toErrorInfo("数据保存失败");
108 return;
109 }
110 }
111
112 protected ObjectBean getPara(HashMap mp, String suffix,
113 HttpServletRequest request) {
114 if (mp == null || mp.isEmpty()) {
115 return null;
116 }
117 ObjectBean para = new ObjectBean();
118 Vector data1 = mp.get("STRING") == null ? null :
119 (Vector) mp.get("STRING");
120 Vector data2 = mp.get("NOSTRING") == null ? null :
121 (Vector) mp.get("NOSTRING");
122 int count = 0;
123 if (data1 != null && !data1.isEmpty()) {
124 for (int i = 0; i < data1.size(); i++) {
125 if (request.getParameter(data1.elementAt(i) + suffix) != null) {
126 para.set(data1.elementAt(i),
127 request.getParameter(data1.elementAt(i) + suffix));
128 count++;
129 }
130 }
131 }
132 if (data2 != null && !data2.isEmpty()) {
133 for (int i = 0; i < data2.size(); i++) {
134 Object xxx = request.getParameter(data2.elementAt(i) + suffix);
135 if (xxx != null && !xxx.equals("")) {
136 para.set(data2.elementAt(i),
137 new StringBuffer("to_date('" +
138 xxx +
139 "','yyyy-mm-dd hh24:mi:ss')"));
140 count++;
141 }
142 }
143 }
144 if (count == 0) {
145 para = null;
146 }
147 return para;
148 }
149
150 protected HashMap getTableColume(String tableName) throws
151 SQLException {
152 HashMap para = new HashMap();
153 Vector para1 = new Vector(), para2 = new Vector();
154 ResultSetMetaData rs = null;
155 try {
156 rs = conn.createStatement().executeQuery(
157 "select * from " + tableName + " where 1=2 ").
158 getMetaData();
159 int r = rs.getColumnCount();
160 for (int i = 1; i < r + 1; i++) {
161 //System.out.println(rs.getColumnType(i));
162 if (rs.getColumnType(i) != 91) { //date
163 para1.add(rs.getColumnName(i));
164 } else {
165 para2.add(rs.getColumnName(i));
166 }
167 }
168 para.put("STRING", para1);
169 para.put("NOSTRING", para2);
170 } catch (SQLException e) {
171 e.printStackTrace();
172 throw e;
173 } finally {
174 }
175 return para;
176 }
177
178 protected String getNextID(String sFieldName, String sTableName) throws
179 Exception {
180 String sID = "";
181 try {
182 ResultSet rs = null;
183 String sSql = "select max(" + sFieldName + ")+1 as id from " + sTableName +
184 "";
185 //System.out.println("getNextID====sSql========:" + sSql);
186 rs = (ResultSet) db.doSql(sSql, db.QUERY_MODE, conn);
187 if (rs.next()) {
188 sID = rs.getString(1);
189 if (sID == null) {
190 sID = "1";
191 }
192 }
193 rs.close();
194 } catch (Exception ioe) {
195 ioe.printStackTrace();
196 }
197 return sID;
198 }
199
200 protected void destroy() {
201 try {
202 if (conn != null) {
203 conn.close();
204 conn = null;
205 }
206 } catch (Exception e) {
207 e.printStackTrace();
208 }
209 }
210
211
212
213
214
215
216
217
218
219
220}
221
1
package
com.onlysoft.txl.util;
2
3 /** */ /**
4 * <p>Title: </p>
5 *
6 * <p>Description: </p>
7 *
8 * <p>Copyright: Copyright (c) 2006</p>
9 *
10 * <p>Company: fishsoft</p>
11 *
12 * @author Danny
13 * @version 1.0
14 */
15 import java.sql. * ;
16 import javax.sql. * ;
17 import java.util. * ;
18 import java.io. * ;
19 import java.math.BigDecimal;
20
21 public class DBUtil {
22
23 public final static int INSERT_MODE = 0;
24 public final static int UPDATE_MODE = 1;
25 public final static int DELETE_MODE = 2;
26 public final static int QUERY_MODE = 3;
27 public final static int MODIFY_MODE = 4;
28
29 private static final String sSeperator = ";";
30 private static final String sDatePattern = "yyyy/MM/dd";
31 private static final String sDateTimePattern = "yyyy/MM/dd hh:mm:ss S";
32 private String sTp = "java.math.BigDecimal;java.lang.Integer;java.lang.Double;java.lang.Float;java.lang.StringBuffer;";
33
34 private DataSource oDs;
35// private Connection oCon;
36 boolean DEBUG = true;
37 private Statement oSt = null;
38
39 public DBUtil() {
40
41 }
42
43 private Integer modify(String s_sql, Connection oCont) throws Exception {
44 Integer iRs = null;
45 if (oSt != null) {
46 oSt.close();
47 oSt = null;
48 }
49 try {
50 oSt = oCont.createStatement();
51 iRs = new Integer(oSt.executeUpdate(s_sql));
52 } catch (SQLException sqle) {
53 throw sqle;
54 }
55 return iRs;
56 }
57
58 private ResultSet query(String s_sql, Connection oCont) throws Exception {
59 ResultSet oRs = null;
60 if (oSt != null) {
61 oSt.close();
62 oSt = null;
63 }
64 try {
65 oSt = oCont.createStatement();
66 oRs = oSt.executeQuery(s_sql);
67 } catch (SQLException sqle) {
68 throw sqle;
69 }
70 return oRs;
71 }
72
73 public Object doSql(String sSql, int i_mode, Connection oCont) throws
74 Exception {
75 try {
76 switch (i_mode) {
77 case INSERT_MODE:
78 case UPDATE_MODE:
79 case DELETE_MODE:
80 return modify(sSql, oCont);
81 case QUERY_MODE:
82 return query(sSql, oCont);
83 }
84 } catch (Exception e) {
85 throw e;
86 }
87 return null;
88 }
89
90 public Integer insOB(String tableName, ObjectBean res, Connection oCont) throws
91 Exception {
92// res.canDB();
93 try {
94 String sql = "";
95 sql = "INSERT INTO " + tableName + " ( ";
96 Enumeration en = res.keys();
97 boolean f = false;
98 String key = "", val = "";
99 while (en.hasMoreElements()) {
100 if (f) {
101 key += ", ";
102 val += ", ";
103 } else {
104 f = true;
105 }
106 String k = (String) en.nextElement();
107 key += k;
108 String tp = res.get(k).getClass().getName();
109 if (this.sTp.indexOf(tp + ";") >= 0) {
110 val += "" + res.get(k) + "";
111 } else {
112 String temp = res.get(k)+"";
113 if (temp.equals("getdate()"))
114 val += "" + temp + "";
115 else
116 val += "'" + temp + "'";
117 }
118 }
119 sql += key + ") VALUES (" + val + ")";
120
121 //System.out.println("real sql:"+sql);
122
123 return (Integer)this.doSql(sql, this.INSERT_MODE, oCont);
124 } catch (Exception e) {
125 e.printStackTrace();
126 throw e;
127 }
128 }
129
130 public Integer updOB(String tableName, ObjectBean res, ObjectBean where,
131 Connection oCont) throws Exception {
132// res.canDB();
133 try {
134 String sql = "";
135 sql = "UPDATE " + tableName + " SET ";
136 Enumeration en = res.keys();
137 boolean f = false;
138 while (en.hasMoreElements()) {
139 if (f) {
140 sql += ", ";
141 } else {
142 f = true;
143 }
144 String k = (String) en.nextElement();
145 String tp = res.get(k).getClass().getName();
146 if (this.sTp.indexOf(tp + ";") >= 0) {
147 sql += k + "=" + "" + res.get(k) + "";
148 } else {
149 sql += k + "=" + "'" + res.get(k) + "'";
150 }
151 }
152 sql += " WHERE ";
153
154 en = where.keys();
155 f = false;
156 while (en.hasMoreElements()) {
157 if (f) {
158 sql += "and ";
159 } else {
160 f = true;
161 }
162 String k = (String) en.nextElement();
163 String tp = where.get(k).getClass().getName();
164 if (this.sTp.indexOf(tp + ";") >= 0) {
165 sql += k + "=" + "" + where.get(k) + "";
166 } else {
167 sql += k + "=" + "'" + where.get(k) + "'";
168 }
169 }
170
171 return (Integer)this.doSql(sql, this.UPDATE_MODE, oCont);
172 } catch (Exception e) {
173 e.printStackTrace();
174 throw e;
175 }
176 }
177
178 public Integer delOB(String tableName, ObjectBean where, Connection oCont) throws
179 Exception {
180 try {
181 String sql = "";
182 sql = "DELETE FROM " + tableName + " WHERE ";
183
184 Enumeration en = where.keys();
185 boolean f = false;
186 while (en.hasMoreElements()) {
187 if (f) {
188 sql += "and ";
189 } else {
190 f = true;
191 }
192 String k = (String) en.nextElement();
193 String tp = where.get(k).getClass().getName();
194 if (this.sTp.indexOf(tp + ";") >= 0) {
195 sql += k + "=" + "" + where.get(k) + "";
196 } else {
197 sql += k + "=" + "'" + where.get(k) + "'";
198 }
199 }
200
201 return (Integer)this.doSql(sql, this.DELETE_MODE, oCont);
202 } catch (Exception e) {
203 e.printStackTrace();
204 throw e;
205 }
206 }
207
208 public ResultSet queOB(String tableName, String res, ObjectBean where,
209 Connection oCont) throws Exception {
210 try {
211 String sql = "";
212 sql = "SELECT " + res + " FROM " + tableName + " where ";
213
214 Enumeration en = where.keys();
215 boolean f = false;
216 while (en.hasMoreElements()) {
217 if (f) {
218 sql += "and ";
219 } else {
220 f = true;
221 }
222 String k = (String) en.nextElement();
223 String tp = where.get(k).getClass().getName();
224 if (this.sTp.indexOf(tp + ";") >= 0) {
225 sql += k + "=" + "" + where.get(k) + "";
226 } else {
227 sql += k + "=" + "'" + where.get(k) + "'";
228 }
229 }
230
231 return (ResultSet)this.doSql(sql, this.QUERY_MODE, oCont);
232 } catch (Exception e) {
233 e.printStackTrace();
234 throw e;
235 }
236 }
237
238 public static Vector getHashMapVByRSDec(ResultSet rs) throws Exception {
239 Vector resV = new Vector();
240 try {
241 if (rs != null) {
242 int count = 0;
243 Vector vColumnName = new Vector();
244 Vector vGetByType = new Vector();
245 while (rs.next()) {
246 ObjectBean ob = new ObjectBean();
247 if (count == 0) {
248 ResultSetMetaData rsm = rs.getMetaData();
249 count = rsm.getColumnCount();
250 for (int i = 1; i < count + 1; i++) {
251 Object oCn = rsm.getColumnName(i);
252 String sSt = rsm.getColumnTypeName(i);
253 vColumnName.addElement(oCn);
254 vGetByType.addElement(sSt);
255 ob.set(oCn, sqlGetByTypeDec(sSt, i, rs));
256 }
257 } else {
258 for (int i = 1; i < count + 1; i++) {
259 ob.set(vColumnName.elementAt(i - 1),
260 sqlGetByTypeDec( (String) vGetByType.elementAt(i -
261 1),
262 i, rs));
263 }
264 }
265 resV.addElement(ob);
266 }
267 }
268 } catch (Exception e) {
269 e.printStackTrace();
270 throw e;
271 } finally {
272 try {
273 if (rs != null) {
274 rs.close();
275 }
276 } catch (Exception e) {
277 e.printStackTrace();
278 throw e;
279 }
280 }
281 return resV;
282 }
283
284 public static HashMap getHashMapByRS(ResultSet rs) throws Exception {
285 HashMap resH = new HashMap();
286 try {
287 if (rs != null) {
288 int count = 0;
289 Vector vColumnName = new Vector();
290 Vector vGetByType = new Vector();
291 if (rs.next()) {
292 if (count == 0) {
293 ResultSetMetaData rsm = rs.getMetaData();
294 count = rsm.getColumnCount();
295 for (int i = 1; i < count + 1; i++) {
296 Object oCn = rsm.getColumnName(i);
297 String sSt = rsm.getColumnTypeName(i);
298 vColumnName.addElement(oCn);
299 vGetByType.addElement(sSt);
300 resH.put(oCn, sqlGetByTypeDec(sSt, i, rs));
301 }
302 } else {
303 for (int i = 1; i < count + 1; i++) {
304 resH.put(vColumnName.elementAt(i - 1),
305 sqlGetByTypeDec( (String) vGetByType.elementAt(i -
306 1),
307 i, rs));
308 }
309 }
310 }
311 }
312 } catch (Exception e) {
313 e.printStackTrace();
314 throw e;
315 } finally {
316 try {
317 if (rs != null) {
318 rs.close();
319 }
320 } catch (Exception e) {
321 e.printStackTrace();
322 throw e;
323 }
324 }
325 return resH;
326 }
327
328 public static List getListByRS(ResultSet rs) throws Exception {
329 List list = new ArrayList();
330
331 try {
332 if (rs != null) {
333 while (rs.next()) {
334 HashMap resH = new HashMap();
335 int count = 0;
336 Vector vColumnName = new Vector();
337 Vector vGetByType = new Vector();
338 if (count == 0) {
339 ResultSetMetaData rsm = rs.getMetaData();
340 count = rsm.getColumnCount();
341 for (int i = 1; i < count + 1; i++) {
342 Object oCn = rsm.getColumnName(i);
343 String sSt = rsm.getColumnTypeName(i);
344 vColumnName.addElement(oCn);
345 vGetByType.addElement(sSt);
346 resH.put(oCn, sqlGetByTypeDec(sSt, i, rs));
347 }
348 } else {
349 for (int i = 1; i < count + 1; i++) {
350 resH.put(vColumnName.elementAt(i - 1),
351 sqlGetByTypeDec( (String) vGetByType.elementAt(i -
352 1),
353 i, rs));
354 }
355 }
356 // System.out.println("hashmap value:"+resH);
357 list.add(resH);
358 }
359 }
360 } catch (Exception e) {
361 e.printStackTrace();
362 throw e;
363 } finally {
364 try {
365 if (rs != null) {
366 rs.close();
367 }
368 } catch (Exception e) {
369 e.printStackTrace();
370 throw e;
371 }
372 }
373 return list;
374 }
375
376 public static Object sqlGetByTypeDec(String eleType, int no, ResultSet rs) throws
377 Exception {
378 Object r = new Object();
379 try {
380 if (eleType.equalsIgnoreCase("datetime")) {
381 r = rs.getDate(no);
382 } else if (eleType.equalsIgnoreCase("date")) {
383 r = rs.getDate(no);
384 } else if (eleType.equalsIgnoreCase("bigint") ||
385 eleType.equalsIgnoreCase("smallint") ||
386 eleType.equalsIgnoreCase("LONGLONG")
387 || eleType.equalsIgnoreCase("SHORT")
388 || eleType.equalsIgnoreCase("INTEGER")) {
389 String ii = rs.getString(no);
390 r = ii == null ? null : new Integer(ii);
391 } else if (eleType.equalsIgnoreCase("boolean")) {
392 r = new Boolean(rs.getBoolean(no));
393 } else if (eleType.equalsIgnoreCase("float")) {
394 r = new Float(rs.getFloat(no));
395 } else if (eleType.equalsIgnoreCase("DECIMAL") ||
396 eleType.equalsIgnoreCase("NUMBER")) {
397 String s = rs.getString(no);
398 r = new BigDecimal(s == null || s.equals("") ? "0.00" : s);
399 } else if (eleType.equalsIgnoreCase("CLOB")) {
400 Clob value = rs.getClob(no);
401 if (value != null && value.length() > 0) {
402 return value.getSubString( (long) 1, (int) value.length());
403 }
404 } else {
405 r = rs.getString(no);
406 }
407 } catch (Exception e) {
408 e.printStackTrace();
409 throw e;
410 }
411 if (r == null) {
412 r = "";
413 }
414 return r;
415 }
416
417}
418
2
3 /** */ /**
4 * <p>Title: </p>
5 *
6 * <p>Description: </p>
7 *
8 * <p>Copyright: Copyright (c) 2006</p>
9 *
10 * <p>Company: fishsoft</p>
11 *
12 * @author Danny
13 * @version 1.0
14 */
15 import java.sql. * ;
16 import javax.sql. * ;
17 import java.util. * ;
18 import java.io. * ;
19 import java.math.BigDecimal;
20
21 public class DBUtil {
22
23 public final static int INSERT_MODE = 0;
24 public final static int UPDATE_MODE = 1;
25 public final static int DELETE_MODE = 2;
26 public final static int QUERY_MODE = 3;
27 public final static int MODIFY_MODE = 4;
28
29 private static final String sSeperator = ";";
30 private static final String sDatePattern = "yyyy/MM/dd";
31 private static final String sDateTimePattern = "yyyy/MM/dd hh:mm:ss S";
32 private String sTp = "java.math.BigDecimal;java.lang.Integer;java.lang.Double;java.lang.Float;java.lang.StringBuffer;";
33
34 private DataSource oDs;
35// private Connection oCon;
36 boolean DEBUG = true;
37 private Statement oSt = null;
38
39 public DBUtil() {
40
41 }
42
43 private Integer modify(String s_sql, Connection oCont) throws Exception {
44 Integer iRs = null;
45 if (oSt != null) {
46 oSt.close();
47 oSt = null;
48 }
49 try {
50 oSt = oCont.createStatement();
51 iRs = new Integer(oSt.executeUpdate(s_sql));
52 } catch (SQLException sqle) {
53 throw sqle;
54 }
55 return iRs;
56 }
57
58 private ResultSet query(String s_sql, Connection oCont) throws Exception {
59 ResultSet oRs = null;
60 if (oSt != null) {
61 oSt.close();
62 oSt = null;
63 }
64 try {
65 oSt = oCont.createStatement();
66 oRs = oSt.executeQuery(s_sql);
67 } catch (SQLException sqle) {
68 throw sqle;
69 }
70 return oRs;
71 }
72
73 public Object doSql(String sSql, int i_mode, Connection oCont) throws
74 Exception {
75 try {
76 switch (i_mode) {
77 case INSERT_MODE:
78 case UPDATE_MODE:
79 case DELETE_MODE:
80 return modify(sSql, oCont);
81 case QUERY_MODE:
82 return query(sSql, oCont);
83 }
84 } catch (Exception e) {
85 throw e;
86 }
87 return null;
88 }
89
90 public Integer insOB(String tableName, ObjectBean res, Connection oCont) throws
91 Exception {
92// res.canDB();
93 try {
94 String sql = "";
95 sql = "INSERT INTO " + tableName + " ( ";
96 Enumeration en = res.keys();
97 boolean f = false;
98 String key = "", val = "";
99 while (en.hasMoreElements()) {
100 if (f) {
101 key += ", ";
102 val += ", ";
103 } else {
104 f = true;
105 }
106 String k = (String) en.nextElement();
107 key += k;
108 String tp = res.get(k).getClass().getName();
109 if (this.sTp.indexOf(tp + ";") >= 0) {
110 val += "" + res.get(k) + "";
111 } else {
112 String temp = res.get(k)+"";
113 if (temp.equals("getdate()"))
114 val += "" + temp + "";
115 else
116 val += "'" + temp + "'";
117 }
118 }
119 sql += key + ") VALUES (" + val + ")";
120
121 //System.out.println("real sql:"+sql);
122
123 return (Integer)this.doSql(sql, this.INSERT_MODE, oCont);
124 } catch (Exception e) {
125 e.printStackTrace();
126 throw e;
127 }
128 }
129
130 public Integer updOB(String tableName, ObjectBean res, ObjectBean where,
131 Connection oCont) throws Exception {
132// res.canDB();
133 try {
134 String sql = "";
135 sql = "UPDATE " + tableName + " SET ";
136 Enumeration en = res.keys();
137 boolean f = false;
138 while (en.hasMoreElements()) {
139 if (f) {
140 sql += ", ";
141 } else {
142 f = true;
143 }
144 String k = (String) en.nextElement();
145 String tp = res.get(k).getClass().getName();
146 if (this.sTp.indexOf(tp + ";") >= 0) {
147 sql += k + "=" + "" + res.get(k) + "";
148 } else {
149 sql += k + "=" + "'" + res.get(k) + "'";
150 }
151 }
152 sql += " WHERE ";
153
154 en = where.keys();
155 f = false;
156 while (en.hasMoreElements()) {
157 if (f) {
158 sql += "and ";
159 } else {
160 f = true;
161 }
162 String k = (String) en.nextElement();
163 String tp = where.get(k).getClass().getName();
164 if (this.sTp.indexOf(tp + ";") >= 0) {
165 sql += k + "=" + "" + where.get(k) + "";
166 } else {
167 sql += k + "=" + "'" + where.get(k) + "'";
168 }
169 }
170
171 return (Integer)this.doSql(sql, this.UPDATE_MODE, oCont);
172 } catch (Exception e) {
173 e.printStackTrace();
174 throw e;
175 }
176 }
177
178 public Integer delOB(String tableName, ObjectBean where, Connection oCont) throws
179 Exception {
180 try {
181 String sql = "";
182 sql = "DELETE FROM " + tableName + " WHERE ";
183
184 Enumeration en = where.keys();
185 boolean f = false;
186 while (en.hasMoreElements()) {
187 if (f) {
188 sql += "and ";
189 } else {
190 f = true;
191 }
192 String k = (String) en.nextElement();
193 String tp = where.get(k).getClass().getName();
194 if (this.sTp.indexOf(tp + ";") >= 0) {
195 sql += k + "=" + "" + where.get(k) + "";
196 } else {
197 sql += k + "=" + "'" + where.get(k) + "'";
198 }
199 }
200
201 return (Integer)this.doSql(sql, this.DELETE_MODE, oCont);
202 } catch (Exception e) {
203 e.printStackTrace();
204 throw e;
205 }
206 }
207
208 public ResultSet queOB(String tableName, String res, ObjectBean where,
209 Connection oCont) throws Exception {
210 try {
211 String sql = "";
212 sql = "SELECT " + res + " FROM " + tableName + " where ";
213
214 Enumeration en = where.keys();
215 boolean f = false;
216 while (en.hasMoreElements()) {
217 if (f) {
218 sql += "and ";
219 } else {
220 f = true;
221 }
222 String k = (String) en.nextElement();
223 String tp = where.get(k).getClass().getName();
224 if (this.sTp.indexOf(tp + ";") >= 0) {
225 sql += k + "=" + "" + where.get(k) + "";
226 } else {
227 sql += k + "=" + "'" + where.get(k) + "'";
228 }
229 }
230
231 return (ResultSet)this.doSql(sql, this.QUERY_MODE, oCont);
232 } catch (Exception e) {
233 e.printStackTrace();
234 throw e;
235 }
236 }
237
238 public static Vector getHashMapVByRSDec(ResultSet rs) throws Exception {
239 Vector resV = new Vector();
240 try {
241 if (rs != null) {
242 int count = 0;
243 Vector vColumnName = new Vector();
244 Vector vGetByType = new Vector();
245 while (rs.next()) {
246 ObjectBean ob = new ObjectBean();
247 if (count == 0) {
248 ResultSetMetaData rsm = rs.getMetaData();
249 count = rsm.getColumnCount();
250 for (int i = 1; i < count + 1; i++) {
251 Object oCn = rsm.getColumnName(i);
252 String sSt = rsm.getColumnTypeName(i);
253 vColumnName.addElement(oCn);
254 vGetByType.addElement(sSt);
255 ob.set(oCn, sqlGetByTypeDec(sSt, i, rs));
256 }
257 } else {
258 for (int i = 1; i < count + 1; i++) {
259 ob.set(vColumnName.elementAt(i - 1),
260 sqlGetByTypeDec( (String) vGetByType.elementAt(i -
261 1),
262 i, rs));
263 }
264 }
265 resV.addElement(ob);
266 }
267 }
268 } catch (Exception e) {
269 e.printStackTrace();
270 throw e;
271 } finally {
272 try {
273 if (rs != null) {
274 rs.close();
275 }
276 } catch (Exception e) {
277 e.printStackTrace();
278 throw e;
279 }
280 }
281 return resV;
282 }
283
284 public static HashMap getHashMapByRS(ResultSet rs) throws Exception {
285 HashMap resH = new HashMap();
286 try {
287 if (rs != null) {
288 int count = 0;
289 Vector vColumnName = new Vector();
290 Vector vGetByType = new Vector();
291 if (rs.next()) {
292 if (count == 0) {
293 ResultSetMetaData rsm = rs.getMetaData();
294 count = rsm.getColumnCount();
295 for (int i = 1; i < count + 1; i++) {
296 Object oCn = rsm.getColumnName(i);
297 String sSt = rsm.getColumnTypeName(i);
298 vColumnName.addElement(oCn);
299 vGetByType.addElement(sSt);
300 resH.put(oCn, sqlGetByTypeDec(sSt, i, rs));
301 }
302 } else {
303 for (int i = 1; i < count + 1; i++) {
304 resH.put(vColumnName.elementAt(i - 1),
305 sqlGetByTypeDec( (String) vGetByType.elementAt(i -
306 1),
307 i, rs));
308 }
309 }
310 }
311 }
312 } catch (Exception e) {
313 e.printStackTrace();
314 throw e;
315 } finally {
316 try {
317 if (rs != null) {
318 rs.close();
319 }
320 } catch (Exception e) {
321 e.printStackTrace();
322 throw e;
323 }
324 }
325 return resH;
326 }
327
328 public static List getListByRS(ResultSet rs) throws Exception {
329 List list = new ArrayList();
330
331 try {
332 if (rs != null) {
333 while (rs.next()) {
334 HashMap resH = new HashMap();
335 int count = 0;
336 Vector vColumnName = new Vector();
337 Vector vGetByType = new Vector();
338 if (count == 0) {
339 ResultSetMetaData rsm = rs.getMetaData();
340 count = rsm.getColumnCount();
341 for (int i = 1; i < count + 1; i++) {
342 Object oCn = rsm.getColumnName(i);
343 String sSt = rsm.getColumnTypeName(i);
344 vColumnName.addElement(oCn);
345 vGetByType.addElement(sSt);
346 resH.put(oCn, sqlGetByTypeDec(sSt, i, rs));
347 }
348 } else {
349 for (int i = 1; i < count + 1; i++) {
350 resH.put(vColumnName.elementAt(i - 1),
351 sqlGetByTypeDec( (String) vGetByType.elementAt(i -
352 1),
353 i, rs));
354 }
355 }
356 // System.out.println("hashmap value:"+resH);
357 list.add(resH);
358 }
359 }
360 } catch (Exception e) {
361 e.printStackTrace();
362 throw e;
363 } finally {
364 try {
365 if (rs != null) {
366 rs.close();
367 }
368 } catch (Exception e) {
369 e.printStackTrace();
370 throw e;
371 }
372 }
373 return list;
374 }
375
376 public static Object sqlGetByTypeDec(String eleType, int no, ResultSet rs) throws
377 Exception {
378 Object r = new Object();
379 try {
380 if (eleType.equalsIgnoreCase("datetime")) {
381 r = rs.getDate(no);
382 } else if (eleType.equalsIgnoreCase("date")) {
383 r = rs.getDate(no);
384 } else if (eleType.equalsIgnoreCase("bigint") ||
385 eleType.equalsIgnoreCase("smallint") ||
386 eleType.equalsIgnoreCase("LONGLONG")
387 || eleType.equalsIgnoreCase("SHORT")
388 || eleType.equalsIgnoreCase("INTEGER")) {
389 String ii = rs.getString(no);
390 r = ii == null ? null : new Integer(ii);
391 } else if (eleType.equalsIgnoreCase("boolean")) {
392 r = new Boolean(rs.getBoolean(no));
393 } else if (eleType.equalsIgnoreCase("float")) {
394 r = new Float(rs.getFloat(no));
395 } else if (eleType.equalsIgnoreCase("DECIMAL") ||
396 eleType.equalsIgnoreCase("NUMBER")) {
397 String s = rs.getString(no);
398 r = new BigDecimal(s == null || s.equals("") ? "0.00" : s);
399 } else if (eleType.equalsIgnoreCase("CLOB")) {
400 Clob value = rs.getClob(no);
401 if (value != null && value.length() > 0) {
402 return value.getSubString( (long) 1, (int) value.length());
403 }
404 } else {
405 r = rs.getString(no);
406 }
407 } catch (Exception e) {
408 e.printStackTrace();
409 throw e;
410 }
411 if (r == null) {
412 r = "";
413 }
414 return r;
415 }
416
417}
418