1 |
package dk.daoas.fulddaekning; |
package dk.daoas.fulddaekning; |
2 |
|
|
3 |
|
import java.io.IOException; |
4 |
import java.sql.Connection; |
import java.sql.Connection; |
5 |
|
import java.sql.DriverManager; |
6 |
import java.sql.PreparedStatement; |
import java.sql.PreparedStatement; |
7 |
import java.sql.ResultSet; |
import java.sql.ResultSet; |
8 |
import java.sql.SQLException; |
import java.sql.SQLException; |
9 |
import java.util.ArrayList; |
import java.util.ArrayList; |
10 |
import java.util.List; |
import java.util.List; |
11 |
|
import java.util.Properties; |
12 |
import java.util.Queue; |
import java.util.Queue; |
13 |
import java.util.concurrent.ConcurrentLinkedQueue; |
import java.util.concurrent.ConcurrentLinkedQueue; |
14 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
21 |
Connection conn; |
Connection conn; |
22 |
PreparedStatement saveStmt; |
PreparedStatement saveStmt; |
23 |
|
|
24 |
public Database(Connection conn) throws SQLException { |
public Database(SafeProperties conf) throws SQLException,IOException { |
25 |
this.conn = conn; |
this.conn = getConnection( conf ); |
26 |
|
|
27 |
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_ny (orgId,orgPostnr, orgAdresse,orgGadeid,orgHusnr,orgHusnrBogstav,orgLatitude,orgLongitude,orgRute,id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,afstand,`timestamp`) "+ |
28 |
"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, now() )"; |
"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, now() )"; |
29 |
|
|
30 |
saveStmt = conn.prepareStatement(sql); |
saveStmt = conn.prepareStatement(sql); |
32 |
} |
} |
33 |
|
|
34 |
public void resetResultTable() throws SQLException { |
public void resetResultTable() throws SQLException { |
35 |
logger.info("Truncating result table"); |
logger.info("Dropping old result table (if exists)"); |
36 |
String sql = "TRUNCATE TABLE fulddaekning.afstand_anden_rute_thn"; |
String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_ny"; |
37 |
conn.createStatement().executeUpdate(sql); |
conn.createStatement().executeUpdate(sql); |
38 |
|
|
39 |
|
logger.info("Create new result table"); |
40 |
|
sql = "CREATE TABLE fulddaekning.afstand_anden_rute_ny LIKE fulddaekning.afstand_anden_rute"; |
41 |
|
conn.createStatement().executeUpdate(sql); |
42 |
|
} |
43 |
|
|
44 |
|
public void renameResultTables() throws SQLException { |
45 |
|
logger.info("Dropping old backup table (if exists)"); |
46 |
|
String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_old"; |
47 |
|
conn.createStatement().executeUpdate(sql); |
48 |
|
|
49 |
|
logger.info("Rename tables"); |
50 |
|
sql = "RENAME TABLE fulddaekning.afstand_anden_rute TO fulddaekning.afstand_anden_rute_old, fulddaekning.afstand_anden_rute_ny TO fulddaekning.afstand_anden_rute"; |
51 |
|
conn.createStatement().executeUpdate(sql); |
52 |
} |
} |
53 |
|
|
54 |
public BoundingBox getBoundingbox(int postnr) throws SQLException { |
public BoundingBox getBoundingbox(int postnr) throws SQLException { |
203 |
return list; |
return list; |
204 |
} |
} |
205 |
|
|
206 |
|
public Connection getConnection(SafeProperties conf) throws SQLException, IOException { |
207 |
|
|
208 |
|
String db_host = conf.getSafeProperty("DB_HOST"); |
209 |
|
String db_user = conf.getSafeProperty("DB_USER"); |
210 |
|
String db_pass = conf.getSafeProperty("DB_PASS"); |
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
Connection conn = null; |
216 |
|
Properties connectionProps = new Properties(); |
217 |
|
connectionProps.put("user", db_user); |
218 |
|
connectionProps.put("password", db_pass); |
219 |
|
|
220 |
|
conn = DriverManager.getConnection( |
221 |
|
"jdbc:mysql://" + |
222 |
|
db_host + |
223 |
|
":3306/", |
224 |
|
connectionProps); |
225 |
|
logger.info("Connected to database"); |
226 |
|
return conn; |
227 |
|
} |
228 |
|
|
229 |
} |
} |