/[projects]/dao/FuldDaekningWorker/src/dk/daoas/fulddaekning/Database.java
ViewVC logotype

Diff of /dao/FuldDaekningWorker/src/dk/daoas/fulddaekning/Database.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2151 by torben, Wed May 7 07:59:03 2014 UTC revision 2203 by torben, Thu Sep 11 14:46:59 2014 UTC
# Line 1  Line 1 
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;
# Line 18  public class Database { Line 21  public class Database {
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);          
# Line 29  public class Database { Line 32  public class Database {
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 {
55    
56                  String sql =                  String sql =
57                                  "select max(latitude) latmax, min(latitude) latmin, max(longitude) lngmax,min(longitude) lngmin  " +                                  "SELECT max(latitude) latmax, min(latitude) latmin, max(longitude) lngmax,min(longitude) lngmin  " +
58                                                  "from fulddaekning.adressetabel WHERE postnr=? and rute is null;";                                  "FROM fulddaekning.adressetabel WHERE postnr=? and rute is null;";
59    
60                  PreparedStatement stmt = conn.prepareStatement(sql);                  PreparedStatement stmt = conn.prepareStatement(sql);
61                  stmt.setInt(1, postnr);                  stmt.setInt(1, postnr);
# Line 62  public class Database { Line 78  public class Database {
78          public Queue<Adresse> hentIkkedaekkedeAdresser(int postnr)  throws SQLException {          public Queue<Adresse> hentIkkedaekkedeAdresser(int postnr)  throws SQLException {
79                  ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>();                  ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>();
80    
81                  String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute FROM fulddaekning.adressetabel " +                  String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute " +
82                                    "FROM fulddaekning.adressetabel " +
83                                  "WHERE rute IS NULL " +  //Ingen dækning                                  "WHERE rute IS NULL " +  //Ingen dækning
84                                  "AND postnr=?  " +                                  "AND postnr=?  " +
85                                  "AND latitude IS NOT NULL " +                                  "AND latitude IS NOT NULL " +
# Line 77  public class Database { Line 94  public class Database {
94    
95          public List<Integer> hentPostnumre() throws SQLException {          public List<Integer> hentPostnumre() throws SQLException {
96                  ArrayList<Integer> list = new ArrayList<Integer>();                  ArrayList<Integer> list = new ArrayList<Integer>();
97                    
98                    Constants consts = Constants.getInstance();
99    
100    
101                  String sql = "SELECT postnr FROM fulddaekning.adressetabel WHERE distributor = 'DAO' and rute is not null GROUP BY postnr ORDER by postnr";                  String sql = "SELECT postnr " +
102                                             "FROM fulddaekning.adressetabel " +
103                                             //"WHERE distributor = ? and rute is not null " +
104                                             "WHERE postnr BETWEEN ? AND ? " +
105                                             "GROUP BY postnr " +
106                                             "ORDER by postnr";
107                  PreparedStatement stmt = conn.prepareStatement(sql);                  PreparedStatement stmt = conn.prepareStatement(sql);
108                    //stmt.setString(1, Lookup.distributor );
109                    stmt.setInt(1, consts.getMinPostnr());
110                    stmt.setInt(2, consts.getMaxPostnr());
111                  ResultSet res = stmt.executeQuery();                  ResultSet res = stmt.executeQuery();
112    
113                  while (res.next()) {                  while (res.next()) {
# Line 96  public class Database { Line 123  public class Database {
123          }          }
124    
125          public ArrayList<Adresse> hentDaekkedeAdresser( BoundingBox bbox) throws SQLException {          public ArrayList<Adresse> hentDaekkedeAdresser( BoundingBox bbox) throws SQLException {
126                  String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute FROM fulddaekning.adressetabel " +                  String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute " +
127                                    "FROM fulddaekning.adressetabel " +
128                                  "WHERE rute IS NOT NULL " +                                  "WHERE rute IS NOT NULL " +
129                                  "AND latitude BETWEEN ? AND ? " +                                  "AND latitude BETWEEN ? AND ? " +
130                                  "AND longitude BETWEEN ? AND ? " +                                  "AND longitude BETWEEN ? AND ? " +
131                                  "AND distributor = 'DAO' ";                                  "AND distributor = ? ";
132    
133                  // Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets)                  // Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets)
134                  // Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html                  // Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html
# Line 112  public class Database { Line 140  public class Database {
140                  stmt.setDouble(2, bbox.latitudeMax);                  stmt.setDouble(2, bbox.latitudeMax);
141                  stmt.setDouble(3, bbox.longitudeMin);                  stmt.setDouble(3, bbox.longitudeMin);
142                  stmt.setDouble(4, bbox.longitudeMax);                  stmt.setDouble(4, bbox.longitudeMax);
143                    stmt.setString(5, Lookup.distributor);
144    
145                  return hentAdresseListe( stmt );                  return hentAdresseListe( stmt );
146    
# Line 187  public class Database { Line 216  public class Database {
216                  return list;                  return list;
217          }          }
218    
219            public Connection getConnection(SafeProperties conf) throws SQLException, IOException {
220                    
221                    String db_host = conf.getSafeProperty("DB_HOST");
222                    String db_user = conf.getSafeProperty("DB_USER");
223                    String db_pass = conf.getSafeProperty("DB_PASS");
224    
225                    
226                    
227    
228                Connection conn = null;
229                Properties connectionProps = new Properties();
230                connectionProps.put("user", db_user);
231                connectionProps.put("password", db_pass);
232    
233                conn = DriverManager.getConnection(
234                               "jdbc:mysql://" +
235                               db_host +
236                               ":3306/",
237                               connectionProps);
238                logger.info("Connected to database");
239                return conn;
240            }
241    
242  }  }

Legend:
Removed from v.2151  
changed lines
  Added in v.2203

  ViewVC Help
Powered by ViewVC 1.1.20