14 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
15 |
|
|
16 |
|
|
17 |
|
|
18 |
public class Database { |
public class Database { |
19 |
Logger logger = Logger.getLogger(Database.class.getName()); |
Logger logger = Logger.getLogger(Database.class.getName()); |
20 |
|
|
21 |
|
int batchCount = 0; |
22 |
|
|
23 |
Connection conn; |
Connection conn; |
24 |
PreparedStatement saveStmt; |
PreparedStatement saveStmt; |
44 |
} |
} |
45 |
|
|
46 |
public void renameResultTables() throws SQLException { |
public void renameResultTables() throws SQLException { |
47 |
|
Constants consts = Constants.getInstance(); |
48 |
|
String ext = consts.getTableExtension(); |
49 |
|
|
50 |
logger.info("Dropping old backup table (if exists)"); |
logger.info("Dropping old backup table (if exists)"); |
51 |
String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_old"; |
String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_old" + ext; |
52 |
conn.createStatement().executeUpdate(sql); |
conn.createStatement().executeUpdate(sql); |
53 |
|
|
54 |
logger.info("Rename tables"); |
logger.info("Rename tables"); |
55 |
sql = "RENAME TABLE fulddaekning.afstand_anden_rute TO fulddaekning.afstand_anden_rute_old, fulddaekning.afstand_anden_rute_ny TO fulddaekning.afstand_anden_rute"; |
sql = "RENAME TABLE fulddaekning.afstand_anden_rute" + ext + " TO fulddaekning.afstand_anden_rute_old" + ext + ", fulddaekning.afstand_anden_rute_ny TO fulddaekning.afstand_anden_rute" + ext; |
56 |
|
|
57 |
|
logger.info("Executing: " + sql); |
58 |
conn.createStatement().executeUpdate(sql); |
conn.createStatement().executeUpdate(sql); |
59 |
} |
} |
60 |
|
|
151 |
|
|
152 |
List<Adresse> list = hentAdresseListe( stmt ); |
List<Adresse> list = hentAdresseListe( stmt ); |
153 |
return list.toArray( new Adresse[ list.size() ] ); |
return list.toArray( new Adresse[ list.size() ] ); |
|
|
|
154 |
} |
} |
155 |
|
|
156 |
|
|
157 |
|
public Adresse[] hentAlleDaekkedeAdresser() throws SQLException { |
158 |
|
String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute " + |
159 |
|
"FROM fulddaekning.adressetabel " + |
160 |
|
"WHERE rute IS NOT NULL " + |
161 |
|
"AND latitude IS NOT NULL " + |
162 |
|
"AND longitude IS NOT NULL " + |
163 |
|
"AND distributor = ? "; |
164 |
|
|
165 |
|
// Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets) |
166 |
|
// Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html |
167 |
|
//PreparedStatement stmt = conn.prepareStatement(sql); |
168 |
|
PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY); |
169 |
|
stmt.setFetchSize(Integer.MIN_VALUE); |
170 |
|
|
171 |
|
stmt.setString(1, Lookup.distributor); |
172 |
|
|
173 |
|
List<Adresse> list = hentAdresseListe( stmt ); |
174 |
|
return list.toArray( new Adresse[ list.size() ] ); |
175 |
|
} |
176 |
|
|
177 |
|
|
178 |
|
|
179 |
public synchronized void gemResultat(Adresse orgAdresse, Adresse bedsteAdresse, double bedsteAfstand) throws SQLException { |
public synchronized void gemResultat(Adresse orgAdresse, Adresse bedsteAdresse, double bedsteAfstand) throws SQLException { |
180 |
/*String sql = "INSERT INTO fulddaekning.afstand_anden_rute_thn (orgId,orgPostnr, orgAdresse,orgGadeid,orgHusnr,orgHusnrBogstav,orgLatitude,orgLongitude,orgRute,id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,afstand,`timestamp`) "+ |
/*String sql = "INSERT INTO fulddaekning.afstand_anden_rute_thn (orgId,orgPostnr, orgAdresse,orgGadeid,orgHusnr,orgHusnrBogstav,orgLatitude,orgLongitude,orgRute,id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,afstand,`timestamp`) "+ |
205 |
|
|
206 |
saveStmt.setDouble(19, bedsteAfstand); |
saveStmt.setDouble(19, bedsteAfstand); |
207 |
|
|
208 |
saveStmt.executeUpdate(); |
saveStmt.addBatch(); |
209 |
saveStmt.clearParameters(); |
batchCount++; |
210 |
|
if (batchCount >= 100) { |
211 |
|
saveStmt.executeBatch(); |
212 |
|
batchCount = 0; |
213 |
|
} |
214 |
|
//saveStmt.executeUpdate(); |
215 |
|
//saveStmt.clearParameters(); |
216 |
|
|
217 |
//saveStmt.close(); |
//saveStmt.close(); |
218 |
|
} |
219 |
|
|
220 |
|
public synchronized void saveBatch() throws SQLException{ |
221 |
|
saveStmt.executeBatch(); |
222 |
|
batchCount = 0; |
223 |
} |
} |
224 |
|
|
225 |
|
|
268 |
connectionProps.put("user", db_user); |
connectionProps.put("user", db_user); |
269 |
connectionProps.put("password", db_pass); |
connectionProps.put("password", db_pass); |
270 |
|
|
271 |
|
//For debug output, tilføj denne til JDBC url'en: &profileSQL=true |
272 |
conn = DriverManager.getConnection( |
conn = DriverManager.getConnection( |
273 |
"jdbc:mysql://" + |
"jdbc:mysql://" + |
274 |
db_host + |
db_host + |