Java Eclipse MySQLSyntaxErrorException

时间:2013-12-02 13:47:25

标签: java mysql eclipse

我正在尝试从我的数据库中获取一些汽车列表,但不知怎的,我不断收到此错误com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'Make' in 'field list'并且我花了更多时间来搞清楚,所以我试图在这里找到答案。< / p>

适配器类:

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

1 个答案:

答案 0 :(得分:5)

您在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