【问题标题】:Java Eclipse MySQLSyntaxErrorExceptionJava Eclipse MySQLSyntaxErrorException
【发布时间】:2013-12-02 13:47:25
【问题描述】:

我正在尝试从我的数据库中获取一些汽车列表,但不知何故我不断收到此错误com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'Make' in 'field list'我花了足够多的时间来解决这个问题,所以我试图在这里找到答案。

适配器类:

import java.sql.SQLException;

public class DatabaseAdapter2
{
   public CarList getAllCars()
   {
      MyDatabase myDB = new MyDatabase();
      CarList cars = new CarList();
      try
      {
         myDB.openMySQLDatabase("db", "root", "");

         String sql = "SELECT Make, Model, LicenseNumber, Color, Year," +
                "HorsePower, TimeUntilService, ConsumptionPerKm," +
                "NumberOfSeats, NumberOfDoors, Transmission, ClimateControl, Price"
               + "FROM cars";

         Object[][] result = myDB.returnSQLQueryResult(sql);

         for (int i = 0; i < result.length; i++)
         {
            for (int j = 0; j < result.length; j++)
            {
               String make = (String) result[i][j];
               String model = (String) result[i][j];
               String licenseNumber = (String) result[i][j];
               String color = (String) result[i][j];
               int year = (int) result[i][j];
               String horsePower = (String) result[i][j];
               String timeUntilService = (String) result[i][j];
               String consumptionPerKm = (String) result[i][j];
               int numberOfSeats = (int) result[i][j];
               int numberOfDoors = (int) result[i][j];
               String transmission = (String) result[i][j];
               String climateControl = (String) result[i][j];
               int price = (int) result[i][j];

               cars.add(new Car(make, model, licenseNumber, color, year,
                     horsePower, timeUntilService, consumptionPerKm,
                     numberOfSeats, numberOfDoors, transmission,
                     climateControl, price));

            }
         }
      }
      catch (SQLException e)
      {
         e.printStackTrace();
      }
      catch (ClassNotFoundException e)
      {
         e.printStackTrace();
      }
      finally
      {
         try
         {
            myDB.closeDatabase();
         }
         catch (SQLException e)
         {
            e.printStackTrace();
         }
      }
      return cars;
   }
}

数据库本身:http://imgur.com/2L3mY6a

【问题讨论】:

    标签: java mysql eclipse


    【解决方案1】:

    您忘记了 FROM 之前的空格:

    String sql = "SELECT Make, Model, LicenseNumber, Color, Year," +
                    "HorsePower, TimeUntilService, ConsumptionPerKm," +
                    "NumberOfSeats, NumberOfDoors, Transmission, ClimateControl, Price "
                   + "FROM cars";
    

    编辑: 对于您的 ClassCastException,当您想将 int 转换为字符串时,请使用 String.valueof(yourInt),更多信息在这里:http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    【讨论】:

    • 哈,愚蠢的错误。非常感谢,虽然我遇到了另一个错误。我将所有变量更改为字符串。而且我仍然收到一个错误:线程“main”中的异常 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String at DatabaseAdapter2.getAllCars(DatabaseAdapter2.java:24) at Test.main(Test .java:10)
    • 如果您认为相关,您能否使用完整的堆栈跟踪和您的汽车类别更新您的帖子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 2012-12-28
    • 2018-04-25
    • 1970-01-01
    • 2012-04-20
    • 2016-08-13
    • 1970-01-01
    相关资源
    最近更新 更多