#33 nukuttu ja menty eteenpäin, mutta ei toimi. haeString palauttaa pyydetyn sarakkeen nimen?
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package fi.lpam.dataluokat;
|
package fi.lpam.dataluokat;
|
||||||
|
|
||||||
import fi.lpam.gui.virheDialogit.SiirtoVirhe;
|
import fi.lpam.gui.virheDialogit.SiirtoVirhe;
|
||||||
|
import javafx.scene.control.Alert;
|
||||||
|
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.time.DayOfWeek;
|
import java.time.DayOfWeek;
|
||||||
@@ -29,7 +30,6 @@ public class Asiakas implements TietokantaOlio {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Vain MultiColumnListViewin käyttöön
|
|
||||||
* @param nimi Asiakkaan nimi
|
* @param nimi Asiakkaan nimi
|
||||||
*/
|
*/
|
||||||
public Asiakas(String nimi) {
|
public Asiakas(String nimi) {
|
||||||
@@ -40,65 +40,9 @@ public class Asiakas implements TietokantaOlio {
|
|||||||
* Kun luetaan asiakas tietokannasta
|
* Kun luetaan asiakas tietokannasta
|
||||||
*/
|
*/
|
||||||
private Asiakas(int id,
|
private Asiakas(int id,
|
||||||
String nimi,
|
String nimi) {
|
||||||
String osoite,
|
|
||||||
int maanantaiKpl,
|
|
||||||
int tiistaiKpl,
|
|
||||||
int keskiviikkoKpl,
|
|
||||||
int torstaiKpl,
|
|
||||||
int perjantaiKpl,
|
|
||||||
int salaatit,
|
|
||||||
int pääruoat,
|
|
||||||
int jälkiruoat,
|
|
||||||
String erityisruokavalio,
|
|
||||||
String avainTieto,
|
|
||||||
String lisätiedot,
|
|
||||||
String laskutusOsoite,
|
|
||||||
String yhteyshenkilönNimi,
|
|
||||||
String yhteyshenkilönPuhelinnumero,
|
|
||||||
String yhteyshenkilönSähköposti,
|
|
||||||
int lauantaiKpl,
|
|
||||||
int sunnuntaiKpl,
|
|
||||||
int dinnerMaanantaiKpl,
|
|
||||||
int dinnerTiistaiKpl,
|
|
||||||
int dinnerKeskiviikkoKpl,
|
|
||||||
int dinnerTorstaiKpl,
|
|
||||||
int dinnerPerjantaiKpl,
|
|
||||||
int dinnerLauantaiKpl,
|
|
||||||
int dinnerSunnuntaiKpl,
|
|
||||||
int dinnerSalaatit,
|
|
||||||
int dinnerPääruoat,
|
|
||||||
int dinnerJälkiruoat) {
|
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.nimi = nimi;
|
this.nimi = nimi;
|
||||||
this.osoite = osoite;
|
|
||||||
this.maanantaiKpl = maanantaiKpl;
|
|
||||||
this.tiistaiKpl = tiistaiKpl;
|
|
||||||
this.keskiviikkoKpl = keskiviikkoKpl;
|
|
||||||
this.torstaiKpl = torstaiKpl;
|
|
||||||
this.perjantaiKpl = perjantaiKpl;
|
|
||||||
this.lauantaiKpl = lauantaiKpl;
|
|
||||||
this.sunnuntaiKpl = sunnuntaiKpl;
|
|
||||||
this.salaatit = salaatit;
|
|
||||||
this.pääruoat = pääruoat;
|
|
||||||
this.jälkiruoat = jälkiruoat;
|
|
||||||
this.erityisruokavalio = erityisruokavalio;
|
|
||||||
this.avainTieto = avainTieto;
|
|
||||||
this.lisätiedot = lisätiedot;
|
|
||||||
this.laskutusOsoite = laskutusOsoite;
|
|
||||||
this.yhteyshenkilönNimi = yhteyshenkilönNimi;
|
|
||||||
this.yhteyshenkilönPuhelinnumero = yhteyshenkilönPuhelinnumero;
|
|
||||||
this.yhteyshenkilönSähköposti = yhteyshenkilönSähköposti;
|
|
||||||
this.dinnerMaanantaiKpl = dinnerMaanantaiKpl;
|
|
||||||
this.dinnerTiistaiKpl = dinnerTiistaiKpl;
|
|
||||||
this.dinnerKeskiviikkoKpl = dinnerKeskiviikkoKpl;
|
|
||||||
this.dinnerTorstaiKpl = dinnerTorstaiKpl;
|
|
||||||
this.dinnerPerjantaiKpl = dinnerPerjantaiKpl;
|
|
||||||
this.dinnerLauantaiKpl = dinnerLauantaiKpl;
|
|
||||||
this.dinnerSunnuntaiKpl = dinnerSunnuntaiKpl;
|
|
||||||
this.dinnerSalaatit = dinnerSalaatit;
|
|
||||||
this.dinnerPääruoat = dinnerPääruoat;
|
|
||||||
this.dinnerJälkiruoat = dinnerJälkiruoat;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -106,15 +50,16 @@ public class Asiakas implements TietokantaOlio {
|
|||||||
* @return ArrayList<Asiakas>
|
* @return ArrayList<Asiakas>
|
||||||
*/
|
*/
|
||||||
public static ArrayList<Asiakas> haeKaikki() {
|
public static ArrayList<Asiakas> haeKaikki() {
|
||||||
|
ArrayList<Asiakas> asiakkaat = new ArrayList<>();
|
||||||
try (Connection tietokanta = Tietokanta.haeYhteys()) {
|
try (Connection tietokanta = Tietokanta.haeYhteys()) {
|
||||||
Statement stmt = tietokanta.createStatement();
|
Statement stmt = tietokanta.createStatement();
|
||||||
ResultSet rs = stmt.executeQuery("select * from asiakkaat");
|
ResultSet rs = stmt.executeQuery("select id, nimi from asiakkaat");
|
||||||
return parsiResultSet(rs);
|
asiakkaat = parsiResultSet(rs);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
SiirtoVirhe alert = new SiirtoVirhe(e);
|
SiirtoVirhe alert = new SiirtoVirhe(e);
|
||||||
alert.showAndWait();
|
alert.showAndWait();
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
return asiakkaat;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -129,7 +74,7 @@ public class Asiakas implements TietokantaOlio {
|
|||||||
sbIDt.append(", ").append(IDt[i]);
|
sbIDt.append(", ").append(IDt[i]);
|
||||||
}
|
}
|
||||||
try (Connection tietokanta = Tietokanta.haeYhteys()) {
|
try (Connection tietokanta = Tietokanta.haeYhteys()) {
|
||||||
String sql = "select * from asiakkaat where id in (" + sbIDt + ")";
|
String sql = "select id, nimi from asiakkaat where id in (" + sbIDt + ")";
|
||||||
Statement stmt = tietokanta.createStatement();
|
Statement stmt = tietokanta.createStatement();
|
||||||
ResultSet rs = stmt.executeQuery(sql);
|
ResultSet rs = stmt.executeQuery(sql);
|
||||||
|
|
||||||
@@ -149,49 +94,15 @@ public class Asiakas implements TietokantaOlio {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Käsittelee ResultSettiä
|
* Olettaa resultsetin sisältävän id:n ja nimen
|
||||||
* @param rs saatu ResultSet
|
* @param rs saatu ResultSet
|
||||||
* @return ArrayList<Asiakas>
|
* @return ArrayList<Asiakas>
|
||||||
*/
|
*/
|
||||||
private static ArrayList<Asiakas> parsiResultSet(ResultSet rs) {
|
private static ArrayList<Asiakas> parsiResultSet(ResultSet rs) {
|
||||||
ArrayList<Asiakas> asiakkaat = new ArrayList<>();
|
ArrayList<Asiakas> asiakkaat = new ArrayList<>();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
int i = 1;
|
asiakkaat.add(new Asiakas(rs.getInt("id"), rs.getString("nimi")));
|
||||||
Asiakas uusi = new Asiakas(
|
|
||||||
rs.getInt(i++),
|
|
||||||
rs.getString(i++),
|
|
||||||
rs.getString(i++),
|
|
||||||
rs.getInt(i++),
|
|
||||||
rs.getInt(i++),
|
|
||||||
rs.getInt(i++),
|
|
||||||
rs.getInt(i++),
|
|
||||||
rs.getInt(i++),
|
|
||||||
rs.getInt(i++),
|
|
||||||
rs.getInt(i++),
|
|
||||||
rs.getInt(i++),
|
|
||||||
rs.getString(i++),
|
|
||||||
rs.getString(i++),
|
|
||||||
rs.getString(i++),
|
|
||||||
rs.getString(i++),
|
|
||||||
rs.getString(i++),
|
|
||||||
rs.getString(i++),
|
|
||||||
rs.getString(i++),
|
|
||||||
rs.getInt(i++),
|
|
||||||
rs.getInt(i++),
|
|
||||||
rs.getInt(i++),
|
|
||||||
rs.getInt(i++),
|
|
||||||
rs.getInt(i++),
|
|
||||||
rs.getInt(i++),
|
|
||||||
rs.getInt(i++),
|
|
||||||
rs.getInt(i++),
|
|
||||||
rs.getInt(i++),
|
|
||||||
rs.getInt(i++),
|
|
||||||
rs.getInt(i++),
|
|
||||||
rs.getInt(i++)
|
|
||||||
);
|
|
||||||
asiakkaat.add(uusi);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (SQLException e) {
|
catch (SQLException e) {
|
||||||
@@ -203,7 +114,6 @@ public class Asiakas implements TietokantaOlio {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Tallentaa uuden asiakkaan tietokantaan. Tietokanta luo automaattisesti id:n asiakkaalle.
|
* Tallentaa uuden asiakkaan tietokantaan. Tietokanta luo automaattisesti id:n asiakkaalle.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
private boolean tallennaUusi() {
|
private boolean tallennaUusi() {
|
||||||
try (Connection tietokanta = Tietokanta.haeYhteys()) {
|
try (Connection tietokanta = Tietokanta.haeYhteys()) {
|
||||||
@@ -393,6 +303,53 @@ public class Asiakas implements TietokantaOlio {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String haeString(String sarake) {
|
||||||
|
try (Connection conn = Tietokanta.haeYhteys()) {
|
||||||
|
PreparedStatement stmt = conn.prepareStatement("select ? from asiakkaat where id = ?");
|
||||||
|
stmt.setString(1, sarake);
|
||||||
|
stmt.setInt(2, this.id);
|
||||||
|
ResultSet rs = stmt.executeQuery();
|
||||||
|
rs.next();
|
||||||
|
return rs.getString(1);
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
Alert alert = new SiirtoVirhe(e);
|
||||||
|
alert.showAndWait();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int haeInt(String sarake) {
|
||||||
|
try (Connection conn = Tietokanta.haeYhteys()) {
|
||||||
|
PreparedStatement stmt = conn.prepareStatement("select ? from asiakkaat where id = ?");
|
||||||
|
stmt.setString(1, sarake);
|
||||||
|
stmt.setInt(2, this.id);
|
||||||
|
ResultSet rs = stmt.executeQuery();
|
||||||
|
rs.next();
|
||||||
|
return rs.getInt(1);
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
Alert alert = new SiirtoVirhe(e);
|
||||||
|
alert.showAndWait();
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Date haeDate(String sarake) {
|
||||||
|
try (Connection conn = Tietokanta.haeYhteys()) {
|
||||||
|
PreparedStatement stmt = conn.prepareStatement("select ? from asiakkaat where id = ?");
|
||||||
|
stmt.setString(1, sarake);
|
||||||
|
stmt.setInt(2, this.id);
|
||||||
|
ResultSet rs = stmt.executeQuery();
|
||||||
|
rs.next();
|
||||||
|
return rs.getDate(1);
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
Alert alert = new SiirtoVirhe(e);
|
||||||
|
alert.showAndWait();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public int getId() {
|
public int getId() {
|
||||||
return id;
|
return id;
|
||||||
@@ -411,6 +368,9 @@ public class Asiakas implements TietokantaOlio {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getOsoite() {
|
public String getOsoite() {
|
||||||
|
if (osoite == null) {
|
||||||
|
osoite = haeString("osoite");
|
||||||
|
}
|
||||||
return osoite;
|
return osoite;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -419,6 +379,9 @@ public class Asiakas implements TietokantaOlio {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int getMaanantaiKpl() {
|
public int getMaanantaiKpl() {
|
||||||
|
if (maanantaiKpl == 0) {
|
||||||
|
maanantaiKpl = haeInt("maanantaiKpl");
|
||||||
|
}
|
||||||
return maanantaiKpl;
|
return maanantaiKpl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user