Mercurial > hg > LGServer
comparison src/main/java/de/mpiwg/gazetteer/utils/DBService.java @ 10:5610250d021a default tip
SectionsIndex, we added a method to print the setting of the VM
author | "jurzua <jurzua@mpiwg-berlin.mpg.de>" |
---|---|
date | Thu, 19 Mar 2015 11:46:33 +0100 |
parents | 5316e79f9a27 |
children |
comparison
equal
deleted
inserted
replaced
9:964fc53abeec | 10:5610250d021a |
---|---|
6 import java.sql.ResultSet; | 6 import java.sql.ResultSet; |
7 import java.sql.SQLException; | 7 import java.sql.SQLException; |
8 import java.sql.Statement; | 8 import java.sql.Statement; |
9 import java.util.ArrayList; | 9 import java.util.ArrayList; |
10 import java.util.Date; | 10 import java.util.Date; |
11 import java.util.HashMap; | |
11 import java.util.List; | 12 import java.util.List; |
13 import java.util.Map; | |
12 | 14 |
13 import org.apache.log4j.Logger; | 15 import org.apache.log4j.Logger; |
14 import org.hibernate.Query; | 16 import org.hibernate.Query; |
15 import org.hibernate.Session; | 17 import org.hibernate.Session; |
16 | 18 |
29 | 31 |
30 // JDBC driver name and database URL | 32 // JDBC driver name and database URL |
31 static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; | 33 static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; |
32 static final String DB_URL = "jdbc:mysql://localhost/"; | 34 static final String DB_URL = "jdbc:mysql://localhost/"; |
33 | 35 |
36 private static DBService instance = null; | |
37 | |
38 public static DBService getInstance(){ | |
39 if(instance == null){ | |
40 instance = new DBService(); | |
41 } | |
42 return instance; | |
43 } | |
44 | |
45 private Map<String, DBBook> bookMap; | |
46 | |
47 public DBBook getBook(String id){ | |
48 return getBookMap().get(id); | |
49 } | |
50 | |
51 private Map<String, DBBook> getBookMap(){ | |
52 if(bookMap == null){ | |
53 try { | |
54 this.loadBookMap(); | |
55 } catch (Exception e) { | |
56 e.printStackTrace(); | |
57 } | |
58 } | |
59 return this.bookMap; | |
60 } | |
61 | |
62 private void loadBookMap() throws SQLException{ | |
63 Long start = System.currentTimeMillis(); | |
64 Connection conn = null; | |
65 Statement stmt = null; | |
66 this.bookMap = new HashMap<String, DBBook>(); | |
67 | |
68 try { | |
69 String query = "SELECT * FROM books"; | |
70 logger.debug(query); | |
71 conn = getNewConnection(); | |
72 stmt = conn.createStatement(); | |
73 ResultSet rs = stmt.executeQuery(query); | |
74 | |
75 while(rs.next()){ | |
76 DBBook book = new DBBook(rs); | |
77 this.bookMap.put(book.getId(), book); | |
78 } | |
79 } catch (Exception e) { | |
80 e.printStackTrace(); | |
81 }finally{ | |
82 conn.close(); | |
83 } | |
84 | |
85 long end = System.currentTimeMillis(); | |
86 logger.debug("Time execution loading Book Map [ms]: " + (end - start)); | |
87 } | |
88 | |
34 /** | 89 /** |
35 * This methods search from a list of terms. | 90 * This methods search from a list of terms. |
36 * Every term is considered a subsequence of whole section name. | 91 * Every term is considered a subsequence of whole section name. |
37 * | 92 * |
38 * @param termList | 93 * @param termList |
54 if(i>0){ | 109 if(i>0){ |
55 query += " OR "; | 110 query += " OR "; |
56 } | 111 } |
57 query += "name like '%" + term + "%' "; | 112 query += "name like '%" + term + "%' "; |
58 } | 113 } |
59 //query += " limit 50"; | |
60 | 114 |
61 try { | 115 try { |
62 Class.forName(JDBC_DRIVER); | 116 Class.forName(JDBC_DRIVER); |
63 conn = getNewConnection(); | 117 conn = getNewConnection(); |
64 stmt = conn.createStatement(); | 118 stmt = conn.createStatement(); |
65 | 119 |
66 ResultSet rs = stmt.executeQuery(query); | 120 ResultSet rs = stmt.executeQuery(query); |
67 while (rs.next()) { | 121 while (rs.next()) { |
68 DBSection section = new DBSection(rs); | 122 DBSection section = new DBSection(rs); |
69 //DBBook book = getBook0(conn, section.getBookId()); | 123 //DBBook book = getBook0(conn, section.getBookId()); |
70 //section.setBook(book); | 124 DBBook book = getInstance().getBook(section.getBookId()); |
125 section.setBook(book); | |
71 list.add(section); | 126 list.add(section); |
72 } | 127 } |
73 rs.close(); | 128 rs.close(); |
74 } catch (Exception e) { | 129 } catch (Exception e) { |
75 e.printStackTrace(); | 130 e.printStackTrace(); |
76 } finally { | 131 } finally { |
77 conn.close(); | 132 conn.close(); |
78 } | 133 } |
79 | 134 |
80 long end = System.currentTimeMillis(); | 135 long end = System.currentTimeMillis(); |
81 System.out.println("Time execution [ms]: " + (end - start)); | 136 logger.debug("Time execution serching [ms]: " + (end - start)); |
82 | 137 |
83 return list; | 138 return list; |
84 } | 139 } |
85 | 140 |
86 public static List<String> suggestSectionName(String term) throws SQLException { | 141 public static List<String> suggestSectionName(String term) throws SQLException { |
171 | 226 |
172 System.out.println("bookId=" + bookId + ", startPage=" + startPage + ", endPage=" + endPage); | 227 System.out.println("bookId=" + bookId + ", startPage=" + startPage + ", endPage=" + endPage); |
173 String content = getContent(conn, bookId, startPage, endPage); | 228 String content = getContent(conn, bookId, startPage, endPage); |
174 response.setText(content); | 229 response.setText(content); |
175 | 230 |
176 DBBook book = getBook0(conn, bookId); | 231 DBBook book = getBook0FromDB(conn, bookId); |
177 response.setBook(book); | 232 response.setBook(book); |
178 | 233 |
179 } | 234 } |
180 rs.close(); | 235 rs.close(); |
181 } catch (Exception e) { | 236 } catch (Exception e) { |
223 | 278 |
224 return sb.toString(); | 279 return sb.toString(); |
225 } | 280 } |
226 | 281 |
227 | 282 |
228 public static DBBook getBook(String id) throws SQLException{ | 283 public static DBBook getBookFromDB(String id) throws SQLException{ |
229 Connection conn = null; | 284 Connection conn = null; |
230 DBBook book = null; | 285 DBBook book = null; |
231 | 286 |
232 try { | 287 try { |
233 Class.forName(JDBC_DRIVER); | 288 //Class.forName(JDBC_DRIVER); |
234 | 289 conn = getNewConnection(); |
235 conn = getNewConnection(); | 290 book = getBook0FromDB(conn, id); |
236 book = getBook0(conn, id); | |
237 } catch (Exception e) { | 291 } catch (Exception e) { |
238 e.printStackTrace(); | 292 e.printStackTrace(); |
239 } finally { | 293 } finally { |
240 conn.close(); | 294 conn.close(); |
241 } | 295 } |
242 return book; | 296 return book; |
243 } | 297 } |
244 | 298 |
245 private static DBBook getBook0(Connection conn, String id) throws SQLException{ | 299 private static DBBook getBook0FromDB(Connection conn, String id) throws SQLException{ |
246 DBBook book = null; | 300 DBBook book = null; |
247 | 301 |
248 String query = "SELECT * FROM books WHERE id = '" + id + "'"; | 302 String query = "SELECT * FROM books WHERE id = '" + id + "'"; |
249 logger.debug(query); | 303 logger.debug(query); |
250 | 304 |