在java数据库中创建对象错误

时间:2017-04-29 04:58:05

标签: java mysql

我创建了loadTransaksi方法,此方法将返回ArrayList

这是我的代码

public ArrayList<Transaksi> loadTransaksi() {
    try {
        ArrayList<Transaksi> daftarTransaksi = new ArrayList<>();
        String query = "select * from transaksi";
        ResultSet rs = statement.executeQuery(query);
        while (rs.next()) {
            Transaksi t = new Transaksi(rs.getInt(4), this.getAccount(rs.getString(5)), this.getPelanggan(rs.getInt(6)),rs.getDate(1), rs.getDate(2), rs.getLong(3));
        }
        return daftarTransaksi;
    } catch (Exception e) {
        throw new IllegalArgumentException("terjadi kesalahan saat load transaksi");
    }
}

但是在调用getAccount和getPelanggan方法时出现错误

这是我的getAccount代码

     public Pegawai getAccount(String username) {
    try {
        Pegawai p = null;
        String query = "select * from pegawai Where username ='" + username +"'";
        ResultSet rs = statement.executeQuery(query);
        if (rs.next()) {
            p = new Pegawai(rs.getString(3), rs.getString(2), rs.getString(1));
        }
        return p;
    } catch (Exception e) {
        throw new IllegalArgumentException("terjadi kesalahan saat load peserta");
    }
}

这个用于getPelanggan

    public Pelanggan getPelanggan(int id_pelanggan){
    try {
        String query = "select * from pelanggan where id_pelanggan='" + id_pelanggan +"'";
        ResultSet rs = statement.executeQuery(query);

        if (rs.next()) {
            Pelanggan p = new Pelanggan(rs.getInt(2) ,rs.getString("nama"), this.getCucian(rs.getInt(3)));
            return p;
        }
        return null;
    } catch (Exception e) {
        throw new IllegalArgumentException("terjadi kesalahan saat load pelanggan");
    }
} 

在getPelanggan方法中我也称为getCucian,这是代码

    public Cucian getCucian(int id_cucian){
    try {
        String query = "select * from cucian Where id_cucian ='" + id_cucian +"'";
        ResultSet rs = statement.executeQuery(query);
        if (rs.next()) {
            Cucian c = new Cucian(rs.getInt("id_cucian"), rs.getInt("berat"));
            System.out.println("Cucian " + c.getId());
            return c;
        }
        return null;
    } catch (Exception e) {
        throw new IllegalArgumentException("terjadi kesalahan saat load peserta");
    }
}

我是java和mySQL的新手,还在学习它。希望你们能帮助我。谢谢你:D 更新---- 这是Transaksi课程

public class Transaksi {
private int kodeTransaksi;
private Pegawai pegawai;
private Pelanggan pelanggan;
private java.util.Date tglMasuk;
private java.util.Date tglKeluar;
private long bayar;

public Transaksi(Pegawai pegawai, Pelanggan pelanggan, Date tglMasuk, Date tglKeluar, long bayar) {
    this.pegawai = pegawai;
    this.pelanggan = pelanggan;
    this.tglMasuk = tglMasuk;
    this.tglKeluar = tglKeluar;
    this.bayar = bayar;
}


public Transaksi(int kodeTransaksi, Pegawai pegawai, Pelanggan pelanggan, java.util.Date tglMasuk, java.util.Date tglKeluar, long bayar) {
    this.kodeTransaksi = kodeTransaksi;
    this.pegawai = pegawai;
    this.pelanggan = pelanggan;
    this.tglMasuk = tglMasuk;
    this.tglKeluar = tglKeluar;
    this.bayar = bayar;
}

public int getKodeTransaksi() {
    return kodeTransaksi;
}

public Pegawai getPegawai() {
    return pegawai;
}

public Pelanggan getPelanggan() {
    return pelanggan;
}

public java.util.Date getTglMasuk() {
    return tglMasuk;
}

public java.util.Date getTglKeluar() {
    return tglKeluar;
}

public long getBayar() {
    return bayar;
}

public void setBayar(long bayar) {
    this.bayar = bayar;
}


public void setKodeTransaksi(int kodeTransaksi) {
    this.kodeTransaksi = kodeTransaksi;
}

public void setPegawai(Pegawai pegawai) {
    this.pegawai = pegawai;
}

public void setPelanggan(Pelanggan pelanggan) {
    this.pelanggan = pelanggan;
}

public void setTglMasuk(java.util.Date tglMasuk) {
    this.tglMasuk = tglMasuk;
}

public void setTglKeluar(java.util.Date tglKeluar) {
    this.tglKeluar = tglKeluar;
}

public long getTotalHarga(){
    return bayar;
}

}

这是错误消息 https://imghost.io/image/abEdo imghost.io/image/abJy0

0 个答案:

没有答案