Advertisement
Guest User

Untitled

a guest
Apr 8th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.16 KB | None | 0 0
  1. package Java;
  2.  
  3.     //Putting into DB class
  4. import java.sql.*;
  5. import java.text.SimpleDateFormat;
  6.  
  7. public class Main {
  8.  
  9.     public static void main(String args[]) {
  10.        
  11.     }
  12.  
  13.     public static void DBcon(){
  14.             //Creating connection
  15.         String url = "jdbc:mysql://localhost:3306/Courses??autoReconnect=true&useSSL=false";
  16.         String user = "root";
  17.         String password = "root";
  18.         Connection con;
  19.         Statement stmt;
  20.         ResultSet resultSet;
  21.             //Creating Array
  22.         String[][] mass = new String[0][0];
  23.         mass = new HTMLparser().Parse(mass);
  24.         String parsedDate= ""; //Date from XML
  25.         String DateString = ""; //Date from DB
  26.         try {
  27.             //Formatting Date To sql.Date Format
  28.             String dateInString;
  29.             String initDateFormat = "dd.MM.yyyy";
  30.             String endDateFormat = "yyyy-MM-dd";
  31.             dateInString = mass[0][5];
  32.             java.util.Date date = new SimpleDateFormat(initDateFormat).parse(dateInString);
  33.             SimpleDateFormat formatter = new SimpleDateFormat(endDateFormat);
  34.             parsedDate = formatter.format(date);
  35.         }
  36.         catch (Exception e){
  37.  
  38.         }
  39.         try {
  40.                 //Connection and getting Dates from DB
  41.             Class.forName("com.mysql.jdbc.Driver");
  42.             con = DriverManager.getConnection(url, user, password);
  43.             stmt = con.createStatement();
  44.             resultSet = stmt.executeQuery("SELECT * FROM course;");
  45.             resultSet.last();
  46.             resultSet.getRow();
  47.             resultSet = stmt.executeQuery("SELECT Date FROM course;");
  48.             while (resultSet.next()){
  49.                 DateString = resultSet.getString("Date");
  50.             }
  51.             con.close();
  52.             stmt.close();
  53.             resultSet.close();
  54.         }
  55.         catch (SQLException e) {
  56.  
  57.         }
  58.         catch (Exception e){
  59.  
  60.         }
  61.                 //Putting DATA into DB
  62.             if(DateString.length()==0)
  63.                 DateString="0-0-0";
  64.             int x = Integer.parseInt(DateString.replace("-",""));
  65.             int y = Integer.parseInt(parsedDate.replace("-",""));
  66.             if (x!=y) //Date Comparison
  67.                 for (int i = 0; i < 33; i++) {
  68.                     try {
  69.                         String Num_id = mass[i][0];
  70.                         String Letter_id = mass[i][1];
  71.                         int Counting = Integer.parseInt(mass[i][2]);
  72.                         String Name = mass[i][3];
  73.                         Double Value = Double.parseDouble(mass[i][4].replace(",", "."));
  74.                         con = DriverManager.getConnection(url, user, password);
  75.                         stmt = con.createStatement();
  76.                             //SQL Query
  77.                         stmt.executeUpdate("INSERT INTO course (Num_id, Letter_id,Counting,Name,Value,Date)" + "VALUES ('" + Num_id + "', '" + Letter_id + "', '" + Counting + "', '" + Name + "', '" + Value + "', '" + parsedDate + "');");
  78.                         con.close();
  79.                         stmt.close();
  80.                     } catch (Exception e) {
  81.                         e.printStackTrace();
  82.                     }
  83.                 }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement