KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > jdbc > JDBCTemplate


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.jdbc;
27
28 import org.snipsnap.util.ConnectionManager;
29
30 import javax.sql.DataSource JavaDoc;
31 import java.sql.Connection JavaDoc;
32 import java.sql.PreparedStatement JavaDoc;
33 import java.sql.ResultSet JavaDoc;
34 import java.sql.SQLException JavaDoc;
35
36 /**
37  * Encapsulates JDBC calls into a template method like in Spring (good idea!)
38  *
39  * @author stephan
40  * @version $Id: JDBCTemplate.java 1257 2003-12-11 13:36:55Z leo $
41  */

42
43 public class JDBCTemplate {
44   private DataSource JavaDoc ds;
45   private PreparedStatementSetter setter;
46
47   public JDBCTemplate(DataSource JavaDoc ds) {
48     this.ds = ds;
49   }
50
51   public void query(String JavaDoc query, RowCallbackHandler handler, PreparedStatementSetter setter) {
52     this.setter = setter;
53     query(query, handler);
54   }
55
56   public int update(String JavaDoc query) {
57     PreparedStatement JavaDoc statement = null;
58     ResultSet JavaDoc result = null;
59     Connection JavaDoc connection = null;
60     int count = -1;
61
62     try {
63       connection = ds.getConnection();
64       statement = connection.prepareStatement(query);
65       if (null != setter) {
66         setter.setValues(statement);
67       }
68       count = statement.executeUpdate();
69       // connection.commit();
70
} catch (SQLException JavaDoc e) {
71       throw new RuntimeException JavaDoc("JDBCTemplate: unable to execute query", e);
72     } finally {
73       ConnectionManager.close(result);
74       ConnectionManager.close(statement);
75       ConnectionManager.close(connection);
76     }
77     return count;
78   }
79
80   public int update(String JavaDoc query, PreparedStatementSetter setter) {
81     this.setter = setter;
82     return update(query);
83   }
84
85   public void query(String JavaDoc query, RowCallbackHandler handler) {
86     PreparedStatement JavaDoc statement = null;
87     ResultSet JavaDoc result = null;
88     Connection JavaDoc connection = null;
89
90     try {
91       connection = ds.getConnection();
92       statement = connection.prepareStatement(query);
93       if (null != setter) {
94         setter.setValues(statement);
95       }
96
97       result = statement.executeQuery();
98       while (result.next()) {
99         handler.processRow(result);
100       }
101
102     } catch (SQLException JavaDoc e) {
103       throw new RuntimeException JavaDoc("JDBCTemplate: unable to execute query", e);
104     } finally {
105       ConnectionManager.close(result);
106       ConnectionManager.close(statement);
107       ConnectionManager.close(connection);
108     }
109
110   }
111 }
112
Popular Tags