コード例 #1
0
  public static void importCsvToDb(File file) throws ClassNotFoundException {
    if (file.getName().matches("^.*.bak*$")) {
      if (DatabaseUtils.checkIfDatabaseExists() == true) {
        int dialogResult =
            JOptionPane.showConfirmDialog(null, "Replace all records'Yes' Or merge all'No'?");
        if (dialogResult == JOptionPane.YES_OPTION) {
          try {
            try {
              DataInputStream drr = new DataInputStream(new FileInputStream(file));
              BufferedReader dr =
                  new BufferedReader(
                      new InputStreamReader(
                          drr, "ISO-8859-7")); // to read greek chars iso-8859-7 needed
              String temp;
              String prs[] = new String[10];
              ArrayList pr = new ArrayList();
              int i = 1;
              while ((temp = dr.readLine()) != null) {
                if (temp.equals("promithiatablestart")) {
                  break;
                }
                if (i <= 9) {
                  prs[i] = temp;
                  i++;
                  if (i == 10) {
                    pr.add(
                        new Pelates(
                            prs[1], prs[2], prs[3], prs[4], prs[5], prs[6], prs[7], prs[8],
                            prs[9]));
                    i = 1;
                  }
                }
              }
              i = 1;
              while ((temp = dr.readLine()) != null) {
                if (temp.equals("recordsend")) {
                  break;
                }
                if (i <= 9) {
                  prs[i] = temp;
                  i++;
                  if (i == 10) {
                    pr.add(
                        new Promithia(
                            prs[1], prs[2], prs[3], prs[4], prs[5], prs[6], prs[7], prs[8],
                            prs[9]));
                    i = 1;
                  }
                }
              }
              dr.close();
              Class.forName(classForName);
              con = DriverManager.getConnection(url);
              sql = "delete   from pelates ";
              pst = con.prepareStatement(sql);
              pst.execute();
              sql = "delete  from promithia ";
              pst = con.prepareStatement(sql);
              pst.execute();
              sql =
                  "insert into pelates(eponimia, onomateponimo, tilefono, kinito, fax, address, email, taxkodikas, afm) values(?, ?, ?, ?, ?, ?, ?, ?, ?)";
              pst = con.prepareStatement(sql);
              for (Object pr1 : pr) {
                if (pr1 instanceof Pelates) {
                  pst.setString(1, ((Pelates) pr1).getEponimia());
                  pst.setString(2, ((Pelates) pr1).getOnomateponimo());
                  pst.setString(3, ((Pelates) pr1).getTilefono());
                  pst.setString(4, ((Pelates) pr1).getKinito());
                  pst.setString(5, ((Pelates) pr1).getFax());
                  pst.setString(6, ((Pelates) pr1).getAddress());
                  pst.setString(7, ((Pelates) pr1).getEmail());
                  pst.setString(8, ((Pelates) pr1).getTaxkodikas());
                  pst.setString(9, ((Pelates) pr1).getAfm());
                  pst.executeUpdate();
                }
              }
              sql =
                  "insert into promithia(eponimia, onomateponimo, tilefono, kinito, fax, address, email, taxkodikas, afm) values(?, ?, ?, ?, ?, ?, ?, ?, ?)";
              pst = con.prepareStatement(sql);
              for (Object pr1 : pr) {
                if (pr1 instanceof Promithia) {
                  pst.setString(1, ((Promithia) pr1).getEponimia());
                  pst.setString(2, ((Promithia) pr1).getOnomateponimo());
                  pst.setString(3, ((Promithia) pr1).getTilefono());
                  pst.setString(4, ((Promithia) pr1).getKinito());
                  pst.setString(5, ((Promithia) pr1).getFax());
                  pst.setString(6, ((Promithia) pr1).getAddress());
                  pst.setString(7, ((Promithia) pr1).getEmail());
                  pst.setString(8, ((Promithia) pr1).getTaxkodikas());
                  pst.setString(9, ((Promithia) pr1).getAfm());
                  pst.executeUpdate();
                }
              }

            } catch (IOException ex) {
            }
          } catch (SQLException ex) {
            Logger.getLogger(ImportToDb.class.getName()).log(Level.SEVERE, null, ex);
          } finally {
            try {
              if (pst != null) {
                pst.close();
              }
              if (con != null) {
                con.close();
              }
              sql = " ";
            } catch (SQLException ex) {

            }
          }
        } else if (dialogResult == JOptionPane.NO_OPTION) {

          try {
            try {
              DataInputStream drr = new DataInputStream(new FileInputStream(file));
              BufferedReader dr =
                  new BufferedReader(
                      new InputStreamReader(
                          drr, "ISO-8859-7")); // to read greek chars iso-8859-7 needed
              String temp;
              String prs[] = new String[10];
              ArrayList pr = new ArrayList();
              int i = 1;
              while ((temp = dr.readLine()) != null) {
                if (temp.equals("promithiatablestart")) {
                  break;
                }
                if (i <= 9) {
                  prs[i] = temp;
                  i++;
                  if (i == 10) {
                    pr.add(
                        new Pelates(
                            prs[1], prs[2], prs[3], prs[4], prs[5], prs[6], prs[7], prs[8],
                            prs[9]));
                    i = 1;
                  }
                }
              }
              i = 1;
              while ((temp = dr.readLine()) != null) {
                if (temp.equals("recordsend")) {
                  break;
                }
                if (i <= 9) {
                  prs[i] = temp;
                  i++;
                  if (i == 10) {
                    pr.add(
                        new Promithia(
                            prs[1], prs[2], prs[3], prs[4], prs[5], prs[6], prs[7], prs[8],
                            prs[9]));
                    i = 1;
                  }
                }
              }
              dr.close();

              Class.forName(classForName);
              con = DriverManager.getConnection(url);
              sql =
                  "insert into pelates(eponimia, onomateponimo, tilefono, kinito, fax, address, email, taxkodikas, afm) values(?, ?, ?, ?, ?, ?, ?, ?, ?)";
              pst = con.prepareStatement(sql);

              for (Object pr1 : pr) {
                if (pr1 instanceof Pelates) {
                  pst.setString(1, ((Pelates) pr1).getEponimia());
                  pst.setString(2, ((Pelates) pr1).getOnomateponimo());
                  pst.setString(3, ((Pelates) pr1).getTilefono());
                  pst.setString(4, ((Pelates) pr1).getKinito());
                  pst.setString(5, ((Pelates) pr1).getFax());
                  pst.setString(6, ((Pelates) pr1).getAddress());
                  pst.setString(7, ((Pelates) pr1).getEmail());
                  pst.setString(8, ((Pelates) pr1).getTaxkodikas());
                  pst.setString(9, ((Pelates) pr1).getAfm());
                  pst.executeUpdate();
                }
              }
              sql =
                  "insert into promithia(eponimia, onomateponimo, tilefono, kinito, fax, address, email, taxkodikas, afm) values(?, ?, ?, ?, ?, ?, ?, ?, ?)";
              pst = con.prepareStatement(sql);
              for (Object pr1 : pr) {
                if (pr1 instanceof Promithia) {
                  pst.setString(1, ((Promithia) pr1).getEponimia());
                  pst.setString(2, ((Promithia) pr1).getOnomateponimo());
                  pst.setString(3, ((Promithia) pr1).getTilefono());
                  pst.setString(4, ((Promithia) pr1).getKinito());
                  pst.setString(5, ((Promithia) pr1).getFax());
                  pst.setString(6, ((Promithia) pr1).getAddress());
                  pst.setString(7, ((Promithia) pr1).getEmail());
                  pst.setString(8, ((Promithia) pr1).getTaxkodikas());
                  pst.setString(9, ((Promithia) pr1).getAfm());
                  pst.executeUpdate();
                }
              }

            } catch (IOException ex) {
            }

          } catch (SQLException ex) {
            Logger.getLogger(ImportToDb.class.getName()).log(Level.SEVERE, null, ex);
          } finally {
            try {
              if (pst != null) {
                pst.close();
              }
              if (con != null) {
                con.close();
              }
              sql = " ";
            } catch (SQLException ex) {

            }
          }
        }
      }

    } else {
      JOptionPane.showMessageDialog(null, " *Αρχειο bak μονο");
    }
  }