ݺߣ

ݺߣShare a Scribd company logo
//Merubah tampilan cursor
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        jTextField1.setCursor(new Cursor(Cursor.WAIT_CURSOR));
}


//Ngeprint JTable
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        MessageFormat header = new MessageFormat("Data Siswa");


        MessageFormat footer = new MessageFormat("Jumlah yang telah dibayar : "
                                                     +jTable1.getRowCount());
        try {
            jTable1.print(JTable.PrintMode.FIT_WIDTH, header, footer);
        } catch (java.awt.print.PrinterException e) {
            System.err.format("Cannot print %s%n", e.getMessage());
        }
}


//Entity Class


public class Buku {
    private String id_buku;
    private String nid;
    private String nama_buku;
    private String penerbit;
    private String tahun_buku;
    private String jenjang_buku;
    private String jumlah;
    private String status_buku;
    private String harga_buku;


    public String getId_buku() {
        return id_buku;
    }


    public void setId_buku(String id_buku) {
        this.id_buku = id_buku;
    }


    public String getNid() {
        return nid;
    }


    public void setNid(String nid) {
        this.nid = nid;
    }


    public String getNama_buku() {
        return nama_buku;
    }
public void setNama_buku(String nama_buku) {
    this.nama_buku = nama_buku;
}


public String getPenerbit() {
    return penerbit;
}


public void setPenerbit(String penerbit) {
    this.penerbit = penerbit;
}


public String getTahun_buku() {
    return tahun_buku;
}


public void setTahun_buku(String tahun_buku) {
    this.tahun_buku = tahun_buku;
}


public String getJenjang_buku() {
    return jenjang_buku;
}


public void setJenjang_buku(String jenjang_buku) {
    this.jenjang_buku = jenjang_buku;
}


public String getJumlah() {
    return jumlah;
}


public void setJumlah(String jumlah) {
    this.jumlah = jumlah;
}


public String getStatus_buku() {
    return status_buku;
}


public void setStatus_buku(String status_buku) {
    this.status_buku = status_buku;
}


public String getHarga_buku() {
    return harga_buku;
}


public void setHarga_buku(String harga_buku) {
    this.harga_buku = harga_buku;
}
}


//Multiple Selection Table


String list = "Jumlah terseleksi : "+jTable1.getSelectedRowCount()+"n";


int i [] = jTable1.getSelectedRows();
for (int j = 0; j < i.length; j++) {
          list += jTable1.getValueAt(i[j], 0)+"n";
}


JOptionPane.showMessageDialog(null, list);


//Multiple Selection Delete Table


int i [] = tableBuku.getSelectedRows();
for (int j = 0; j < i.length; j++) {
          bukuService.deleteBuku(tableBuku.getValueAt(i[j], 0).toString());
}


//addItem jCombo tahun dan setSelectedItemCombo berdasarkan tahun sekarang


int tahun = 1981;
for (int i = 0; i < 50; i++) {
          comboTahun.addItem(""+tahun++);
}
comboTahun.setSelectedItem(setTahun());


public String setTahun(){
          Date date = new Date();
          SimpleDateFormat formatTahun = new SimpleDateFormat("yyyy");


          String tahun1 = formatTahun.format(date);
          return tahun1;
}


//Login


public boolean isLoginAdmin(String user, String pass){
          try {
              preparedStatement = null;
              preparedStatement = DatabaseUtilities.getConnection().prepareStatement(
                       "SELECT * FROM ADMIN WHERE NID = ? and Password = ?");
              preparedStatement.setString(1, user);
              preparedStatement.setString(2, pass);


              ResultSet rs = preparedStatement.executeQuery();
              if(rs.next()){
                  return true;
              }else{
                  return false;
}
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Login Gagal, Karena : "+ex.getMessage());
        }
        return false;
}


//Insert Buku


public void insertBuku(Buku buku){
        preparedStatement = null;


        try {
            preparedStatement = DatabaseUtilities.getConnection().prepareStatement(
                     "INSERT INTO BUKU (ID_BUKU, NID, NAMA_BUKU, PENERBIT, "
                     + "TAHUN_BUKU, JENJANG_BUKU, JUMLAH, STATUS_BUKU, "
                     + "HARGA_BUKU) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");


            preparedStatement.setString(1, buku.getId_buku());
            preparedStatement.setString(2, buku.getNid());
            preparedStatement.setString(3, buku.getNama_buku());
            preparedStatement.setString(4, buku.getPenerbit());
            preparedStatement.setString(5, buku.getTahun_buku());
            preparedStatement.setString(6, buku.getJenjang_buku());
            preparedStatement.setString(7, buku.getJumlah());
            preparedStatement.setString(8, buku.getStatus_buku());
            preparedStatement.setString(9, buku.getHarga_buku());


            int insertBuku = preparedStatement.executeUpdate();
            if(insertBuku == 1){
                JOptionPane.showMessageDialog(null, "Insert berhasil");
            }else{
                JOptionPane.showMessageDialog(null, "Insert gagal","Error
Message",JOptionPane.ERROR_MESSAGE);
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Insert gagal karena :
"+ex.getMessage(),"Error Message",JOptionPane.ERROR_MESSAGE);
        }
}


//Update Buku


public void updateBuku(Buku buku){
        preparedStatement = null;


        try {
            preparedStatement = DatabaseUtilities.getConnection().prepareStatement(
                     "UPDATE BUKU SET NID = ?, NAMA_BUKU = ?, PENERBIT = ?, "
                     + "TAHUN_BUKU = ?, JENJANG_BUKU = ?, JUMLAH = ?, STATUS_BUKU = ?, "
                     + "HARGA_BUKU = ? WHERE ID_BUKU = ?");
preparedStatement.setString(1, buku.getNid());
            preparedStatement.setString(2, buku.getNama_buku());
            preparedStatement.setString(3, buku.getPenerbit());
            preparedStatement.setString(4, buku.getTahun_buku());
            preparedStatement.setString(5, buku.getJenjang_buku());
            preparedStatement.setString(6, buku.getJumlah());
            preparedStatement.setString(7, buku.getStatus_buku());
            preparedStatement.setString(8, buku.getHarga_buku());
            preparedStatement.setString(9, buku.getId_buku());


            int update = preparedStatement.executeUpdate();
            if(update   == 1 ){
                JOptionPane.showMessageDialog(null, "Edit buku berhasil");
            }else{
                JOptionPane.showMessageDialog(null, "Edit buku gagal","Error
Message",JOptionPane.ERROR_MESSAGE);
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Edit buku gagal karena :
"+ex.getMessage(),"Error Message",JOptionPane.ERROR_MESSAGE);
        }
}


//Delete Buku


public void deleteBuku(String id_buku){
        preparedStatement = null;


        try {
            preparedStatement = DatabaseUtilities.getConnection().prepareStatement(
                     "DELETE FROM BUKU WHERE ID_BUKU = ?");


            preparedStatement.setString(1, id_buku);


            int delete = preparedStatement.executeUpdate();
            if(delete   == 1){
                JOptionPane.showMessageDialog(null, "Hapus buku berhasil");
            }else{
                JOptionPane.showMessageDialog(null, "Hapus buku gagal","Error
Message",JOptionPane.ERROR_MESSAGE);
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Hapus buku gagal karena : "+ex.getMessage());
        }
}
//Default Table Model
public DefaultTableModel getAllSiswa(){
        DefaultTableModel model = null;
        model = new DefaultTableModel();


        model.addColumn("NIS");
        model.addColumn("NID");
        model.addColumn("NAMA SISWA");
        model.addColumn("SEKOLAH SISWA");
        model.addColumn("KELAS SISWA");
        model.addColumn("JENIS KELAMIN");


        statement = null;
        try {
            statement = DatabaseUtilities.getConnection().createStatement();


            ResultSet rs = statement.executeQuery("SELECT * FROM SISWA");
            while(rs.next()){
                model.addRow(new Object[]{rs.getString("NIS"), rs.getString("NID"),
                        rs.getString("NAMA_SISWA"), rs.getString("SEKOLAH_SISWA"),
                        rs.getString("KELAS_SISWA"), rs.getString("JENIS_KELAMIN_SISWA")});
            }
        } catch (SQLException ex) {
            Logger.getLogger(SiswaService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return model;
}

More Related Content

Arsip coding java rev1

  • 1. //Merubah tampilan cursor private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { jTextField1.setCursor(new Cursor(Cursor.WAIT_CURSOR)); } //Ngeprint JTable private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { MessageFormat header = new MessageFormat("Data Siswa"); MessageFormat footer = new MessageFormat("Jumlah yang telah dibayar : " +jTable1.getRowCount()); try { jTable1.print(JTable.PrintMode.FIT_WIDTH, header, footer); } catch (java.awt.print.PrinterException e) { System.err.format("Cannot print %s%n", e.getMessage()); } } //Entity Class public class Buku { private String id_buku; private String nid; private String nama_buku; private String penerbit; private String tahun_buku; private String jenjang_buku; private String jumlah; private String status_buku; private String harga_buku; public String getId_buku() { return id_buku; } public void setId_buku(String id_buku) { this.id_buku = id_buku; } public String getNid() { return nid; } public void setNid(String nid) { this.nid = nid; } public String getNama_buku() { return nama_buku; }
  • 2. public void setNama_buku(String nama_buku) { this.nama_buku = nama_buku; } public String getPenerbit() { return penerbit; } public void setPenerbit(String penerbit) { this.penerbit = penerbit; } public String getTahun_buku() { return tahun_buku; } public void setTahun_buku(String tahun_buku) { this.tahun_buku = tahun_buku; } public String getJenjang_buku() { return jenjang_buku; } public void setJenjang_buku(String jenjang_buku) { this.jenjang_buku = jenjang_buku; } public String getJumlah() { return jumlah; } public void setJumlah(String jumlah) { this.jumlah = jumlah; } public String getStatus_buku() { return status_buku; } public void setStatus_buku(String status_buku) { this.status_buku = status_buku; } public String getHarga_buku() { return harga_buku; } public void setHarga_buku(String harga_buku) { this.harga_buku = harga_buku; }
  • 3. } //Multiple Selection Table String list = "Jumlah terseleksi : "+jTable1.getSelectedRowCount()+"n"; int i [] = jTable1.getSelectedRows(); for (int j = 0; j < i.length; j++) { list += jTable1.getValueAt(i[j], 0)+"n"; } JOptionPane.showMessageDialog(null, list); //Multiple Selection Delete Table int i [] = tableBuku.getSelectedRows(); for (int j = 0; j < i.length; j++) { bukuService.deleteBuku(tableBuku.getValueAt(i[j], 0).toString()); } //addItem jCombo tahun dan setSelectedItemCombo berdasarkan tahun sekarang int tahun = 1981; for (int i = 0; i < 50; i++) { comboTahun.addItem(""+tahun++); } comboTahun.setSelectedItem(setTahun()); public String setTahun(){ Date date = new Date(); SimpleDateFormat formatTahun = new SimpleDateFormat("yyyy"); String tahun1 = formatTahun.format(date); return tahun1; } //Login public boolean isLoginAdmin(String user, String pass){ try { preparedStatement = null; preparedStatement = DatabaseUtilities.getConnection().prepareStatement( "SELECT * FROM ADMIN WHERE NID = ? and Password = ?"); preparedStatement.setString(1, user); preparedStatement.setString(2, pass); ResultSet rs = preparedStatement.executeQuery(); if(rs.next()){ return true; }else{ return false;
  • 4. } } catch (SQLException ex) { JOptionPane.showMessageDialog(null, "Login Gagal, Karena : "+ex.getMessage()); } return false; } //Insert Buku public void insertBuku(Buku buku){ preparedStatement = null; try { preparedStatement = DatabaseUtilities.getConnection().prepareStatement( "INSERT INTO BUKU (ID_BUKU, NID, NAMA_BUKU, PENERBIT, " + "TAHUN_BUKU, JENJANG_BUKU, JUMLAH, STATUS_BUKU, " + "HARGA_BUKU) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"); preparedStatement.setString(1, buku.getId_buku()); preparedStatement.setString(2, buku.getNid()); preparedStatement.setString(3, buku.getNama_buku()); preparedStatement.setString(4, buku.getPenerbit()); preparedStatement.setString(5, buku.getTahun_buku()); preparedStatement.setString(6, buku.getJenjang_buku()); preparedStatement.setString(7, buku.getJumlah()); preparedStatement.setString(8, buku.getStatus_buku()); preparedStatement.setString(9, buku.getHarga_buku()); int insertBuku = preparedStatement.executeUpdate(); if(insertBuku == 1){ JOptionPane.showMessageDialog(null, "Insert berhasil"); }else{ JOptionPane.showMessageDialog(null, "Insert gagal","Error Message",JOptionPane.ERROR_MESSAGE); } } catch (SQLException ex) { JOptionPane.showMessageDialog(null, "Insert gagal karena : "+ex.getMessage(),"Error Message",JOptionPane.ERROR_MESSAGE); } } //Update Buku public void updateBuku(Buku buku){ preparedStatement = null; try { preparedStatement = DatabaseUtilities.getConnection().prepareStatement( "UPDATE BUKU SET NID = ?, NAMA_BUKU = ?, PENERBIT = ?, " + "TAHUN_BUKU = ?, JENJANG_BUKU = ?, JUMLAH = ?, STATUS_BUKU = ?, " + "HARGA_BUKU = ? WHERE ID_BUKU = ?");
  • 5. preparedStatement.setString(1, buku.getNid()); preparedStatement.setString(2, buku.getNama_buku()); preparedStatement.setString(3, buku.getPenerbit()); preparedStatement.setString(4, buku.getTahun_buku()); preparedStatement.setString(5, buku.getJenjang_buku()); preparedStatement.setString(6, buku.getJumlah()); preparedStatement.setString(7, buku.getStatus_buku()); preparedStatement.setString(8, buku.getHarga_buku()); preparedStatement.setString(9, buku.getId_buku()); int update = preparedStatement.executeUpdate(); if(update == 1 ){ JOptionPane.showMessageDialog(null, "Edit buku berhasil"); }else{ JOptionPane.showMessageDialog(null, "Edit buku gagal","Error Message",JOptionPane.ERROR_MESSAGE); } } catch (SQLException ex) { JOptionPane.showMessageDialog(null, "Edit buku gagal karena : "+ex.getMessage(),"Error Message",JOptionPane.ERROR_MESSAGE); } } //Delete Buku public void deleteBuku(String id_buku){ preparedStatement = null; try { preparedStatement = DatabaseUtilities.getConnection().prepareStatement( "DELETE FROM BUKU WHERE ID_BUKU = ?"); preparedStatement.setString(1, id_buku); int delete = preparedStatement.executeUpdate(); if(delete == 1){ JOptionPane.showMessageDialog(null, "Hapus buku berhasil"); }else{ JOptionPane.showMessageDialog(null, "Hapus buku gagal","Error Message",JOptionPane.ERROR_MESSAGE); } } catch (SQLException ex) { JOptionPane.showMessageDialog(null, "Hapus buku gagal karena : "+ex.getMessage()); } }
  • 6. //Default Table Model public DefaultTableModel getAllSiswa(){ DefaultTableModel model = null; model = new DefaultTableModel(); model.addColumn("NIS"); model.addColumn("NID"); model.addColumn("NAMA SISWA"); model.addColumn("SEKOLAH SISWA"); model.addColumn("KELAS SISWA"); model.addColumn("JENIS KELAMIN"); statement = null; try { statement = DatabaseUtilities.getConnection().createStatement(); ResultSet rs = statement.executeQuery("SELECT * FROM SISWA"); while(rs.next()){ model.addRow(new Object[]{rs.getString("NIS"), rs.getString("NID"), rs.getString("NAMA_SISWA"), rs.getString("SEKOLAH_SISWA"), rs.getString("KELAS_SISWA"), rs.getString("JENIS_KELAMIN_SISWA")}); } } catch (SQLException ex) { Logger.getLogger(SiswaService.class.getName()).log(Level.SEVERE, null, ex); } return model; }