Java程序不会将.csv文件插入到mysql中,但不会抛出任何错误

时间:2017-09-15 06:42:25

标签: java mysql csv jdbc

这将是一个很长的问题,比标题所暗示的要复杂得多,所以请耐心等待。 我有一个程序,它执行三个步骤:

  • 步骤1)以固定间隔检查电子邮件服务器是否有带附件的新电子邮件,并将附件下载到指定目录

  • 步骤2)检查目录(我使用Java的WatchService api)进行文件更改

  • 步骤3)检测到下载的新文件后,继续将文件内容导入MySQL数据库表。

我有三个类和一个主类(执行并调用三个)来处理这个:

1)JobStarter(Main class开始步骤1)到3))

2)MailMonitor ---->详细信息步骤1)检查邮件和下载附件

3)DBConnection ---->用于指定与数据库的Java连接的小类

4)DirectoryChecker ---->细节步骤2)和3)

现在问题是:没有异常或错误被抛出。该程序运行步骤1)和2)完全正常,但似乎似乎无法执行步骤3),即使它打印出终端中的“成功导入”语句。

我将按如下方式说明:

New path created: uploadtest.csv
LOAD DATA LOCAL INFILE 'C:/Users/User/Desktop/Test/uploadtest.csv' INTO 
TABLE txn_tbl FIELDS TERMINATED BY ',' LINES TERMINATED BY '
'  IGNORE 1 LINES(txn_amount, card_number, terminal_id)
Data import success
New path modified: uploadtest.csv

终端上的输出显示已执行步骤3),但是当我用SELECT * FROM txn_tbl;检查数据库表时,实际上没有任何内容插入到表中。

我决定尝试隔离处理目录检查和文件导入功能的类,并通过键入内部主方法来查看它是否仍然有效,然后单独运行它。如果我运行它并手动将新文件粘贴/插入目录,它将成功插入数据库。因此我的困惑。我详细介绍了以下源代码:

1)JobStarter代码:

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import java.io.File;
import java.io.IOException;


public class JobStarter {

    public static void main(String[] agrs) throws SchedulerException, IOException, InterruptedException {

        //Define a job and tie it to our job classes

        JobDetail job=JobBuilder.newJob(MonitorMail.class).build();
        //Trigger t1=TriggerBuilder.newTrigger().withIdentity("SimpleTrigger").startNow().build();

        Trigger t1=TriggerBuilder.newTrigger().withIdentity("CronTrigger").withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(05).repeatForever()).build();
        Scheduler sc=StdSchedulerFactory.getDefaultScheduler();

        sc.start();
        sc.scheduleJob(job, t1);
        File dir = new File("C:\\Users\\User\\Desktop\\Test");
        MainWatch.watchDirectoryPath(dir.toPath());
    }

}

2)MailMonitor:

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class MailMonitor implements Job{

    private Properties getServerProperties(String protocol, String host, String port) {
        Properties properties = new Properties();

        //Server
        properties.put(String.format("mail.%s.host", protocol), host);
        properties.put(String.format("mail.%s.port",protocol), port);

        //SSL
        properties.setProperty(String.format("mail.%s.socketFactory.class", protocol), "javax.net.ssl.SSLSocketFactory");
        properties.setProperty(String.format("mail.%s.socketFactory.fallback", protocol), "false");
        properties.setProperty(String.format("mail.%s.socketFactory.port",protocol), String.valueOf(port));

        return properties;
   }

   //Get the new Unread Mail
   public void getNewEmails(String protocol, String host,
      String port, String userName, String password) throws IOException {Properties properties = getServerProperties(protocol,host, port);
      Session session = Session.getDefaultInstance(properties);

      try {
          Store store = session.getStore(protocol);
          store.connect(userName, password);

          Folder inbox = store.getFolder("INBOX");
          inbox.open(Folder.READ_WRITE);
          int count = inbox.getMessageCount();
          Message[] messages = inbox.getMessages(1, count);
          for (Message message : messages) {
              if (!message.getFlags().contains(Flags.Flag.SEEN)) {
                  Address[] fromAddresses = message.getFrom();
                  System.out.println("...................");
                  System.out.println("\t From: "+ fromAddresses[0].toString());
                  System.out.println("\t To: "+ parseAddresses(message.getRecipients(RecipientType.TO)));
                  System.out.println("\t CC: "+ parseAddresses(message.getRecipients(RecipientType.CC)));
                  System.out.println("\t Subject: "+ message.getSubject());
                  System.out.println("\t Sent Date:"+ message.getSentDate().toString());

                  String contentType = message.getContentType();
                  String messageContent = "";

                  // store attachment file name, separated by comma
                  String attachFiles = "";

                  if (contentType.contains("multipart")) {
                      // content may contain attachments
                      Multipart multiPart = (Multipart) message.getContent();
                      int numberOfParts = multiPart.getCount();
                      for (int partCount = 0; partCount < numberOfParts; partCount++) {
                          MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
                       if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
                              // this part is attachment
                              String fileName = part.getFileName();
                              attachFiles += fileName + ", ";
                              part.saveFile("/Users/User/Desktop/Test" + File.separator + fileName);
                       } 

                       else {
                           // this part may be the message content
                           messageContent = part.getContent().toString();
                       }
                   }

                   if (attachFiles.length() > 1) {
                       attachFiles = attachFiles.substring(0, attachFiles.length() - 2);
                   }
               } 

               else if (contentType.contains("text/plain") || contentType.contains("text/html")) {
                   Object content = message.getContent();
                   if (content != null) {
                       messageContent = content.toString();
                   }
               }

               message.setFlag(Flags.Flag.SEEN,true);
               System.out.println("Message:"+messageContent);

               try {
                   System.out.println(message.getContent().toString());
               } 

               catch (Exception ex) {
                   System.out.println("Error reading content!!");
                   ex.printStackTrace();
               }

            }

            else {

            }// end of if-else statement

         }// end of for-loop

         System.out.println(new Date());
         inbox.close(false);
         store.close();
      } catch (NoSuchProviderException ex) {
         System.out.println("No provider for protocol: "
            + protocol);
         ex.printStackTrace();
      } catch (MessagingException ex) {
         System.out.println("Could not connect to the message store");
         ex.printStackTrace();
      }
   }

   private String parseAddresses(Address[] address) {

      String listOfAddress = "";
      if ((address == null) || (address.length < 1))
         return null;
      if (!(address[0] instanceof InternetAddress))
         return null;

      for (int i = 0; i < address.length; i++) {
         InternetAddress internetAddress =
            (InternetAddress) address[0];
         listOfAddress += internetAddress.getAddress()+",";
      }
      return listOfAddress;
   }

   public void execute(JobExecutionContext arg0) throws JobExecutionException{

       MailMonitor monitor=new MailMonitor();
       try {
           monitor.getNewEmails("imap", "imap.gmail.com", "993","testserve@gmail.com", "rootpassword");
       } 

       catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }

    }

3)数据库连接

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class DBConnection {

    public static Connection getConnection() throws SQLException, ClassNotFoundException {

        //Class.forName("com.mysql.jdbc.Driver");

        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jcg?useSSL=false", "root", "password");

        return connection;
    }

    public static void main(String[] args) {

        try {
            getConnection();
        }

        catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

4)DirectoryChecker

import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.OVERFLOW;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.WatchEvent;
import java.nio.file.WatchEvent.Kind;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.sql.Connection;

import java.sql.Statement;

public class DirectoryChecker {

    public static void watchDirectoryPath(Path path) {
        // Sanity check - Check if path is a folder
        try {
            Boolean isFolder = (Boolean) Files.getAttribute(path,
                    "basic:isDirectory", NOFOLLOW_LINKS);
            if (!isFolder) {
                throw new IllegalArgumentException("Path: " + path
                        + " is not a folder");
            }
        } 

        catch (IOException ioe) {
            // Folder does not exists
            ioe.printStackTrace();
        }

        System.out.println("Watching path: " + path);

        // We obtain the file system of the Path
        FileSystem fs = path.getFileSystem();

        // We create the new WatchService using the new try() block
        try (WatchService service = fs.newWatchService()) {

            // We register the path to the service
            // We watch for creation events
            path.register(service, ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE);

            // Start the infinite polling loop
            WatchKey key = null;
            while (true) {
                key = service.take();

                // Dequeueing events
                Kind<?> kind = null;
                for (WatchEvent<?> watchEvent : key.pollEvents()) {
                    // Get the type of the event
                    kind = watchEvent.kind();
                    if (OVERFLOW == kind) {
                        continue; // loop
                    } else if (ENTRY_CREATE == kind) {
                        // A new Path was created
                        Path newPath = ((WatchEvent<Path>) watchEvent)
                                .context();
                        // Output
                        System.out.println("New path created: " + newPath);

                        String newPathfinal = newPath.toString();
                        try (Connection connection = DBConnection.getConnection()) {


                            String file = "C:/Users/User/Desktop/Test/" + newPathfinal;

                            String loadQuery = "LOAD DATA LOCAL INFILE '" + file + "' INTO TABLE txn_tbl FIELDS TERMINATED BY ','"
                                    + " LINES TERMINATED BY '\n'  " + "IGNORE 1 LINES(txn_amount, card_number, terminal_id)";
                            System.out.println(loadQuery);
                            Statement stmt = connection.createStatement();
                            stmt.execute(loadQuery);
                            System.out.println("Data import success");
                        } 

                        catch (Exception e) {
                            e.printStackTrace();
                        }

                    } 

                    else if (ENTRY_MODIFY == kind) {
                        // modified
                        Path newPath = ((WatchEvent<Path>) watchEvent)
                                .context();
                        // Output
                        System.out.println("New path modified: " + newPath);
                    } 

                    else if (ENTRY_DELETE == kind) {
                        Path newPath = ((WatchEvent<Path>) watchEvent)
                                .context();
                        System.out.println("New Path deleted: " + newPath);


                    }
                }

                if (!key.reset()) {
                    break; // loop
                }
            }

        } 

        catch (IOException ioe) {
            ioe.printStackTrace();
        } 

        catch (InterruptedException ie) {
            ie.printStackTrace();
        }

    }

}

对此有任何建议或帮助,我们非常感谢!

0 个答案:

没有答案