i have a class which function is to validate the input to the database befor it’s insert into the other database… it’s look like this :
1. Database Penduduk using oracle 9i
2. Database Pemohon using MySQL
here is the class i used to validate
this class is in data.KTP which is call Oracle 9i Database
public class KTP {
public KTP (){
}
private Connection connection;
private String sql;
public KTP(Connection connection) {
this.connection = connection;
}
public boolean CekNikPadaKtp(String nikPemohon) throws Exception {
PreparedStatement ps = null;
try {
boolean cekNik = true;
String nikKtp = "";
sql = "SELECT NIK, TGL_BERLAKU FROM SIAKOFF.VW_KTP_WNI A, DUAL B "
+ "where TGL_BERLAKU < to_char(sysdate, 'DD-MM-YYYY') AND NIK=?";
ps = connection.prepareStatement(sql);
ps.setString(1, nikPemohon);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
nikKtp = rs.getString("nik");
}
rs.close();
if (nikKtp.equals("")) {
cekNik = false;
}
return cekNik;
} finally {
ConnectionUtil.closePreparedStatement(ps);
}
}
}
and this one is from the other which is call from the above
public class Pemohon {
private Connection connection;
private String sql;
public Pemohon(Connection connection) {
this.connection = connection;
}
public String savePemohon(TblPemohon tp) throws Exception {
PreparedStatement ps = null;
try {
if (CekNikPadaKtp(tp.getKtpPemohon())) {
sql = "insert into pemohon"
+ "(noktp_pemohon,nama_depan,nama_belakang,tempat_lahir_pemohon,tgl_lahir_pemohon,sex,"
+ "agama,status_perkawinan,pekerjaan_pemohon,nama_jalan_pemohon,rt_pemohon,rw_pemohon,kelurahan_pemohon,"
+ "kecamatan_pemohon,kab_ko_pemohon,provinsi_pemohon,pos_pemohon,telp_pemohon,kewarganegaraan_pemohon) "
+ "values("
+ "?,?,?,?,?,"
+ "?,?,?,?,?,"
+ "?,?,?,?,?,"
+ "?,?,?,?,?)";
ps = connection.prepareStatement(sql);
ps.setString(1, tp.getKtpPemohon());
ps.setString(2, tp.getNamaDepanPemohon());
ps.setString(3, tp.getNamaBelakangPemohon());
ps.setString(4, tp.getTempatLahirPemohon());
ps.setString(5, tp.getTanggalLahirPemohon());
ps.setString(6, tp.getJenisKelamin());
ps.setString(7, tp.getAgamaPemohon());
ps.setString(8, tp.getStatusPerkawinan());
ps.setString(9, tp.getPekerjaanPemohon());
ps.setString(10, tp.getNamaJalanPemohon());
ps.setString(11, tp.getRtPemohon());
ps.setString(12, tp.getRwPemohon());
ps.setString(13, tp.getKelurahanPemohon());
ps.setString(14, tp.getKecamatanPemohon());
ps.setString(15, tp.getKabupatenPemohon());
ps.setString(17, tp.getProvinsiPemohon());
ps.setString(18, tp.getKodePosPemohon());
ps.setString(19, tp.getTelpPemohon());
ps.setString(20, tp.getKewarganegaraanPemohon());
ps.execute();
return "success";
} else {
return "Info : nik pemohon belum ada di tabel ktp !";
}
} finally {
ConnectionUtil.closePreparedStatement(ps);
}
}
}
Help me please…
public boolean CekNikPadaKtp(String nikPemohon)is an instance method in yourKTPclass and now you want to call it inside your classPemohonthen first you need to create an instance of
KTPclass and then invoke the method, likeYou cannot call an instance method of some other class without creating an instance/object of that class.
I think you are not clear enough with classes, objects, instance methods, static methods, etc. and various other concepts in java.
So better first clear these and then try, you will find it much easy.