1 2 package de.jwi.jgallery.db; 3 4 22 23 import java.io.Serializable ; 24 import java.sql.Connection ; 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 import java.sql.Statement ; 28 29 import javax.sql.DataSource ; 30 31 35 public class DBManager implements Serializable 36 { 37 38 private DataSource dataSource; 39 40 public DBManager(DataSource dataSource) 41 { 42 this.dataSource = dataSource; 43 } 44 45 public int getAndIncFolderCounter(String folder, boolean doInc) 46 throws SQLException 47 { 48 String query; 49 Connection conn = dataSource.getConnection(); 50 int counter = 0; 51 52 if (conn != null) 53 { 54 Statement stmt = conn.createStatement(); 55 56 query = "SELECT id FROM folders where folder='$FOLDER'"; 57 query = query.replaceAll("\\$FOLDER", folder); 58 ResultSet 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 folder, String image, boolean doInc) 94 throws SQLException 95 { 96 String query; 97 Connection conn = dataSource.getConnection(); 98 int counter = 0; 99 int id = 0; 100 101 if (conn != null) 102 { 103 Statement stmt = conn.createStatement(); 104 105 query = "SELECT id FROM folders f where folder='$FOLDER'"; 106 query = query.replaceAll("\\$FOLDER", folder); 107 ResultSet rs = stmt.executeQuery(query); 108 109 if (rs.next()) 110 { 111 id = rs.getInt(1); 112 113 } 114 115 String 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 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 |