com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException UNKNOWN COLUMN

时间:2014-11-24 20:35:01

标签: java mysql jdbc java.util.scanner

我目前正在尝试扫描和解析不是sql格式的文件。我试图将所有数据输入到SQL表中,但由于某些原因,每次运行程序时,我都会收到错误消息,说明“字段列表”中的未知列“什么”。所以这两个数据都没有通过。 'what'是文本中的名字之一。该表目前有11列。我知道我正在解析或扫描错误,但我无法弄清楚在哪里。这是我的代码:

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 hello cheese 1111 what@yahoo.com用户adm street zip美国什么的 2 Alex cheese 1111 what@yahoo.com用户adm street 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];
            }
        }

1 个答案:

答案 0 :(得分:0)

当您使用MySQL时,您需要用引号将文本输入括起来。尝试将您插入的String值括在引号中,然后执行代码。