Advertisement
Guest User

Untitled

a guest
May 26th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package pb_sem;
  7. import java.sql.*;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10. /**
  11.  *
  12.  * @author edo
  13.  */
  14. public class Main {
  15.  
  16.     public static void gostota(Connection con){
  17.         try {
  18.  
  19.             int mapSize = 800;
  20.             int[][] tab = new int[mapSize + 1][mapSize + 1];
  21.             Statement stm = con.createStatement();
  22.             ResultSet result = stm.executeQuery("SELECT x , y ,population FROM VILLAGE");
  23.            
  24.             while (result.next()) {
  25.                 int x = result.getInt(1);
  26.                 int y = result.getInt(2);
  27.                 tab[x + (mapSize / 2)][y + (mapSize / 2)] = result.getInt(3);
  28.             }
  29.             stm.executeUpdate("TRUNCATE TABLE gostota");
  30.             int sum;
  31.             int index=0;
  32.             for (int i = 0; i < mapSize; i += 10) {
  33.                 for (int j = 0; j < mapSize; j += 10) {
  34.                     sum = 0;
  35.                     for (int k = 0; k < 10; k++) {
  36.                         for (int m = 0; m < 10; m++) {
  37.                             sum += tab[i + k][j + m];
  38.                         }
  39.                     }
  40.                     double gostota = (double) sum / 100.0;
  41.                     String baza=String.format("INSERT INTO gostota(gid,x,y,gostota) values(%d,'%d to %d','%d to %d',%f)",index++, i - (mapSize / 2), i + 10 - (mapSize / 2), j - (mapSize / 2), j + 10 - (mapSize / 2), gostota);
  42.                     stm.executeUpdate(baza);
  43.                 }
  44.             }
  45.         } catch (SQLException ex) {
  46.             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  47.         }
  48.     }
  49.  
  50.     public static void main(String[] args) {
  51.  
  52.                 String dbUser = "pb";
  53.                 String dbPass = "pbvaje";
  54.                 String dbHost = "127.0.0.1:3306";
  55.                 String db = "vaje";
  56.  
  57.  
  58.             String abUrl = "jdbc:mysql://" + dbHost + "/" + db;
  59.         try {
  60.             Class.forName("com.mysql.jdbc.Driver");
  61.         } catch (ClassNotFoundException ex) {
  62.             System.out.println("Ni driverja");
  63.         }
  64.         try {
  65.            Connection con = DriverManager.getConnection(abUrl, dbUser, dbPass);
  66.            gostota(con);
  67.         } catch (SQLException ex) {
  68.            System.out.println("se ne poveze");
  69.         }
  70.            
  71.  
  72.  
  73.  
  74.     }
  75.  
  76.  
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement