Buat database Mysql, kemudian buat pada netbeans seperti gambar dibawah ini :
1. Karyawan.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dbapp;
/**
*
* @author isca
*/
public class karyawan {
private String nip;
private String nama;
private String alamat;
public karyawan(){
}
public String getnip(){
return nip;
}
public void setnip(String nip){
this.nip = nip;
}
public String getnama(){
return nip;
}
public void setnama(String nama){
this.nama = nama;
}
public String getalamat(){
return alamat;
}
public void setalamat(String alamat){
this.alamat = alamat;
}
}
- KaryawanManage.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dbapp;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author isca
*/
public class karyawanManage {
Connection con = null;
Statement st = null;
String url ="jdbc::mysql://localhost:3306/dbapp_11080978";
String user = "root";
String pass = "password";
/**Creates a new instance of karyawanManage*/
public karyawanManage(){
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection(url, user, pass);
st=con.createStatement();
}
catch(Exception ex){
ex.printStackTrace();
}
}
public List
getKaryawans(){
ResultSet rs=null;
List
karya= new ArrayList();
try{
rs=st.executeQuery("SELECT NIP,NAMA,ALAMAT FROM tkaryawan");
while(rs.next()){
karyawan k = new karyawan();
k.setnip(rs.getString(1));
k.setnama(rs.getString(2));
k.setalamat(rs.getString(3));
karya.add(k);
}
}catch(Exception ex){
ex.printStackTrace();
}
return karya;
}
public int insert(karyawan k){
int result = 0 ;
try{
result = st.executeUpdate("INSERT INTO tkaryawan(nip,nama,alamat)"+"VALUES("+k.getnip()+",'"+k.getnama()+"',"+k.getalamat()+"')");
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
public int delete(karyawan k){
int result = 0 ;
try{
result = st.executeUpdate("DELETE FROM tkaryawan WHERE ID = "+k.getnip()+"");
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
}
0 komentar:
Posting Komentar