Commit 94a68ae6 by Patryk Czarnik

Formatowanie kodu IDEA

parent fd341077
package ogloszenia; package ogloszenia;
/** W tej klasie trzeba wpisać swoje ustawienia ścieżek. */ /**
* W tej klasie trzeba wpisać swoje ustawienia ścieżek.
*/
public class Ustawienia { public class Ustawienia {
public static final String SQLITE_PATH = "/home/patryk/ogloszenia/ogloszenia.db"; public static final String SQLITE_PATH = "/home/patryk/ogloszenia/ogloszenia.db";
......
...@@ -23,7 +23,7 @@ public class DostepDoBazySqlite implements AutoCloseable { ...@@ -23,7 +23,7 @@ public class DostepDoBazySqlite implements AutoCloseable {
Connection con = DriverManager.getConnection("jdbc:sqlite:" + sqliteFile); Connection con = DriverManager.getConnection("jdbc:sqlite:" + sqliteFile);
return new DostepDoBazySqlite(con); return new DostepDoBazySqlite(con);
} catch (Exception e) { } catch (Exception e) {
throw new BladBazyDanych("Błąd podczas otwierania bazy danych ("+e.getMessage()+")", e); throw new BladBazyDanych("Błąd podczas otwierania bazy danych (" + e.getMessage() + ")", e);
} }
} }
...@@ -34,7 +34,7 @@ public class DostepDoBazySqlite implements AutoCloseable { ...@@ -34,7 +34,7 @@ public class DostepDoBazySqlite implements AutoCloseable {
@Override @Override
public void close() { public void close() {
try { try {
if(c != null) if (c != null)
c.close(); c.close();
} catch (SQLException e) { } catch (SQLException e) {
System.err.println("Błąd podczas close: " + e.getMessage()); System.err.println("Błąd podczas close: " + e.getMessage());
...@@ -56,7 +56,7 @@ public class DostepDoBazySqlite implements AutoCloseable { ...@@ -56,7 +56,7 @@ public class DostepDoBazySqlite implements AutoCloseable {
public void endTransaction(boolean commit) throws BladBazyDanych { public void endTransaction(boolean commit) throws BladBazyDanych {
try { try {
if(commit) { if (commit) {
c.commit(); c.commit();
} else { } else {
c.rollback(); c.rollback();
......
...@@ -31,26 +31,26 @@ public class OgloszenieDAO { ...@@ -31,26 +31,26 @@ public class OgloszenieDAO {
public List<Integer> idList() throws BladBazyDanych { public List<Integer> idList() throws BladBazyDanych {
final String sql = "SELECT id_ogloszenia FROM ogloszenia ORDER BY id_ogloszenia"; final String sql = "SELECT id_ogloszenia FROM ogloszenia ORDER BY id_ogloszenia";
List<Integer> lista = new LinkedList<>(); List<Integer> lista = new LinkedList<>();
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
try(ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
while(rs.next()) { while (rs.next()) {
lista.add(rs.getInt(1)); lista.add(rs.getInt(1));
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new BladBazyDanych("listaIdOgloszen: "+e.getMessage(), e); throw new BladBazyDanych("listaIdOgloszen: " + e.getMessage(), e);
} }
return lista; return lista;
} }
public OgloszenieSamochodowe findById(int idOgloszenia, boolean full) throws BladBazyDanych, NieznanyRekord { public OgloszenieSamochodowe findById(int idOgloszenia, boolean full) throws BladBazyDanych, NieznanyRekord {
final String sql = "SELECT * FROM ogloszenia WHERE id_ogloszenia = ?"; final String sql = "SELECT * FROM ogloszenia WHERE id_ogloszenia = ?";
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, idOgloszenia); stmt.setInt(1, idOgloszenia);
try(ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
if(rs.next()) { if (rs.next()) {
OgloszenieSamochodowe ogl = ogloszenieZResultSet(rs); OgloszenieSamochodowe ogl = ogloszenieZResultSet(rs);
if(full) { if (full) {
doczytajSprzedawce(ogl); doczytajSprzedawce(ogl);
} }
return ogl; return ogl;
...@@ -59,7 +59,7 @@ public class OgloszenieDAO { ...@@ -59,7 +59,7 @@ public class OgloszenieDAO {
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new BladBazyDanych("Ogloszenie byIdFull: "+e.getMessage(), e); throw new BladBazyDanych("Ogloszenie byIdFull: " + e.getMessage(), e);
} }
} }
...@@ -74,11 +74,11 @@ public class OgloszenieDAO { ...@@ -74,11 +74,11 @@ public class OgloszenieDAO {
public List<OgloszenieSamochodowe> readAll(boolean full) throws BladBazyDanych { public List<OgloszenieSamochodowe> readAll(boolean full) throws BladBazyDanych {
final String sql = "SELECT * FROM ogloszenia"; final String sql = "SELECT * FROM ogloszenia";
List<OgloszenieSamochodowe> lista = new LinkedList<>(); List<OgloszenieSamochodowe> lista = new LinkedList<>();
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
try(ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
while(rs.next()) { while (rs.next()) {
OgloszenieSamochodowe ogl = ogloszenieZResultSet(rs); OgloszenieSamochodowe ogl = ogloszenieZResultSet(rs);
if(full) try { if (full) try {
doczytajSprzedawce(ogl); doczytajSprzedawce(ogl);
} catch (NieznanyRekord e) { } catch (NieznanyRekord e) {
System.err.println(e); System.err.println(e);
...@@ -87,7 +87,7 @@ public class OgloszenieDAO { ...@@ -87,7 +87,7 @@ public class OgloszenieDAO {
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new BladBazyDanych("Ogloszenie readAll: "+e.getMessage(), e); throw new BladBazyDanych("Ogloszenie readAll: " + e.getMessage(), e);
} }
return lista; return lista;
} }
...@@ -105,19 +105,19 @@ public class OgloszenieDAO { ...@@ -105,19 +105,19 @@ public class OgloszenieDAO {
+ " WHERE cena BETWEEN ? AND ?" + " WHERE cena BETWEEN ? AND ?"
+ " ORDER BY id_ogloszenia"; + " ORDER BY id_ogloszenia";
if(cenaOd == null) if (cenaOd == null)
cenaOd = BigDecimal.ZERO; cenaOd = BigDecimal.ZERO;
if(cenaDo == null) if (cenaDo == null)
cenaDo = new BigDecimal(100000000); cenaDo = new BigDecimal(100000000);
List<OgloszenieSamochodowe> lista = new LinkedList<>(); List<OgloszenieSamochodowe> lista = new LinkedList<>();
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setBigDecimal(1, cenaOd); stmt.setBigDecimal(1, cenaOd);
stmt.setBigDecimal(2, cenaDo); stmt.setBigDecimal(2, cenaDo);
try(ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
while(rs.next()) { while (rs.next()) {
OgloszenieSamochodowe ogl = ogloszenieZResultSet(rs); OgloszenieSamochodowe ogl = ogloszenieZResultSet(rs);
try { try {
doczytajSprzedawce(ogl); doczytajSprzedawce(ogl);
...@@ -137,15 +137,15 @@ public class OgloszenieDAO { ...@@ -137,15 +137,15 @@ public class OgloszenieDAO {
public List<OgloszenieSamochodowe> ogloszeniaSprzedawcy(int idSprzedawcy) throws BladBazyDanych { public List<OgloszenieSamochodowe> ogloszeniaSprzedawcy(int idSprzedawcy) throws BladBazyDanych {
final String sql = "SELECT * FROM ogloszenia WHERE id_sprzedawcy = ?"; final String sql = "SELECT * FROM ogloszenia WHERE id_sprzedawcy = ?";
List<OgloszenieSamochodowe> lista = new LinkedList<>(); List<OgloszenieSamochodowe> lista = new LinkedList<>();
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, idSprzedawcy); stmt.setInt(1, idSprzedawcy);
try(ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
while(rs.next()) { while (rs.next()) {
lista.add(ogloszenieZResultSet(rs)); lista.add(ogloszenieZResultSet(rs));
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new BladBazyDanych("ogloszeniaSprzedawcy: "+e.getMessage(), e); throw new BladBazyDanych("ogloszeniaSprzedawcy: " + e.getMessage(), e);
} }
return lista; return lista;
} }
...@@ -153,24 +153,24 @@ public class OgloszenieDAO { ...@@ -153,24 +153,24 @@ public class OgloszenieDAO {
public List<Integer> listaIdOgloszenSprzedawcy(int idSprzedawcy) throws BladBazyDanych { public List<Integer> listaIdOgloszenSprzedawcy(int idSprzedawcy) throws BladBazyDanych {
final String sql = "SELECT id_ogloszenia FROM ogloszenia WHERE id_sprzedawcy = ?"; final String sql = "SELECT id_ogloszenia FROM ogloszenia WHERE id_sprzedawcy = ?";
List<Integer> lista = new LinkedList<>(); List<Integer> lista = new LinkedList<>();
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, idSprzedawcy); stmt.setInt(1, idSprzedawcy);
try(ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
while(rs.next()) { while (rs.next()) {
lista.add(rs.getInt(1)); lista.add(rs.getInt(1));
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new BladBazyDanych("listaIdOgloszenSprzedawcy: "+e.getMessage(), e); throw new BladBazyDanych("listaIdOgloszenSprzedawcy: " + e.getMessage(), e);
} }
return lista; return lista;
} }
public void save(OgloszenieSamochodowe ogl) throws BladBazyDanych { public void save(OgloszenieSamochodowe ogl) throws BladBazyDanych {
if(ogl.getIdOgloszenia() == null) { if (ogl.getIdOgloszenia() == null) {
// wstawiamy nowy rekord korzystajac z autoincrement // wstawiamy nowy rekord korzystajac z autoincrement
insertNew(ogl); insertNew(ogl);
} else if(! update(ogl)) { } else if (!update(ogl)) {
insert(ogl); insert(ogl);
} }
} }
...@@ -182,7 +182,7 @@ public class OgloszenieDAO { ...@@ -182,7 +182,7 @@ public class OgloszenieDAO {
+ " marka, model, generacja, kolor, rocznik, przebieg, pojemnosc, moc, paliwo)" + " marka, model, generacja, kolor, rocznik, przebieg, pojemnosc, moc, paliwo)"
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, ogl.getIdOgloszenia()); stmt.setInt(1, ogl.getIdOgloszenia());
stmt.setInt(2, ogl.getIdSprzedawcy()); stmt.setInt(2, ogl.getIdSprzedawcy());
stmt.setString(3, Konwersje.dateToString(ogl.getDataWystawienia())); stmt.setString(3, Konwersje.dateToString(ogl.getDataWystawienia()));
...@@ -196,7 +196,7 @@ public class OgloszenieDAO { ...@@ -196,7 +196,7 @@ public class OgloszenieDAO {
stmt.setInt(11, ogl.getRocznik()); stmt.setInt(11, ogl.getRocznik());
stmt.setInt(12, ogl.getPrzebieg()); stmt.setInt(12, ogl.getPrzebieg());
Silnik silnik = ogl.getSilnik(); Silnik silnik = ogl.getSilnik();
if(silnik != null) { if (silnik != null) {
stmt.setFloat(13, silnik.getMoc()); stmt.setFloat(13, silnik.getMoc());
stmt.setFloat(14, silnik.getPojemnosc()); stmt.setFloat(14, silnik.getPojemnosc());
stmt.setString(15, Konwersje.paliwoToString(silnik.getPaliwo())); stmt.setString(15, Konwersje.paliwoToString(silnik.getPaliwo()));
...@@ -205,7 +205,7 @@ public class OgloszenieDAO { ...@@ -205,7 +205,7 @@ public class OgloszenieDAO {
int ile = stmt.executeUpdate(); int ile = stmt.executeUpdate();
return (ile > 0); return (ile > 0);
} catch (SQLException e) { } catch (SQLException e) {
throw new BladBazyDanych("Ogloszenie insert: "+e.getMessage(), e); throw new BladBazyDanych("Ogloszenie insert: " + e.getMessage(), e);
} }
} }
...@@ -216,7 +216,7 @@ public class OgloszenieDAO { ...@@ -216,7 +216,7 @@ public class OgloszenieDAO {
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
try { try {
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, ogl.getIdSprzedawcy()); stmt.setInt(1, ogl.getIdSprzedawcy());
stmt.setString(2, Konwersje.dateToString(ogl.getDataWystawienia())); stmt.setString(2, Konwersje.dateToString(ogl.getDataWystawienia()));
stmt.setBigDecimal(3, ogl.getCena()); stmt.setBigDecimal(3, ogl.getCena());
...@@ -229,20 +229,20 @@ public class OgloszenieDAO { ...@@ -229,20 +229,20 @@ public class OgloszenieDAO {
stmt.setInt(10, ogl.getRocznik()); stmt.setInt(10, ogl.getRocznik());
stmt.setInt(11, ogl.getPrzebieg()); stmt.setInt(11, ogl.getPrzebieg());
Silnik silnik = ogl.getSilnik(); Silnik silnik = ogl.getSilnik();
if(silnik != null) { if (silnik != null) {
stmt.setFloat(12, silnik.getPojemnosc()); stmt.setFloat(12, silnik.getPojemnosc());
stmt.setFloat(13, silnik.getMoc()); stmt.setFloat(13, silnik.getMoc());
stmt.setString(14, Konwersje.paliwoToString(silnik.getPaliwo())); stmt.setString(14, Konwersje.paliwoToString(silnik.getPaliwo()));
} }
int ile = stmt.executeUpdate(); int ile = stmt.executeUpdate();
if(ile == 0) { if (ile == 0) {
throw new SQLException("Nie mogę wstawić nowego rekordu ogloszenie"); throw new SQLException("Nie mogę wstawić nowego rekordu ogloszenie");
} }
} }
try(Statement stmt = db.c().createStatement(); try (Statement stmt = db.c().createStatement();
ResultSet rs = stmt.executeQuery("SELECT last_insert_rowid()")) { ResultSet rs = stmt.executeQuery("SELECT last_insert_rowid()")) {
if(rs.next()) { if (rs.next()) {
int noweId = rs.getInt(1); int noweId = rs.getInt(1);
ogl.setIdOgloszenia(noweId); ogl.setIdOgloszenia(noweId);
return noweId; return noweId;
...@@ -251,7 +251,7 @@ public class OgloszenieDAO { ...@@ -251,7 +251,7 @@ public class OgloszenieDAO {
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new BladBazyDanych("Ogloszenie insertNew: "+e.getMessage(), e); throw new BladBazyDanych("Ogloszenie insertNew: " + e.getMessage(), e);
} }
} }
...@@ -261,7 +261,7 @@ public class OgloszenieDAO { ...@@ -261,7 +261,7 @@ public class OgloszenieDAO {
+ " kolor=?, rocznik=?, przebieg=?, pojemnosc=?, moc=?, paliwo=?" + " kolor=?, rocznik=?, przebieg=?, pojemnosc=?, moc=?, paliwo=?"
+ " WHERE id_ogloszenia=?"; + " WHERE id_ogloszenia=?";
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, ogl.getIdSprzedawcy()); stmt.setInt(1, ogl.getIdSprzedawcy());
stmt.setString(2, Konwersje.dateToString(ogl.getDataWystawienia())); stmt.setString(2, Konwersje.dateToString(ogl.getDataWystawienia()));
stmt.setBigDecimal(3, ogl.getCena()); stmt.setBigDecimal(3, ogl.getCena());
...@@ -274,7 +274,7 @@ public class OgloszenieDAO { ...@@ -274,7 +274,7 @@ public class OgloszenieDAO {
stmt.setInt(10, ogl.getRocznik()); stmt.setInt(10, ogl.getRocznik());
stmt.setInt(11, ogl.getPrzebieg()); stmt.setInt(11, ogl.getPrzebieg());
Silnik silnik = ogl.getSilnik(); Silnik silnik = ogl.getSilnik();
if(silnik != null) { if (silnik != null) {
stmt.setFloat(12, silnik.getPojemnosc()); stmt.setFloat(12, silnik.getPojemnosc());
stmt.setFloat(13, silnik.getMoc()); stmt.setFloat(13, silnik.getMoc());
stmt.setString(14, Konwersje.paliwoToString(silnik.getPaliwo())); stmt.setString(14, Konwersje.paliwoToString(silnik.getPaliwo()));
...@@ -284,7 +284,7 @@ public class OgloszenieDAO { ...@@ -284,7 +284,7 @@ public class OgloszenieDAO {
int ile = stmt.executeUpdate(); int ile = stmt.executeUpdate();
return (ile > 0); return (ile > 0);
} catch (SQLException e) { } catch (SQLException e) {
throw new BladBazyDanych("Ogloszenie update: "+e.getMessage(), e); throw new BladBazyDanych("Ogloszenie update: " + e.getMessage(), e);
} }
} }
...@@ -293,17 +293,17 @@ public class OgloszenieDAO { ...@@ -293,17 +293,17 @@ public class OgloszenieDAO {
final String sql = "UPDATE ogloszenia SET cena=?" final String sql = "UPDATE ogloszenia SET cena=?"
+ " WHERE id_ogloszenia=?"; + " WHERE id_ogloszenia=?";
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setBigDecimal(1, nowaCena); stmt.setBigDecimal(1, nowaCena);
stmt.setInt(2, idOgloszenia); stmt.setInt(2, idOgloszenia);
int ile = stmt.executeUpdate(); int ile = stmt.executeUpdate();
if(ile == 0) { if (ile == 0) {
throw new NieznanyRekord("Nieznane ogloszenie " + idOgloszenia); throw new NieznanyRekord("Nieznane ogloszenie " + idOgloszenia);
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new BladBazyDanych("Ogloszenie zmienCene: "+e.getMessage(), e); throw new BladBazyDanych("Ogloszenie zmienCene: " + e.getMessage(), e);
} }
} }
...@@ -311,18 +311,19 @@ public class OgloszenieDAO { ...@@ -311,18 +311,19 @@ public class OgloszenieDAO {
throws BladBazyDanych, NieznanyRekord { throws BladBazyDanych, NieznanyRekord {
final String sql = "DELETE FROM ogloszenia WHERE id_ogloszenia=?"; final String sql = "DELETE FROM ogloszenia WHERE id_ogloszenia=?";
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, idOgloszenia); stmt.setInt(1, idOgloszenia);
int ile = stmt.executeUpdate(); int ile = stmt.executeUpdate();
if(ile == 0) { if (ile == 0) {
throw new NieznanyRekord("Nieznane ogloszenie " + idOgloszenia); throw new NieznanyRekord("Nieznane ogloszenie " + idOgloszenia);
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new BladBazyDanych("Ogloszenie delete: "+e.getMessage(), e); throw new BladBazyDanych("Ogloszenie delete: " + e.getMessage(), e);
} }
} }
private OgloszenieSamochodowe ogloszenieZResultSet(ResultSet rs) throws SQLException { private OgloszenieSamochodowe ogloszenieZResultSet(ResultSet rs) throws SQLException {
Silnik silnik = new Silnik(rs.getFloat("moc"), Silnik silnik = new Silnik(rs.getFloat("moc"),
rs.getFloat("pojemnosc"), rs.getFloat("pojemnosc"),
...@@ -352,7 +353,7 @@ public class OgloszenieDAO { ...@@ -352,7 +353,7 @@ public class OgloszenieDAO {
} }
private void uzupelnijDate(OgloszenieSamochodowe ogl) { private void uzupelnijDate(OgloszenieSamochodowe ogl) {
if(ogl.getDataWystawienia() == null) { if (ogl.getDataWystawienia() == null) {
ogl.setDataWystawienia(LocalDateTime.now()); ogl.setDataWystawienia(LocalDateTime.now());
} }
} }
......
...@@ -25,45 +25,45 @@ public class SprzedawcaDAO { ...@@ -25,45 +25,45 @@ public class SprzedawcaDAO {
public List<Integer> idList() throws BladBazyDanych { public List<Integer> idList() throws BladBazyDanych {
final String sql = "SELECT id_sprzedawcy FROM sprzedawcy"; final String sql = "SELECT id_sprzedawcy FROM sprzedawcy";
List<Integer> lista = new LinkedList<>(); List<Integer> lista = new LinkedList<>();
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
try(ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
while(rs.next()) { while (rs.next()) {
lista.add(rs.getInt(1)); lista.add(rs.getInt(1));
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new BladBazyDanych("listaIdSprzedawcow: "+e.getMessage(), e); throw new BladBazyDanych("listaIdSprzedawcow: " + e.getMessage(), e);
} }
return lista; return lista;
} }
public Sprzedawca findById(int idSprzedawcy) throws BladBazyDanych, NieznanyRekord { public Sprzedawca findById(int idSprzedawcy) throws BladBazyDanych, NieznanyRekord {
final String sql = "SELECT * FROM sprzedawcy WHERE id_sprzedawcy = ?"; final String sql = "SELECT * FROM sprzedawcy WHERE id_sprzedawcy = ?";
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
stmt.setInt(1, idSprzedawcy); stmt.setInt(1, idSprzedawcy);
try(ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
if(rs.next()) { if (rs.next()) {
return sprzedawcaZResultSet(rs); return sprzedawcaZResultSet(rs);
} else { } else {
throw new NieznanyRekord("Nieznany sprzedawca " + idSprzedawcy); throw new NieznanyRekord("Nieznany sprzedawca " + idSprzedawcy);
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new BladBazyDanych("Sprzedawca byId: "+e.getMessage(), e); throw new BladBazyDanych("Sprzedawca byId: " + e.getMessage(), e);
} }
} }
public List<Sprzedawca> readAll() throws BladBazyDanych { public List<Sprzedawca> readAll() throws BladBazyDanych {
final String sql = "SELECT * FROM sprzedawcy"; final String sql = "SELECT * FROM sprzedawcy";
List<Sprzedawca> lista = new LinkedList<>(); List<Sprzedawca> lista = new LinkedList<>();
try(PreparedStatement stmt = db.c().prepareStatement(sql)) { try (PreparedStatement stmt = db.c().prepareStatement(sql)) {
try(ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
while(rs.next()) { while (rs.next()) {
lista.add(sprzedawcaZResultSet(rs)); lista.add(sprzedawcaZResultSet(rs));
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new BladBazyDanych("Sprzedawca readAll: "+e.getMessage(), e); throw new BladBazyDanych("Sprzedawca readAll: " + e.getMessage(), e);
} }
return lista; return lista;
} }
......
...@@ -16,7 +16,7 @@ public class UtworzBaze { ...@@ -16,7 +16,7 @@ public class UtworzBaze {
String skrypt = ""; String skrypt = "";
try { try {
skrypt = String.join("\n", Files.readAllLines(Paths.get("ogloszenia_sqlite.sql"))); skrypt = String.join("\n", Files.readAllLines(Paths.get("ogloszenia_sqlite.sql")));
if(Files.deleteIfExists(Paths.get(Ustawienia.SQLITE_PATH))) { if (Files.deleteIfExists(Paths.get(Ustawienia.SQLITE_PATH))) {
System.out.println("Usunięto plik " + Ustawienia.SQLITE_PATH); System.out.println("Usunięto plik " + Ustawienia.SQLITE_PATH);
} }
} catch (IOException e) { } catch (IOException e) {
...@@ -25,11 +25,11 @@ public class UtworzBaze { ...@@ -25,11 +25,11 @@ public class UtworzBaze {
String[] poleceniaSkryptu = skrypt.split("\\s*;\\s*"); String[] poleceniaSkryptu = skrypt.split("\\s*;\\s*");
System.out.println("Otwarcie pliku z bazą..."); System.out.println("Otwarcie pliku z bazą...");
try(Connection c = DriverManager.getConnection("jdbc:sqlite:" + Ustawienia.SQLITE_PATH); try (Connection c = DriverManager.getConnection("jdbc:sqlite:" + Ustawienia.SQLITE_PATH);
Statement stmt = c.createStatement()) { Statement stmt = c.createStatement()) {
c.setAutoCommit(false); c.setAutoCommit(false);
System.out.println("Wgrywam dane"); System.out.println("Wgrywam dane");
for(String sql : poleceniaSkryptu) { for (String sql : poleceniaSkryptu) {
stmt.addBatch(sql); stmt.addBatch(sql);
System.out.print("."); System.out.print(".");
System.out.flush(); System.out.flush();
......
...@@ -24,7 +24,9 @@ public class Ogloszenie { ...@@ -24,7 +24,9 @@ public class Ogloszenie {
this.opis = opis; this.opis = opis;
} }
/** Pozostawia pole sprzedawca równe null. */ /**
* Pozostawia pole sprzedawca równe null.
*/
public Ogloszenie(Integer idOgloszenia, LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis) { public Ogloszenie(Integer idOgloszenia, LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis) {
this.idOgloszenia = idOgloszenia; this.idOgloszenia = idOgloszenia;
this.dataWystawienia = dataWystawienia; this.dataWystawienia = dataWystawienia;
...@@ -34,7 +36,9 @@ public class Ogloszenie { ...@@ -34,7 +36,9 @@ public class Ogloszenie {
this.idSprzedawcy = null; this.idSprzedawcy = null;
} }
/** Wartość idSprzedawcy pobiera z obiektu sprzedawca. */ /**
* Wartość idSprzedawcy pobiera z obiektu sprzedawca.
*/
public Ogloszenie(Integer idOgloszenia, Sprzedawca sprzedawca, LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis) { public Ogloszenie(Integer idOgloszenia, Sprzedawca sprzedawca, LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis) {
this.idOgloszenia = idOgloszenia; this.idOgloszenia = idOgloszenia;
this.dataWystawienia = dataWystawienia; this.dataWystawienia = dataWystawienia;
...@@ -42,7 +46,7 @@ public class Ogloszenie { ...@@ -42,7 +46,7 @@ public class Ogloszenie {
this.tytul = tytul; this.tytul = tytul;
this.opis = opis; this.opis = opis;
this.sprzedawca = sprzedawca; this.sprzedawca = sprzedawca;
if(sprzedawca != null) { if (sprzedawca != null) {
this.idSprzedawcy = sprzedawca.getIdSprzedawcy(); this.idSprzedawcy = sprzedawca.getIdSprzedawcy();
} }
} }
......
...@@ -28,7 +28,9 @@ public class OgloszenieSamochodowe extends Ogloszenie { ...@@ -28,7 +28,9 @@ public class OgloszenieSamochodowe extends Ogloszenie {
this.silnik = silnik; this.silnik = silnik;
} }
/** Pozostawia pole sprzedawca równe null. */ /**
* Pozostawia pole sprzedawca równe null.
*/
public OgloszenieSamochodowe(Integer idOgloszenia, LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis, public OgloszenieSamochodowe(Integer idOgloszenia, LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis,
String marka, String model, String generacja, String kolor, int rocznik, int przebieg, Silnik silnik) { String marka, String model, String generacja, String kolor, int rocznik, int przebieg, Silnik silnik) {
super(idOgloszenia, dataWystawienia, cena, tytul, opis); super(idOgloszenia, dataWystawienia, cena, tytul, opis);
...@@ -41,7 +43,9 @@ public class OgloszenieSamochodowe extends Ogloszenie { ...@@ -41,7 +43,9 @@ public class OgloszenieSamochodowe extends Ogloszenie {
this.silnik = silnik; this.silnik = silnik;
} }
/** Wartość idSprzedawcy pobiera z obiektu sprzedawca. */ /**
* Wartość idSprzedawcy pobiera z obiektu sprzedawca.
*/
public OgloszenieSamochodowe(Integer idOgloszenia, Sprzedawca sprzedawca, public OgloszenieSamochodowe(Integer idOgloszenia, Sprzedawca sprzedawca,
LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis, LocalDateTime dataWystawienia, BigDecimal cena, String tytul, String opis,
String marka, String model, String generacja, String kolor, int rocznik, int przebieg, Silnik silnik) { String marka, String model, String generacja, String kolor, int rocznik, int przebieg, Silnik silnik) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment