【问题标题】:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException UNKNOWN COLUMNcom.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException UNKNOWN COLUMN
【发布时间】:2015-01-22 16:30:22
【问题描述】:

我目前正在尝试扫描和解析非 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 what USA 2 Alex cheese 1111 what@yahoo.com 用户 adm street zip what USA

所以这是我现在使用 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];
            }
        }

【问题讨论】:

  • 如果您使用PreparedStatement 并一个一个地设置每个参数而不是连接这些值并尝试在没有适当转义的情况下传递所有值会更好,例如what != 'what'.
  • @LuiggiMendoza 我尝试使用preparedstatement,但仍然遇到一些错误。我在网上查找了 preparestatement 信息,但它并没有真正帮助我。
  • 您的新例外是什么?提供有关它的详细信息
  • @LuiggiMendoza “字段列表”中的未知列“IDQ”。这是我得到的例外。
  • 嗯,异常信息非常具有描述性。确保您的表中有一个名为 IDQ 的列,并且您在其上使用了正确的拼写。

标签: java mysql jdbc java.util.scanner


【解决方案1】:

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

【讨论】:

  • 这不是必需的,仅在某些极端情况下需要,例如使用关键字作为列名。
猜你喜欢
  • 1970-01-01
  • 2016-09-18
  • 2017-09-10
  • 2012-04-20
  • 2013-11-20
  • 2016-07-07
  • 2018-04-26
  • 2012-07-20
  • 2011-12-15
相关资源
最近更新 更多