Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package pb_sem;
- import java.sql.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author edo
- */
- public class Main {
- public static void gostota(Connection con){
- try {
- int mapSize = 800;
- int[][] tab = new int[mapSize + 1][mapSize + 1];
- Statement stm = con.createStatement();
- ResultSet result = stm.executeQuery("SELECT x , y ,population FROM VILLAGE");
- while (result.next()) {
- int x = result.getInt(1);
- int y = result.getInt(2);
- tab[x + (mapSize / 2)][y + (mapSize / 2)] = result.getInt(3);
- }
- stm.executeUpdate("TRUNCATE TABLE gostota");
- int sum;
- int index=0;
- for (int i = 0; i < mapSize; i += 10) {
- for (int j = 0; j < mapSize; j += 10) {
- sum = 0;
- for (int k = 0; k < 10; k++) {
- for (int m = 0; m < 10; m++) {
- sum += tab[i + k][j + m];
- }
- }
- double gostota = (double) sum / 100.0;
- 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);
- stm.executeUpdate(baza);
- }
- }
- } catch (SQLException ex) {
- Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public static void main(String[] args) {
- String dbUser = "pb";
- String dbPass = "pbvaje";
- String dbHost = "127.0.0.1:3306";
- String db = "vaje";
- String abUrl = "jdbc:mysql://" + dbHost + "/" + db;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException ex) {
- System.out.println("Ni driverja");
- }
- try {
- Connection con = DriverManager.getConnection(abUrl, dbUser, dbPass);
- gostota(con);
- } catch (SQLException ex) {
- System.out.println("se ne poveze");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement