“'字段列表中的未知列' MySQLSyntaxErrorException

时间:2014-10-14 20:35:35

标签: java mysql jdbc

我正在尝试填充远程数据库但我目前收到此错误,而且我不确定如何解决此问题。错误是指我试图放入表格的第一个值。我处理了no eventID不会传递到eventID表的事件。

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column '0654fac7' in 'field list'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)

这是我的代码:

protected void loadToDatabase() {
    String dbURL = "jdbc:mysql://remotehost/database";
    String usr = "username";
    String password = "";
    try {
        Connection Conn = DriverManager.getConnection(dbURL, usr, password);
        Statement stmt = Conn.createStatement();
        for (String i : eventIDs) {
            String sql = "INSERT INTO eventID VALUES (" + i + ");";
            stmt.executeUpdate(sql);
        }
        System.out.println("Insert complete");
    } catch (Exception e ) {
        e.printStackTrace();
    }
}

这是我试图传递到数据库的数据:

Test Case 0: 0654fac7-7aa8-4c55-bdaf-77b39692aa3f-1413318357238
Test Case 1: f9914577-c60d-49ae-bec6-4db0463676fd-1413318358598
Test Case 2: 6f1b6e4e-9d51-411b-8248-2cd9e1ef2712-1413318360005
Test Case 3: 330860b3-f22b-4b84-9837-bb949c2659ca-1413318361411
Test Case 4: no eventID
Test Case 5: e09f77f3-d816-49ab-90cd-4df56d8c8ef2-1413318366577
Test Case 6: f580e84e-7a3d-4693-bbea-454c5d699070-1413318367992
Test Case 7: no eventID

我的数据库只包含一列。

mysql> show tables in eventID;
+-------------------+
| Tables_in_eventID |
+-------------------+
| eventID           |
+-------------------+

2 个答案:

答案 0 :(得分:3)

所以我犯的错误就是这行代码

String sql = "INSERT INTO eventID VALUES (" + i + ");";

正确的功能代码,因为我传递的字符串是:

String sql = "INSERT INTO eventID VALUES (" + "\"" + i + "\"" + ");";

我需要引号!

答案 1 :(得分:1)

我有一个litle代码,分享。

这是我的示例表 ++++++++++ ganaderos ++++++++++ CodGanadero NomGanadero ApeGanadero ++++++++++

        System.out.print("Ingrese codigo del Ganadero : ");
        CodigoGanadero= scn.nextLine();

        System.out.print("Ingrese Nombre del Ganadero : ");
        NombreGanadero=scn.nextLine();

        System.out.print("Ingrese Apellido del Ganadero : ");
        ApellidoGanadero=scn.nextLine();
        int rs2=0;
        rs2 = s2.executeUpdate ("insert into ganaderos (CodGanadero,NomGanadero,ApeGanadero) "
                + "values ("+"\""+CodigoGanadero+"\""+","+"\""+NombreGanadero+"\""+","+"\""+ApellidoGanadero+"\""+")");