我的抽象表模型有问题,当我要将数据从数据库填充到我的抽象表模型时,我不明白如何将数据从数据库移动到我的抽象表模型,这是我从数据库获取数据的语法:
public class UserJavaToDatabase {
public User getUser(int id) {
Connection koneksi = Koneksi.buka_koneksi();
try {
Statement stmt = koneksi.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM user WHERE id ="+id);
if (rs.next()){
User user = new User();
return extractUserFromResultSet(rs);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private User extractUserFromResultSet(ResultSet rs) throws SQLException{
User user = new User();
user.setID(rs.getInt("id_user_perpus"));
user.setNim(rs.getString("nim_user_perpus"));
user.setNama(rs.getString("nama_user_perpus"));
user.setProdi(rs.getString("prodi_user_perpus"));
user.setUserPinjam(rs.getInt("user_pinjam"));
user.setPassword(rs.getString("password_user_perpus"));
return user;
}
private Admin extractAdminFromResultSet(ResultSet rs) throws SQLException{
Admin admin = new Admin();
admin.setId(rs.getInt("id_admin"));
admin.setNik(rs.getString("nik_admin"));
admin.setPassword(rs.getString("password_admin"));
return admin;
}
public ArrayList getAllUser() {
Connection koneksi = Koneksi.buka_koneksi();
try {
Statement stmt = koneksi.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM user");
ArrayList users = new ArrayList();
while (rs.next()){
User user = extractUserFromResultSet(rs);
users.add(user);
}
return users;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public User getUserByUserNameAndPassword(String nim) {
Connection koneksi = Koneksi.buka_koneksi();
try {
PreparedStatement ps = koneksi.prepareStatement("SELECT * FROM user WHERE nim_user_perpus=? AND password_user_perpus=?");
ps.setString(1, nim);
ps.setString(2, nim);
ResultSet rs = ps.executeQuery();
if (rs.next()){
return extractUserFromResultSet(rs);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public Admin getAdminByUserNameAndPassword(String nik, String password){
Connection koneksi = Koneksi.buka_koneksi();
try {
PreparedStatement ps = koneksi.prepareStatement("SELECT * FROM admin WHERE nik_admin=? AND password_admin=?");
ps.setString(1, nik);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
if(rs.next()){
return extractAdminFromResultSet(rs);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public boolean insertUser(User user) {
Connection koneksi = Koneksi.buka_koneksi();
try {
PreparedStatement ps = koneksi.prepareStatement("INSERT INTO user(nim_user_perpus, nama_user_perpus, prodi_user_perpus, password_user_perpus) VALUES (?,?,?,?)");
ps.setString(1, user.getNim());
ps.setString(2, user.getNama());
ps.setString(3, user.getProdi());
ps.setString(4, user.getPassword());
int i = ps.executeUpdate();
if (i == 1){
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public boolean updateUser(User user) {
Connection koneksi = Koneksi.buka_koneksi();
try {
PreparedStatement ps = koneksi.prepareStatement("UPDATE user SET nim_user_perpus=?, nama_user_perpus=?, prodi_user_perpus=?, user_pinjam=? WHERE id=?");
ps.setString(1, user.getNim());
ps.setString(2, user.getNama());
ps.setString(3, user.getProdi());
ps.setInt(4, user.getUserPinjam());
ps.setString(5, user.getPassword());
ps.setInt(6, user.getID());
int i = ps.executeUpdate();
if (i == 1){
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public boolean deleteUser(int id) {
Connection koneksi = Koneksi.buka_koneksi();
try {
Statement stmt = koneksi.createStatement();
int i = stmt.executeUpdate("DELETE FROM user WHERE id="+id);
if(i == 1){
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
这是我从数据库中获取数据的方法。
public Set getAllUser() {
Connection koneksi = Koneksi.buka_koneksi();
try {
Statement stmt = koneksi.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM user");
Set users = new HashSet();
while (rs.next()){
User user = extractUserFromResultSet(rs);
users.add(user);
}
return users;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
这是我在jFrame中创建表的抽象表模型代码。
public class userTableModel extends AbstractTableModel {
private String [] columnNames = {"ID","Nim","Nama","Prodi","Jumlah Peminjaman"};
private Object [][] data = {{}}; //This should inputed from getAllUser() in UserJavaToDatabase class
@Override
public int getRowCount() {
return data.length;
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
}
@Override
public Class<?> getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
@Override
public String getColumnName(int column) {
return columnNames[column]; //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean isCellEditable(int row, int column)
{
if (column == 1 || column == 2){
return false;
}else
return true;
}
}
这是我的用户类别:
public class User {
private String nim, nama, prodi, password;
private int ID,userPinjam;
public User(){
}
public User(String nim){
this.nim = nim;
}
public User(String nim, String password){
this.nim = nim;
this.password = password;
}
public User(String nim, String nama, String prodi){
this.nim = nim;
this.nama = nama;
this.prodi = prodi;
this.password = nim;
}
public String getNim() {
return nim;
}
public String getNama() {
return nama;
}
public String getProdi() {
return prodi;
}
public String getPassword() {
return password;
}
public int getID() {
return ID;
}
public int getUserPinjam() {
return userPinjam;
}
public void setNim(String nim) {
this.nim = nim;
}
public void setNama(String nama) {
this.nama = nama;
}
public void setProdi(String prodi) {
this.prodi = prodi;
}
public void setPassword(String password) {
this.password = password;
}
public void setID(int ID) {
this.ID = ID;
}
public void setUserPinjam(int userPinjam) {
this.userPinjam = userPinjam;
}
}