2013-05-30 64 views
0

我想做一个程序,我会从txt文件中读取数据并将它们存储在表中。用户将给出文件的目录,然后创建具有特定字段的表格。从表txt文件存储数据mysql

然后我必须填写每个表与它的数据。有没有人可以帮助我如何做到这一点? .txt文件的数据与图中所示的数据相同:enter image description here
这两列由制表符分隔。 我曾尝试下面的代码,但我得到的错误:

public class notepad { 

    public static void main(String args[]) throws Exception { 

     Class.forName("com.mysql.jdbc.Driver"); 
     Connection con = (Connection) DriverManager.getConnection(
       "jdbc:mysql://localhost:3300/mydb", "root", "root"); 

     String dirpath = ""; 
     Scanner scanner1 = new Scanner(System.in); 
     while (true) { 
      System.out.println("Please give the directory:"); 
      dirpath = scanner1.nextLine(); 
      File fl = new File(dirpath); 
      if (fl.canRead()) 

       break; 
      System.out.println("Error:Directory does not exists"); 
     } 

     try { 
      String files; 
      File folder = new File(dirpath); 
      File[] listOfFiles = folder.listFiles(); 

      for (int i = 0; i < listOfFiles.length; i++) { 
       if (listOfFiles[i].isFile()) { 
        files = listOfFiles[i].getName(); 
        if (files.endsWith(".txt") || files.endsWith(".TXT")) { 
         List<File> txtFiles = new ArrayList<File>(); 
         txtFiles.add(listOfFiles[i]); 
         String[] parts = files.split("\\."); 
         String tablename = parts[0]; 
         for (File txtFile : txtFiles) { 
          List sheetData = new ArrayList(); 

          try { 
           FileReader in = new FileReader(txtFile); 
           BufferedReader br = new BufferedReader(in); 
           String line = br.readLine(); 
           while (line != null) { 
            System.out.println(line); 
            line = br.readLine(); 
           } 
           in.close(); 

          } catch (Exception e) { 
           System.err.println("Error: " + e.getMessage()); 
          } 
          showExcelData(sheetData); 
          getCreateTable1(con, tablename); 
          importData(con, txtFile); 
         } 
        } 
       } 
      } 

     } catch (Exception e) { 
      System.out.println(); 
     } 
    } 

    private static String getCreateTable1(Connection con, String tablename) { 

     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      Statement stmt = con.createStatement(); 
      String createtable = "CREATE TABLE " 
        + tablename 
        + " (text VARCHAR(255), price int)"; 
      System.out.println("Create a new table in the database"); 
      stmt.executeUpdate(createtable); 
     } catch (Exception e) { 
      System.out.println(((SQLException) e).getSQLState()); 
      System.out.println(e.getMessage()); 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    private static void importData(Connection con, File txtFile) { 

     Statement stmt; 
     String query; 
     try { 
      stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 
        ResultSet.CONCUR_UPDATABLE); 

      query = "LOAD DATA INFILE '" + txtFile 
        + "' INTO TABLE tablename (text,price);"; 

      stmt.executeUpdate(query); 

     } catch (Exception e) { 
      e.printStackTrace(); 
      stmt = null; 
     } 

    } 

谁能帮助我,我会怎么做:

java.sql.SQLException:at part1.importData(part1.java:31) 
    at part1.main(part1.java:16) 

,我已经尝试的代码是?我的困难是当我想导入数据时。

+0

请编辑您的文章。 SQL查询中存在特定于SQL的错误。它与Java或JDBC无关。远程java代码,只留下查询并重新发布到MySQL。首先尝试从mysql控制台调用命令以获取更精确的SQL错误代码。 –

回答

1

的问题是,你试图通过文件的查询,而据我所知,应该是文件的完整路径,如:

'C:\temp\data1.txt'

所以在此功能,您应该使用:

private static void importData(Connection con, File txtFile) { 

     query = "LOAD DATA INFILE '" + txtFile.getAbsolutePath() 
       + "' INTO TABLE tablename (text,price);"; 

,请注意,这只适用于本地主机,对于远程服务器,最好在批量插入时这样做,这意味着您需要重写整个代码。

+0

我更新了我的答案,所以.getAbsolutePath()可能对您有用。 –