gpt4 book ai didi

Java、Swing - 删除后 Jtable 行保留

转载 作者:行者123 更新时间:2023-11-29 19:04:57 25 4
gpt4 key购买 nike

我有一个显示来自mysql的数据的JTable,下面的代码是有效的(可以插入,更新,删除)但是如果我删除一行并创建另一个具有相同id的行,则该行中的先前数据(在我删除之前)它)出现而不是新数据。

插入和删除代码

private void simpanBtnActionPerformed(java.awt.event.ActionEvent evt) {                                          
String hantaranID = hantaranIDText.getText();
String namaLengkap = namaLengkapET.getText();
String alamat = jTextArea1.getText();
String hp = noHp.getText();

Date pengambilan = jXDatePicker1.getDate();
Date pengembalian = jXDatePicker2.getDate();

if (hantaranID.isEmpty()){
JOptionPane.showMessageDialog(null, "Hantaran ID tidak boleh kosong.");
} else if (namaLengkap.isEmpty()){
JOptionPane.showMessageDialog(null, "Nama lengkap tidak boleh kosong.");
} else if (alamat.isEmpty()) {
JOptionPane.showMessageDialog(null, "Alamat tidak boleh kosong.");
} else if (hp.isEmpty()){
JOptionPane.showMessageDialog(null, "Nomor Hand Phone tidak boleh kosong.");
} else if (pengambilan != null && pengembalian != null){

try {
DateFormat sysDate = new SimpleDateFormat("yyyy/MM/dd");
String tglPengambilan = sysDate.format(jXDatePicker1.getDate()).toString();
String tglPengembalian = sysDate.format(jXDatePicker2.getDate()).toString();
Connection conn = MyDBConnection.getConnection();
String insert = "insert into hantaran (hantaran_id, nama_lengkap, alamat, no_hp, tgl_pengambilan, tgl_pengembalian)"
+ "values (?, ?, ? , ? , ?, ?)";
PreparedStatement insertHantaran = conn.prepareStatement(insert, Statement.RETURN_GENERATED_KEYS);
insertHantaran.setString(1, hantaranID);
insertHantaran.setString(2, namaLengkap);
insertHantaran.setString(3, alamat);
insertHantaran.setString(4, hp);
insertHantaran.setString(5, tglPengambilan);
insertHantaran.setString(6, tglPengembalian);
insertHantaran.executeUpdate();

hantaranTabel.revalidate();
hantaranList.clear();
hantaranList.addAll( hantaranQuery.getResultList());

hantaranIDText.setText("");
namaLengkapET.setText("");
jTextArea1.setText("");
noHp.setText("");
jXDatePicker1.setDate(null);
jXDatePicker2.setDate(null);

} catch (Exception e) {
e.printStackTrace();
}

} else {

JOptionPane.showMessageDialog(null, "Tanggal Pengambilan dan Pengembalian tidak boleh kosong.");

}


// TODO add your handling code here:
}

private void hapusBtnActionPerformed(java.awt.event.ActionEvent evt) {

String id = hantaranIDText.getText();

Object[] options = { "YA", "Tidak" };
int choice = JOptionPane.showOptionDialog(null,
"Hapus data ini??",
"Hapus..!",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
if (choice == JOptionPane.YES_OPTION){
try {
Connection conn = MyDBConnection.getConnection();
String reqq = "DELETE FROM hantaran WHERE hantaran_id = ?";
PreparedStatement delete = conn.prepareStatement(reqq);
delete.setString(1, id);
delete.executeUpdate();

hantaranTabel.revalidate();
hantaranList.clear();
hantaranList.addAll( hantaranQuery.getResultList());

editBtn.setText("EDIT");
hantaranIDText.setText("");
namaLengkapET.setText("");
jTextArea1.setText("");
noHp.setText("");
jXDatePicker1.setDate(null);
jXDatePicker2.setDate(null);

hapusBtn.setEnabled(false);
simpanBtn.setEnabled(true);
} catch (Exception ex) {
Logger.getLogger(HennaPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

我的类(class)

package aplikasi_mahar;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
*
* @author User
*/
@Entity
@Table(name = "hantaran", catalog = "mahardb", schema = "")
@NamedQueries({
@NamedQuery(name = "Hantaran.findAll", query = "SELECT h FROM Hantaran h"),
@NamedQuery(name = "Hantaran.findByHantaranId", query = "SELECT h FROM Hantaran h WHERE h.hantaranId = :hantaranId"),
@NamedQuery(name = "Hantaran.findByNamaLengkap", query = "SELECT h FROM Hantaran h WHERE h.namaLengkap = :namaLengkap"),
@NamedQuery(name = "Hantaran.findByAlamat", query = "SELECT h FROM Hantaran h WHERE h.alamat = :alamat"),
@NamedQuery(name = "Hantaran.findByNoHp", query = "SELECT h FROM Hantaran h WHERE h.noHp = :noHp"),
@NamedQuery(name = "Hantaran.findByTglPengambilan", query = "SELECT h FROM Hantaran h WHERE h.tglPengambilan = :tglPengambilan"),
@NamedQuery(name = "Hantaran.findByTglPengembalian", query = "SELECT h FROM Hantaran h WHERE h.tglPengembalian = :tglPengembalian")})
public class Hantaran implements Serializable {
@Transient
private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "hantaran_id")
private Integer hantaranId;
@Basic(optional = false)
@Column(name = "nama_lengkap")
private String namaLengkap;
@Basic(optional = false)
@Column(name = "alamat")
private String alamat;
@Basic(optional = false)
@Column(name = "no_hp")
private String noHp;
@Basic(optional = false)
@Column(name = "tgl_pengambilan")
@Temporal(TemporalType.DATE)
private Date tglPengambilan;
@Basic(optional = false)
@Column(name = "tgl_pengembalian")
@Temporal(TemporalType.DATE)
private Date tglPengembalian;

public Hantaran() {
}

public Hantaran(Integer hantaranId) {
this.hantaranId = hantaranId;
}

public Hantaran(Integer hantaranId, String namaLengkap, String alamat, String noHp, Date tglPengambilan, Date tglPengembalian) {
this.hantaranId = hantaranId;
this.namaLengkap = namaLengkap;
this.alamat = alamat;
this.noHp = noHp;
this.tglPengambilan = tglPengambilan;
this.tglPengembalian = tglPengembalian;
}

public Integer getHantaranId() {
return hantaranId;
}

public void setHantaranId(Integer hantaranId) {
Integer oldHantaranId = this.hantaranId;
this.hantaranId = hantaranId;
changeSupport.firePropertyChange("hantaranId", oldHantaranId, hantaranId);
}

public String getNamaLengkap() {
return namaLengkap;
}

public void setNamaLengkap(String namaLengkap) {
String oldNamaLengkap = this.namaLengkap;
this.namaLengkap = namaLengkap;
changeSupport.firePropertyChange("namaLengkap", oldNamaLengkap, namaLengkap);
}

public String getAlamat() {
return alamat;
}

public void setAlamat(String alamat) {
String oldAlamat = this.alamat;
this.alamat = alamat;
changeSupport.firePropertyChange("alamat", oldAlamat, alamat);
}

public String getNoHp() {
return noHp;
}

public void setNoHp(String noHp) {
String oldNoHp = this.noHp;
this.noHp = noHp;
changeSupport.firePropertyChange("noHp", oldNoHp, noHp);
}

public Date getTglPengambilan() {
return tglPengambilan;
}

public void setTglPengambilan(Date tglPengambilan) {
Date oldTglPengambilan = this.tglPengambilan;
this.tglPengambilan = tglPengambilan;
changeSupport.firePropertyChange("tglPengambilan", oldTglPengambilan, tglPengambilan);
}

public Date getTglPengembalian() {
return tglPengembalian;
}

public void setTglPengembalian(Date tglPengembalian) {
Date oldTglPengembalian = this.tglPengembalian;
this.tglPengembalian = tglPengembalian;
changeSupport.firePropertyChange("tglPengembalian", oldTglPengembalian, tglPengembalian);
}

@Override
public int hashCode() {
int hash = 0;
hash += (hantaranId != null ? hantaranId.hashCode() : 0);
return hash;
}

@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Hantaran)) {
return false;
}
Hantaran other = (Hantaran) object;
if ((this.hantaranId == null && other.hantaranId != null) || (this.hantaranId != null && !this.hantaranId.equals(other.hantaranId))) {
return false;
}
return true;
}

@Override
public String toString() {
return "aplikasi_mahar.Hantaran[ hantaranId=" + hantaranId + " ]";
}

public void addPropertyChangeListener(PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
}

public void removePropertyChangeListener(PropertyChangeListener listener) {
changeSupport.removePropertyChangeListener(listener);
}

}

连接类别

public class MyDBConnection {

static private Connection connection;

public static Connection getConnection() throws Exception{
if(connection == null){
//JDBC
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mahardb", "root", "");
}
return connection;
}

}

注释:我使用持久连接,java jdk7

最佳答案

您必须为 TableModel 设置新数据或从 TableModel 中删除已删除的数据/行并调用 fireTableDataChanged 方法:

yourTableModel.setData(getYourData());
yourTableModel.fireTableDataChanged();

关于Java、Swing - 删除后 Jtable 行保留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43581099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com