2014-11-24 110 views
0

我目前正在尝试扫描并解析非sql格式的文件。我试图将所有数据输入到SQL表中,但由于某种原因,每当我运行该程序时,我都会收到错误消息,说明'字段列表中的未知列'是什么'。所以这两个数据都没有通过。 “什么”是文字上的名字之一。该表目前有11列。我知道我解析或扫描错了,但我不知道在哪里。这里是我的代码:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException UNKNOWN COLUMN

public class parseTable { 

public parseTable (String name) throws FileNotFoundException 
{ 
    File file = new File(name); 
    parse(file); 
} 

private void parse(File file) throws FileNotFoundException 
{ 
    Connection conn = null; 
    Statement stmt = null; 
    ResultSet rs = null; 

    try{ 
    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    String connectionUrl = "jdbc:mysql://localhost:3306/"; 
    String connectionUser = ""; 
    String connectionPassword = ""; 
    conn = DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword); 
    stmt = conn.createStatement(); 

    Scanner scan = new Scanner(file); 

    String[] rowInfo = new String[11]; 
    int count = 0; 
    while(scan.hasNextLine()){ 

     //String data = scan.nextLine(); 

     Scanner lineScan = new Scanner(scan.nextLine()); 

     while(lineScan.hasNext()){ 
      String words = lineScan.next(); 
      if(count < 11){ 
       rowInfo[count] = words; 
       count++; 
      } 


      else if(count == 11 && words.equals("States")){ 
       rowInfo[count - 1] = rowInfo[count - 1] + " " + words; 
      } 

      else{ 
       String query = ""; 
       for(int i = 0; i < rowInfo.length; i++) 
       { 
        if(query.equals("")) 
        { 
         query = rowInfo[i]; 
        } 
        else if(i == 9){ 
         query = query + "," + rowInfo[i]; 
        } 

        else if(rowInfo[i].equals(null)){ 
         query = query + ", " + "NULL"; 
        } 

        else 
         query = query + ", " + "'" + rowInfo[i] + "'"; 

       } 

       stmt.executeUpdate("INSERT INTO dup VALUES(" + query + ")"); 

       count = 0; 
       rowInfo = new String[11]; 
      } 
     } 



    } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); } 
     try { if (stmt != null) stmt.close(); } catch (SQLException e) { e.printStackTrace(); } 
     try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } 
    } 
} 

}

这是我想输入的数据: 1你好奶酪1111 [email protected]用户ADM街道邮编什么美国 2亚历奶酪1111 [email protected]用户adm街道zip什么美国

所以这是我现在使用PrepareStatement的新代码。然而,我仍然遇到了一个错误,我在网上寻找解决方案,在那里我犯了一个错误,但我似乎无法弄清楚在哪里。

String query = "INSERT INTO mil_table (UserName, NameFirst, NameLast, supportID, EmailAddress, Password, 
       IDQ, AddressCity, AddressState, AddressZip, AddressCountry) VALUES(?,?,?,?,?,?,?,?,?,?,?)"; 

    pstmt = conn.prepareStatement(query); 

    Scanner scan = new Scanner(file); 

    String[] rowInfo = new String[11]; 
    int count = 0; 
    while(scan.hasNextLine()){ 

     //String data = scan.nextLine(); 

     Scanner lineScan = new Scanner(scan.nextLine()); 

     while(lineScan.hasNext()){ 
      String words = lineScan.next(); 
      if(count < 11){ 
       rowInfo[count] = words; 
       count++; 
      } 


      else if(count == 11 && words.equals("States")){ 
       rowInfo[count - 1] = rowInfo[count - 1] + " " + words; 
      } 

      else{ 

       for(int i = 0; i <rowInfo.length; i++) 
       { 
        pstmt.setString(i + 1, rowInfo[i]); 

}

   //stmt.executeUpdate("INSERT INTO mil_table VALUES(" + query + ")"); 
       //System.out.println("@" + query + "@"); 
       pstmt.executeUpdate(); 
       count = 0; 
       rowInfo = new String[11]; 
      } 
     } 
+2

如果您使用'PreparedStatement'并逐个设置每个参数,而不是连接值并尝试在没有正确转义的情况下通过所有参数,则会更好。 '什么!='什么'。 – 2014-11-24 20:43:01

+0

@LuiggiMendoza我试图使用preparedstatement,但我仍然得到一些错误。我在网上查看预备信息,但它并没有真正帮助我。 – Kito 2014-11-24 21:45:51

+0

你有什么新的例外?提供关于它的详细信息 – 2014-11-24 21:48:22

回答

0

当你正在使用MySQL,则需要使用引号内的文字输入。尝试使用引号括住要插入的字符串值,然后执行代码。

+0

这不是必需的,只有在使用关键字作为列名的某些边角情况下才是必需的。 – 2014-11-25 08:06:24

相关问题