KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > jwi > jgallery > db > DBManager


1
2 package de.jwi.jgallery.db;
3
4 /*
5  * jGallery - Java web application to display image galleries
6  *
7  * Copyright (C) 2004 Juergen Weber
8  *
9  * This file is part of jGallery.
10  *
11  * jGallery is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
13  * version.
14  *
15  * jGallery is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
16  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License along with jGallery; if not, write to the Free
20  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston
21  */

22
23 import java.io.Serializable JavaDoc;
24 import java.sql.Connection JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.Statement JavaDoc;
28
29 import javax.sql.DataSource JavaDoc;
30
31 /**
32  * @author Jürgen Weber Source file created on 21.03.2004
33  *
34  */

35 public class DBManager implements Serializable JavaDoc
36 {
37
38     private DataSource JavaDoc dataSource;
39
40     public DBManager(DataSource JavaDoc dataSource)
41     {
42         this.dataSource = dataSource;
43     }
44
45     public int getAndIncFolderCounter(String JavaDoc folder, boolean doInc)
46             throws SQLException JavaDoc
47     {
48         String JavaDoc query;
49         Connection JavaDoc conn = dataSource.getConnection();
50         int counter = 0;
51
52         if (conn != null)
53         {
54             Statement JavaDoc stmt = conn.createStatement();
55
56             query = "SELECT id FROM folders where folder='$FOLDER'";
57             query = query.replaceAll("\\$FOLDER", folder);
58             ResultSet JavaDoc rs = stmt.executeQuery(query);
59
60             if (!rs.next())
61             {
62                 if (!doInc)
63                 {
64                     conn.close();
65                     return 0;
66                 }
67                 query = "INSERT INTO folders (folder) VALUES ('$FOLDER')";
68                 query = query.replaceAll("\\$FOLDER", folder);
69                 stmt.executeUpdate(query);
70             }
71
72             if (doInc)
73             {
74                 query = "UPDATE folders SET hits = hits + 1 where folder='$FOLDER'";
75                 query = query.replaceAll("\\$FOLDER", folder);
76                 stmt.executeUpdate(query);
77             }
78
79             query = "SELECT hits FROM folders where folder='$FOLDER'";
80             query = query.replaceAll("\\$FOLDER", folder);
81             rs = stmt.executeQuery(query);
82
83             if (rs.next())
84             {
85                 counter = rs.getInt(1);
86             }
87             conn.close();
88         }
89
90         return counter;
91     }
92
93     public int getAndIncImageCounter(String JavaDoc folder, String JavaDoc image, boolean doInc)
94             throws SQLException JavaDoc
95     {
96         String JavaDoc query;
97         Connection JavaDoc conn = dataSource.getConnection();
98         int counter = 0;
99         int id = 0;
100
101         if (conn != null)
102         {
103             Statement JavaDoc stmt = conn.createStatement();
104
105             query = "SELECT id FROM folders f where folder='$FOLDER'";
106             query = query.replaceAll("\\$FOLDER", folder);
107             ResultSet JavaDoc rs = stmt.executeQuery(query);
108
109             if (rs.next())
110             {
111                 id = rs.getInt(1);
112
113             }
114
115             String JavaDoc fid = Integer.toString(id);
116
117             query = "SELECT id FROM images WHERE folderid='$FID' and image='$IMAGE'";
118             query = query.replaceAll("\\$FID", fid);
119             query = query.replaceAll("\\$IMAGE", image);
120
121             rs = stmt.executeQuery(query);
122
123             // is image allready in table ?
124

125             if (!rs.next())
126             {
127                 if (!doInc)
128                 {
129                     conn.close();
130                     return 0;
131                 }
132                 query = "INSERT INTO images (folderid,image) VALUES ('$FID','$IMAGE')";
133                 query = query.replaceAll("\\$FID", fid);
134                 query = query.replaceAll("\\$IMAGE", image);
135                 stmt.executeUpdate(query);
136             }
137
138             if (doInc)
139             {
140                 query = "UPDATE images SET hits = hits + 1 where folderid='$FID' and image='$IMAGE'";
141                 query = query.replaceAll("\\$FID", fid);
142                 query = query.replaceAll("\\$IMAGE", image);
143                 stmt.executeUpdate(query);
144             }
145             
146             query = "SELECT hits FROM images where folderid='$FID' and image='$IMAGE'";
147             query = query.replaceAll("\\$FID", fid);
148             query = query.replaceAll("\\$IMAGE", image);
149             rs = stmt.executeQuery(query);
150
151             if (rs.next())
152             {
153                 counter = rs.getInt(1);
154             }
155             conn.close();
156         }
157
158         return counter;
159     }
160 }
Popular Tags