下面是我的Java应用程序的代码,其中它使用mysql在充满学生数据的数据库中提取学生表。以下代码特别允许您从删除或删除特定学生的角度来管理数据。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.swing.table.DefaultTableModel;
//import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Calendar;
public class manage_students_form extends javax.swing.JFrame {
students stdnt = new students();
public manage_students_form() {
initComponents();
stdnt.fillStudentJtable(jTable1, "");
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
manage_student_form = new javax.swing.JPanel();
title_label = new javax.swing.JLabel();
first_name_label = new javax.swing.JLabel();
last_name_label = new javax.swing.JLabel();
gender_label = new javax.swing.JLabel();
dob_label = new javax.swing.JLabel();
phone_number_label = new javax.swing.JLabel();
email_label = new javax.swing.JLabel();
physical_address_label = new javax.swing.JLabel();
residence_label = new javax.swing.JLabel();
student_id_textfield = new javax.swing.JTextField();
last_name_textfield = new javax.swing.JTextField();
phone_number_textfield = new javax.swing.JTextField();
email_address_textfield = new javax.swing.JTextField();
residence_textfield = new javax.swing.JTextField();
male_radiobutton = new javax.swing.JRadioButton();
female_radiobutton = new javax.swing.JRadioButton();
jScrollPane1 = new javax.swing.JScrollPane();
physical_address_TextArea = new javax.swing.JTextArea();
edit_student_button = new javax.swing.JButton();
cancel_button = new javax.swing.JButton();
dateChooser = new datechooser.beans.DateChooserCombo();
first_name_textfield = new javax.swing.JTextField();
student_id_label = new javax.swing.JLabel();
remove_button = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
manage_student_form.setBackground(new java.awt.Color(0, 204, 153));
title_label.setFont(new java.awt.Font("Times New Roman", 3, 28)); // NOI18N
title_label.setText("Manage Students");
first_name_label.setFont(new java.awt.Font("Times New Roman", 3, 18)); // NOI18N
first_name_label.setText("FirstName");
last_name_label.setFont(new java.awt.Font("Times New Roman", 3, 18)); // NOI18N
last_name_label.setText("LastName");
gender_label.setFont(new java.awt.Font("Times New Roman", 3, 18)); // NOI18N
gender_label.setText("Gender");
dob_label.setFont(new java.awt.Font("Times New Roman", 3, 18)); // NOI18N
dob_label.setText("Date of Birth");
phone_number_label.setFont(new java.awt.Font("Times New Roman", 3, 18)); // NOI18N
phone_number_label.setText("Phone Number");
email_label.setFont(new java.awt.Font("Times New Roman", 3, 18)); // NOI18N
email_label.setText("Email Address");
physical_address_label.setFont(new java.awt.Font("Times New Roman", 3, 18)); // NOI18N
physical_address_label.setText("Physical Address");
residence_label.setFont(new java.awt.Font("Times New Roman", 3, 18)); // NOI18N
residence_label.setText("Residence");
phone_number_textfield.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
phone_number_textfieldActionPerformed(evt);
}
});
phone_number_textfield.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
phone_number_textfieldKeyTyped(evt);
}
});
male_radiobutton.setText("Male");
female_radiobutton.setText("Female");
physical_address_TextArea.setColumns(20);
physical_address_TextArea.setRows(5);
jScrollPane1.setViewportView(physical_address_TextArea);
edit_student_button.setFont(new java.awt.Font("Times New Roman", 3, 14)); // NOI18N
edit_student_button.setText("Edit Student");
edit_student_button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
edit_student_buttonActionPerformed(evt);
}
});
cancel_button.setFont(new java.awt.Font("Times New Roman", 3, 14)); // NOI18N
cancel_button.setText("Cancel");
cancel_button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancel_buttonActionPerformed(evt);
}
});
student_id_label.setFont(new java.awt.Font("Times New Roman", 3, 18)); // NOI18N
student_id_label.setText("Student ID");
remove_button.setFont(new java.awt.Font("Times New Roman", 3, 14)); // NOI18N
remove_button.setText("Remove");
remove_button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
remove_buttonActionPerformed(evt);
}
});
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Student ID", "FirstName", "LastName", "Gender", "Date of Birth", "Phone Number", "Email Address", "Physical Address", "Residence"
}
));
jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jTable1MouseClicked(evt);
}
});
jScrollPane2.setViewportView(jTable1);
javax.swing.GroupLayout manage_student_formLayout = new javax.swing.GroupLayout(manage_student_form);
manage_student_form.setLayout(manage_student_formLayout);
manage_student_formLayout.setHorizontalGroup(
manage_student_formLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(manage_student_formLayout.createSequentialGroup()
.addGap(157, 157, 157)
.addComponent(title_label))
.addGroup(manage_student_formLayout.createSequentialGroup()
.addGroup(manage_student_formLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(manage_student_formLayout.createSequentialGroup()
.addComponent(phone_number_label)
.addGap(6, 6, 6)
.addComponent(phone_number_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(manage_student_formLayout.createSequentialGroup()
.addComponent(dob_label)
.addGap(1, 1, 1)
.addComponent(dateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(manage_student_formLayout.createSequentialGroup()
.addComponent(email_label)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(email_address_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(manage_student_formLayout.createSequentialGroup()
.addGap(10, 10, 10)
.addGroup(manage_student_formLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(manage_student_formLayout.createSequentialGroup()
.addComponent(student_id_label)
.addGap(8, 8, 8)
.addComponent(student_id_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(manage_student_formLayout.createSequentialGroup()
.addComponent(first_name_label)
.addGap(10, 10, 10)
.addComponent(first_name_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(manage_student_formLayout.createSequentialGroup()
.addComponent(last_name_label)
.addGap(14, 14, 14)
.addComponent(last_name_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(manage_student_formLayout.createSequentialGroup()
.addComponent(gender_label)
.addGap(35, 35, 35)
.addComponent(male_radiobutton)
.addGap(31, 31, 31)
.addComponent(female_radiobutton)))))
.addGap(16, 16, 16)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 1010, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(manage_student_formLayout.createSequentialGroup()
.addComponent(physical_address_label)
.addGap(13, 13, 13)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(30, 30, 30)
.addComponent(remove_button, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(103, 103, 103)
.addComponent(edit_student_button, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(83, 83, 83)
.addComponent(cancel_button, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(manage_student_formLayout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(residence_label)
.addGap(45, 45, 45)
.addComponent(residence_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE))
);
manage_student_formLayout.setVerticalGroup(
manage_student_formLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(manage_student_formLayout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(title_label)
.addGap(41, 41, 41)
.addGroup(manage_student_formLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(manage_student_formLayout.createSequentialGroup()
.addGap(20, 20, 20)
.addGroup(manage_student_formLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(student_id_label)
.addComponent(student_id_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(26, 26, 26)
.addGroup(manage_student_formLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(first_name_label)
.addComponent(first_name_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(16, 16, 16)
.addGroup(manage_student_formLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(last_name_label)
.addComponent(last_name_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(26, 26, 26)
.addGroup(manage_student_formLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(gender_label)
.addComponent(male_radiobutton)
.addComponent(female_radiobutton))
.addGap(32, 32, 32)
.addGroup(manage_student_formLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(phone_number_label)
.addComponent(phone_number_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(46, 46, 46)
.addGroup(manage_student_formLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(dob_label)
.addComponent(dateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(38, 38, 38)
.addGroup(manage_student_formLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(email_label)
.addComponent(email_address_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 403, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(7, 7, 7)
.addGroup(manage_student_formLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(manage_student_formLayout.createSequentialGroup()
.addGap(60, 60, 60)
.addComponent(physical_address_label))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(manage_student_formLayout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(remove_button))
.addGroup(manage_student_formLayout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(edit_student_button))
.addGroup(manage_student_formLayout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(cancel_button)))
.addGap(20, 20, 20)
.addGroup(manage_student_formLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(residence_label)
.addComponent(residence_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(manage_student_form, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(manage_student_form, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void phone_number_textfieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void phone_number_textfieldKeyTyped(java.awt.event.KeyEvent evt) {
if(!Character.isDigit(evt.getKeyChar())){
evt.consume();
}
}
private void edit_student_buttonActionPerformed(java.awt.event.ActionEvent evt) {
//
}
private void cancel_buttonActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void remove_buttonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
int rowIndex = jTable1.getSelectedRow();
DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
if(model.getValueAt(rowIndex, 3).toString().equals("Male") ){
male_radiobutton.setSelected(true);
female_radiobutton.setSelected(false);
}else{
female_radiobutton.setSelected(true);
male_radiobutton.setSelected(false);
}
student_id_textfield.setText(model.getValueAt(rowIndex, 0).toString());
first_name_textfield.setText(model.getValueAt(rowIndex, 1).toString());
last_name_textfield.setText(model.getValueAt(rowIndex, 2).toString());
phone_number_textfield.setText(model.getValueAt(rowIndex, 5).toString());
email_address_textfield.setText(model.getValueAt(rowIndex, 6).toString());
physical_address_TextArea.setText(model.getValueAt(rowIndex, 7).toString());
residence_textfield.setText(model.getValueAt(rowIndex, 8).toString());
无论您选择的是哪一行数据,都应从students表中获取并根据所选数据自动填写“ manage students form” java表单中的字段。问题是它会获取并显示表中所有字段的数据,但不会从我的数据库中获取并显示“出生日期”数据,也不会显示该数据,并且它不会基于此更改所选学生的“出生日期”特定的学生。
// Date dob;
// try {
// dob = new SimpleDateFormat("yyyy-MM-dd").parse(model.getValueAt(rowIndex, 4).toString());
// dateChooser.getSelectedDate(dob);
// } catch (ParseException ex) {
// Logger.getLogger(manage_students_form.class.getName()).log(Level.SEVERE, null, ex);
// }
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new manage_students_form().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton cancel_button;
private datechooser.beans.DateChooserCombo dateChooser;
private javax.swing.JLabel dob_label;
private javax.swing.JButton edit_student_button;
private javax.swing.JTextField email_address_textfield;
private javax.swing.JLabel email_label;
private javax.swing.JRadioButton female_radiobutton;
private javax.swing.JLabel first_name_label;
private javax.swing.JTextField first_name_textfield;
private javax.swing.JLabel gender_label;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTable jTable1;
private javax.swing.JLabel last_name_label;
private javax.swing.JTextField last_name_textfield;
private javax.swing.JRadioButton male_radiobutton;
private javax.swing.JPanel manage_student_form;
private javax.swing.JLabel phone_number_label;
private javax.swing.JTextField phone_number_textfield;
private javax.swing.JTextArea physical_address_TextArea;
private javax.swing.JLabel physical_address_label;
private javax.swing.JButton remove_button;
private javax.swing.JLabel residence_label;
private javax.swing.JTextField residence_textfield;
private javax.swing.JLabel student_id_label;
private javax.swing.JTextField student_id_textfield;
private javax.swing.JLabel title_label;
// End of variables declaration
}
我尝试过:
Date dob;
try {
dob = new SimpleDateFormat("yyyy-MM-dd").parse(model.getValueAt(rowIndex, 4).toString());
dateChooser.getSelectedDate(dob);
} catch (ParseException ex) {
Logger.getLogger(manage_students_form.class.getName()).log(Level.SEVERE, null, ex);
}
,但不起作用。 我该如何纠正。问题是日期功能。请帮忙。**