DB に新しく追加または変更された情報で JTable を更新したい 現在、クリックしたとき、showDetails
または DB に 1 つのレコードを挿入した後に一度だけ機能します。
新しいエントリが追加または削除されるたびに情報を何度も表示するのではなく、一度だけ情報を表示します
コードは GUIClass のものです
package studentmanagementsystem.gui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.Vector;
import studentmanagementsystem.entity.*;
import studentmanagementsystem.backend.*;
public class StudentPersonalGui extends JFrame implements ActionListener{
JLabel heading,lenroll,lDOB,lgender,lmobileno,lemailid,lfathername,lname;
JLabel lmothername,laddress;
JTextField tenroll,tDOB,tmobileno,temailid,tfathername,tname;
JTextField tmothername,taddress;
JComboBox<String> cgender;
JButton insert,retrieve;
JButton reset;
JPanel p1,dataPanel;
String gender[]={"Male","Female"};
Font f1=new Font("BOLD",Font.ITALIC,25);
Color c1=Color.white;
Color c2=Color.lightGray ;
Color c3=new Color(50,100,10);
boolean insert_status;
StudentPersonal sp;
public StudentPersonalGui(){
setLayout(null);
heading=new JLabel("Student Personal Record");
heading.setFont(f1);
heading.setForeground(c3);
p1=new JPanel();
p1.add(heading);
p1.setBackground(c2);
p1.setLayout(null);
p1.setBounds(200,20,400,60);
heading.setBounds(50,10,300,30);
add(p1);
lenroll=new JLabel("Enrollment No.:");
lname=new JLabel("Student Name:");
lDOB=new JLabel("Date of Birth:");
lmobileno=new JLabel("Mobile No.:");
lemailid=new JLabel("Email Id:");
lgender=new JLabel("Gender:");
lfathername=new JLabel("Father Name:");
lmothername=new JLabel("Mother Name:");
laddress=new JLabel("Address:");
tenroll=new JTextField(12);
tname=new JTextField(12);
tDOB=new JTextField(12);
tmobileno=new JTextField(12);
temailid=new JTextField(12);
tfathername=new JTextField(12);
tmothername=new JTextField(12);
taddress=new JTextField(12);
cgender=new JComboBox<String>(gender);
insert=new JButton("Add Record");
retrieve=new JButton("View Details");
reset=new JButton("Reset");
add(lenroll);
add(lDOB);
add(lmobileno);
add(lgender);
add(lemailid);
add(lfathername);
add(lmothername);
add(lname);
add(laddress);
add(tenroll);
add(tDOB);
add(tmobileno);
add(cgender);
add(temailid);
add(tfathername);
add(tmothername);
add(tname);
add(taddress);
add(insert);
add(retrieve);
add(reset);
lenroll.setBounds(100,100,100,20);
tenroll.setBounds(220,100,150,20);
lname.setBounds(450,100,100,20);
tname.setBounds(570,100,200,20);
lDOB.setBounds(100,130,100,20);
tDOB.setBounds(220,130,120,20);
lgender.setBounds(450,130,100,20);
cgender.setBounds(570,130,100,20);
lmobileno.setBounds(100,160,100,20);
tmobileno.setBounds(220,160,120,20);
lemailid.setBounds(450,160,200,20);
temailid.setBounds(550,160,200,20);
lfathername.setBounds(100,190,100,20);
tfathername.setBounds(220,190,200,20);
lmothername.setBounds(100,220,100,20);
tmothername.setBounds(220,220,200,20);
laddress.setBounds(100,250,200,20);
taddress.setBounds(220,250,350,20);
insert.setBounds(150,300,150,20);
reset.setBounds(350,300,80,20);
retrieve.setBounds(480,300,150,20);
insert.addActionListener(this);
reset.addActionListener(this);
retrieve.addActionListener(this);
dataPanel=new JPanel();
dataPanel.setBounds(50,350,730,250);
dataPanel.setLayout(new BorderLayout());
dataPanel.setBackground(Color.WHITE);
add(dataPanel);
setResizable(false);
setVisible(true);
setSize(850,700);
setTitle("Student Record Management");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae){
if(ae.getActionCommand()=="Add Record"){
sp=new StudentPersonal();
String sDOB=tDOB.getText();
String sname=tname.getText();
String gender=(String)cgender.getSelectedItem();
String emailid=temailid.getText();
String fathername=tfathername.getText();
String mothername=tmothername.getText();
String address=taddress.getText();
long enrollno=0;
long mobileno=0;
try{
enrollno=Long.parseLong(tenroll.getText());
mobileno=Long.parseLong(tmobileno.getText());
}catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(null,"Alphabets cann't be inserted in phone field","Information", JOptionPane.ERROR_MESSAGE);
}
sp.setSenrollno(enrollno);
sp.setSname(sname);
sp.setSDOB(sDOB);
sp.setSgender(gender);
sp.setSmobileno(mobileno);
sp.setSemailid(emailid);
sp.setSfathername(fathername);
sp.setSmothername(mothername);
sp.setSaddress(address);
insert_status=new CRUDOperations().insert(sp);
if(insert_status){
JOptionPane.showMessageDialog(null,"Record Inserted Successfully","Information", JOptionPane.INFORMATION_MESSAGE);
showData();
}
}
else if(ae.getActionCommand()=="View Details"){
showData();
}
else{
tenroll.setText("");
tDOB.setText("");
tmobileno.setText("");
temailid.setText("");
tfathername.setText("");
tname.setText("");
tmothername.setText("");
taddress.setText("");
cgender.setSelectedIndex(0);
}
}
public void showData(){
JTable table;
JScrollPane pane;
Vector<String> columnname=new Vector<String>();
Vector<Vector<String>> rowData=new Vector<Vector<String>>();
Vector<String> student;
try{
ResultSet rs=new CRUDOperations().retrieve_Data();
if(rs!=null){
ResultSetMetaData rsmd=rs.getMetaData();
int columnCount=rsmd.getColumnCount();
for(int j=1;j<=columnCount;j++){
columnname.add(rsmd.getColumnName(j));
}
while(rs.next()){
student=new Vector<String>();
student.add(rs.getString(1));
student.add(rs.getString(2));
student.add(rs.getString(3));
student.add(rs.getString(4));
student.add(rs.getString(5));
student.add(rs.getString(6));
student.add(rs.getString(7));
student.add(rs.getString(8));
student.add(rs.getString(9));
rowData.add(student);
}
rs.close();
table = new JTable(rowData,columnname);
pane=new JScrollPane(table);
dataPanel.add(pane,BorderLayout.CENTER);
add(dataPanel);
validate();
repaint();
}
}catch(SQLException sqle){
}
}
}
CRUDOperation クラスは、ShowDetails ボタンの DB 操作に使用されます CRUDOperation 関数 retrieve_Data が呼び出され、JTable データの設定に使用される resultSet が返されます
package studentmanagementsystem.backend;
import java.awt.Color;
import java.sql.*;
import java.util.Vector;
import studentmanagementsystem.entity.*;
import studentmanagementsystem.gui.*;
public class CRUDOperations{
Connection con;
PreparedStatement stat;
StudentPersonal sp;
boolean insert_status,delete_status,update_status,login_status;
public boolean insert(StudentPersonal sp){
con=new ConnectionManager().makeConn();
try{
long enrollno=sp.getSenrollno();
String sname=sp.getSname();
String sDOB=sp.getSDOB();
String gender=sp.getSgender();
String emailid=sp.getSemailid();
long mobileno=sp.getSmobileno();
String fathername=sp.getSfathername();
String mothername=sp.getSmothername();
String address=sp.getSaddress();
String insertSP="insert into StudentPersonal values(?,?,?,?,?,?,?,?,?)";
stat=con.prepareStatement(insertSP);
stat.setLong(1,enrollno);
stat.setString(2,sname);
stat.setString(3,sDOB);
stat.setString(4,gender);
stat.setLong(5,mobileno);
stat.setString(6,emailid);
stat.setString(7,fathername);
stat.setString(8,mothername);
stat.setString(9,address);
int i=stat.executeUpdate();
if(i>0){
insert_status=true;
}
}catch(Exception e){
}
return insert_status;
}
public ResultSet retrieve_Data(){
ResultSet rs=null;
try{
con=new ConnectionManager().makeConn();
String retrieve="select * from StudentPersonal";
stat=con.prepareStatement(retrieve);
rs=stat.executeQuery();
}catch(SQLException sql){
System.out.println(sql);
}
return rs;
}
}