Advertisement
Guest User

Untitled

a guest
Mar 11th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1. package SomePackage;
  2.  
  3. /**
  4.  * Created by A.V.Tsaplin on 03.03.2016.
  5.  */
  6.  
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.SQLException;
  10. import java.sql.Statement;
  11.  
  12.  
  13.  
  14.  
  15. public class SqlInit {
  16.     // JDBC URL, username and password of MySQL server
  17.     private static final String url = "jdbc:mysql://localhost:3306/mydb";
  18.     private static final String user = "root";
  19.     private static final String password = "mercedesg55amg";
  20.  
  21.     // JDBC variables for opening and managing connection
  22.     private static Connection con;
  23.     private static Statement stmt;
  24.  
  25.     public void Init() {
  26.  
  27.         final String queryDropTable = "Drop table mapnumberone";
  28.  
  29.         final String queryCreateTable = "create table if not exists mapnumberone (id int(11) not null," +
  30.                 " x int(11) not null, y int(11) not null, value int(11) not null, primary key(id))" +
  31.                 " engine = INNODB default charset = latin1";
  32.  
  33.         char[][] mapNew = {{'1', '1', '0', '1', '1', '1', '1', '1', '1'},
  34.                 {'1', '0', '0', '0', '0', '0', '0', '0', '1'},
  35.                 {'1', '1', '1', '1', '0', '1', '1', '1', '1'},
  36.                 {'1', '1', '1', '1', '0', '0', '0', '0', '1'},
  37.                 {'1', '1', '0', '0', '0', '1', '0', '1', '1'},
  38.                 {'1', '1', '0', '1', '0', '1', '0', '0', '1'},
  39.                 {'1', '1', '0', '1', '0', '1', '1', '0', '$'},
  40.                 {'1', '1', '1', '1', '1', '1', '1', '1', '1'}};
  41.  
  42.  
  43.  
  44.         int borderY = mapNew.length;    // y-8
  45.         int borderX = mapNew[0].length; // x-9
  46.  
  47.         try {
  48.             // opening database connection to MySQL server
  49.             con = DriverManager.getConnection(url, user, password);
  50.             // getting Statement object to execute query
  51.             stmt = con.createStatement();
  52.             // executing drop query
  53.             stmt.executeUpdate(queryDropTable);
  54.             // executing create query
  55.             stmt.executeUpdate(queryCreateTable);
  56.             // executing insert queries
  57.             for (int i = 0; i <  borderX; i++) {
  58.                 for (int j = 0; j < borderY; j++) {
  59.                     try {
  60.                         String queryInsertData = "INSERT INTO mydb.mapnumberone (id, x, y, value) VALUES (" + (j*i+j) + ", " + i + ", " + j + ", " + mapNew[j][i] + ");";
  61.                         stmt.executeUpdate(queryInsertData);
  62.                     } catch (Exception e) {
  63.  
  64.                     }
  65.                 }
  66.             }
  67.         } catch (SQLException sqlEx) {
  68.             sqlEx.printStackTrace();
  69.         } finally {
  70.             //close connection ,stmt and resultset here
  71.             try { con.close(); } catch(SQLException se) { /*can't do anything */ }
  72.             try { stmt.close(); } catch(SQLException se) { /*can't do anything */ }
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement