0

I'm trying to get some car lists from my database ,but somehow I keep getting this error com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'Make' in 'field list'and I spent more then enough time figuring it out so I'm trying to find an answer here.

Adapter class:

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;
   }
}

The Database itself: https://i.sstatic.net/AYGqY.jpg

0

1 Answer 1

5

You have forgotten a white space before the FROM :

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

Edit: For your ClassCastException, when you want to convert an int to string, use String.valueof(yourInt), more information here: http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

Sign up to request clarification or add additional context in comments.

2 Comments

Ha, silly mistake. Thanks a lot, although I'm getting another error. I changed all the variables to String. And i still get a error :Exception in thread "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)
can you update your post with full stacktrace and your car class if you think that's relevant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.