我有sqlite3 DB和2个表:cars和naprawy。当我从汽车表中检索数据时,一切都很好。但是,当我尝试从naprawy查询数据时,出现以下错误:[SQLITE_ERROR] SQL错误或缺少数据库(没有这样的表:naprawy)。 在数据库浏览器中,我可以从naprawy查询数据,但是在应用程序中,总是使我感到异常。
对于连接数据库和查询数据,我使用此类:
package CompanyCars.MainWindow.DataSource;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class DataSource {
public static final String TABLE_CARS = "cars";
public static final String DB_NAME = "companyCars";
public static final String CARS_COLUMN_NRREJ = "nrrej";
public static final String CARS_COLUMN_MARKA = "marka";
public static final String CARS_COLUMN_MODEL = "model";
public static final String CARS_COLUMN_KOLOR = "kolor";
public static final String CARS_COLUMN_MIASTO = "miasto";
public static final String CARS_COLUMN_UZYTKOWNIK = "uzytkownik";
public static final String CARS_COLUMN_KARTA_PALIWOWA = "kartapaliwowa";
public static final String CARS_COLUMN_DATA_PRZEGLADU = "dataprzegladu";
public static final String CARS_COLUMN_OLEJ_PRZEGIEG = "olejprzebieg";
public static final String CARS_COLUMN_PRZEBIEG_OBECNIE = "przebiegobecnie";
public static final String CARS_COLUMN_PALIWO= "paliwo";
public static final String CARS_COLUMN_BUTLA_WAZNOSC = "butlawaznosc";
public static final String CARS_COLUMN_PRZEBIEG_PRZEGLAD_GAZU = "przebiegprzegladgazu";
public static final String CARS_COLUMN_DATA_PIERWSZEJ_REJESTRACJI = "datapierwszejrejestracji";
public static final String CARS_COLUMN_UBEZPIECZYCIEL = "ubezpieczyciel";
public static final String CARS_COLUMN_OCDATA = "ocdata";
public static final String CARS_COLUMN_OCFIRMA = "ocfirma";
public static final String CARS_COLUMN_ACFIRMA = "acfirma";
public static final String CARS_COLUMN_AUTOPOMOC = "autopomoc";
public static final String CARS_COLUMN_VAT_1 = "vat1";
public static final String CARS_COLUMN_OPONY_LETNIE = "oponyletnie";
public static final String CARS_COLUMN_OPONY_ZIMOWE = "oponyzimowe";
public static final String CARS_COLUMN_OPONY_LOKALIZACJA = "oponylokalizacja";
public static final String CARS_COLUMN_WYPOSAZENIE = "wyposazenie";
public static final String CARS_COLUMN_INNE = "inne";
public static final String CARS_COLUMN_KSYWKA = "ksywka";
public static final String QUERY_ALL_CARS = "select * from cars" ;
public static final String UPDATE_ITEM = "UPDATE " + TABLE_CARS + " SET ";
public static final String INSERT_INTO_CAR = "INSERT INTO " + TABLE_CARS + " VALUES (";
public static final String TABLE_NAPRAWY = "naprawy";
public static final String NAPRAWY_COLUMN_IDNAPRAWY = "IDnaprawy";
public static final String NAPRAWY_COLUMN_DATA = "data";
public static final String NAPRAWY_COLUMN_MECHANIK = "mechanik";
public static final String NAPRAWY_COLUMN_CO = "co";
public static final String NAPRAWY_COLUMN_NETTO = "netto";
public static final String NAPRAWY_COLUMN_BRUTTO = "brutto";
public static final String QUERY_ALL_REPAIRS = "SELECT * FROM \"" + TABLE_NAPRAWY + "\" WHERE nrrej =\"";
public static final String NAPRAWY_COLUMN_NRREJ = "nrrej";
List<Car> carList = new ArrayList<>();
public static final String CONNECTION_STRING = "jdbc:sqlite:" + DB_NAME;
private Connection connection;
private static DataSource instance = new DataSource();
private DataSource() {
}
public static DataSource getInstance(){
return instance;
}
public boolean open(){
try {
connection = DriverManager.getConnection(CONNECTION_STRING);
return true;
} catch (SQLException e){
System.out.println("Nie można połączyć się z bazą danych");
return false;
}
}
public void close(){
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e){
System.out.println("Wystąpił błąd");
}
}
//That method works fine
public ObservableList<Car> queryAllCars(){
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(QUERY_ALL_CARS)){
while(resultSet.next()){
Car car = new Car();
car.setNrRej(resultSet.getString(CARS_COLUMN_NRREJ));
car.setMarka(resultSet.getString(CARS_COLUMN_MARKA));
car.setModel(resultSet.getString(CARS_COLUMN_MODEL));
car.setKolor(resultSet.getString(CARS_COLUMN_KOLOR));
car.setMiasto(resultSet.getString(CARS_COLUMN_MIASTO));
car.setUzytkownik(resultSet.getString(CARS_COLUMN_UZYTKOWNIK));
car.setKartaPaliwowa((resultSet.getInt(CARS_COLUMN_KARTA_PALIWOWA)==1));
car.setDataPrzegladu(resultSet.getString(CARS_COLUMN_DATA_PRZEGLADU));
car.setOlejPrzebieg(resultSet.getInt(CARS_COLUMN_OLEJ_PRZEGIEG));
car.setPrzebiegObecnie(resultSet.getInt(CARS_COLUMN_PRZEBIEG_OBECNIE));
car.setPaliwo(resultSet.getString(CARS_COLUMN_PALIWO));
car.setButlaWaznosc(resultSet.getString(CARS_COLUMN_BUTLA_WAZNOSC));
car.setPrzebiegPrzegladGazu(resultSet.getInt(CARS_COLUMN_PRZEBIEG_PRZEGLAD_GAZU));
car.setDataPierwszejRejestracji(resultSet.getString(CARS_COLUMN_DATA_PIERWSZEJ_REJESTRACJI));
car.setUbezpieczyciel(resultSet.getString(CARS_COLUMN_UBEZPIECZYCIEL));
car.setOCData(resultSet.getString(CARS_COLUMN_OCDATA));
car.setOCFirma(resultSet.getString(CARS_COLUMN_OCFIRMA));
car.setACFirma(resultSet.getString(CARS_COLUMN_ACFIRMA));
car.setAutoPomoc(resultSet.getString(CARS_COLUMN_AUTOPOMOC));
car.setVAT_1((resultSet.getInt(CARS_COLUMN_VAT_1)==1));
car.setOponyLetnie(resultSet.getString(CARS_COLUMN_OPONY_LETNIE));
car.setOponyZimowe(resultSet.getString(CARS_COLUMN_OPONY_ZIMOWE));
car.setLokalizacjaOpon(resultSet.getString(CARS_COLUMN_OPONY_LOKALIZACJA));
car.setWyposazenie(resultSet.getString(CARS_COLUMN_WYPOSAZENIE));
car.setInne(resultSet.getString(CARS_COLUMN_INNE));
car.setKsywka(resultSet.getString(CARS_COLUMN_KSYWKA));
carList.add(car);
}
return FXCollections.observableArrayList(carList);
} catch (SQLException e){
System.out.println("Blad queryAllCars");
return null;
}
}
//This method keeps throwing exception, but the above method works fine
public ObservableList<Repair> queryAllRepairs(String nrRej){
try(Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select * FROM \"naprawy\"")){
List<Repair> repairs= new ArrayList<>();
while(resultSet.next()){
Repair repair = new Repair();
repair.setNrRej(resultSet.getString(NAPRAWY_COLUMN_NRREJ));
repair.setIDnaprawy(resultSet.getInt(NAPRAWY_COLUMN_IDNAPRAWY));
repair.setData(resultSet.getString(NAPRAWY_COLUMN_DATA));
repair.setMechanik(resultSet.getString(NAPRAWY_COLUMN_MECHANIK));
repair.setCo(resultSet.getString(NAPRAWY_COLUMN_CO));
repair.setNetto(resultSet.getDouble(NAPRAWY_COLUMN_NETTO));
repair.setBrutto(resultSet.getDouble(NAPRAWY_COLUMN_BRUTTO));
repairs.add(repair);
}
return FXCollections.observableArrayList(repairs);
}catch (SQLException e){
System.out.println("Nie udało się pobrać napraw samachodu: " + e.getMessage());
e.printStackTrace();
return null;
}
}
}
应用程序正确地与DB连接,因为我还有其他方法可以从db更新,插入和删除数据,并且它们工作正常。 我正在使用Singleton类DataSource。