📄 stumagdialog.java
字号:
rs = stmt.executeQuery();
while (rs.next()) {
majorid = rs.getInt(1);
}
rs.close();
stmt.close();
conn.close();
if (majorid == 0) {
return 0;
}
// int upLine = stmt.executeUpdate(sql);
else {
return majorid;
}
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 通过班级名获取班级号
* @param className
* @return
*/
public int getClassId(String className) {
try {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
int majorid = 0;
conn = conn = JdbcConnct.fetchConnection();
String sql = "select classid from classes where classname = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, className);
rs = stmt.executeQuery();
while (rs.next()) {
majorid = rs.getInt(1);
}
rs.close();
stmt.close();
conn.close();
if (majorid == 0) {
return 0;
}
// int upLine = stmt.executeUpdate(sql);
else {
return majorid;
}
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 添加功能
*
* @param evt
* @throws SQLException
*/
private void addButtonActionPerformed(ActionEvent evt) {
StudentAddDialog studentAddDialog = new StudentAddDialog(null);
studentAddDialog.setVisible(true);
studentAddDialog.submitButton.setEnabled(false);
}
/**
* 返回数据集中所用数据
*
* @param strSQL
* 查询语句
* @return
* @throws SQLException
*/
private Vector getData(String strSQL) throws SQLException {
// 建立一个JDBC对象
JdbcConnct jdbcConnection = new JdbcConnct();
// 建立一个数据库连接对象
Connection connection = null;
// 建议一个SQLStatement状态对象
Statement statement = null;
// 建立一个ResultSet结果集对象
ResultSet resultSet = null;
// 定义一个Vector变量 vecData 存储数据集
Vector<String[]> vecData = new Vector();
try {
// System.out.println("before");
connection = jdbcConnection.fetchConnection();
// System.out.println(connection);
statement = connection.createStatement();
// String sql1 = "select * from stu_class_major order by studentid
// ASC";
String sql1 = strSQL;
System.out.println(sql1);
// System.out.println(sql1);
resultSet = statement.executeQuery(sql1);
// cnum = resultSet.getMetaData().getColumnCount();
// resultSet.beforeFirst();
// row = new String[rowNum][4];
// System.out.println(rowNum);
// 将选择的政治面貌从数据库的数字对应出文字显示
while (resultSet.next()) {
String polis = resultSet.getString("polistatus");
if (polis == null) {
polis = "无";
} else if (polis.equals("1")) {
polis = "共青团员";
} else if (polis.equals("2")) {
polis = "党员";
} else if (polis.equals("3")) {
polis = "群众";
} else {
polis = "其他党派";
}
// 将选择的学生状态从数据库的数字对应出文字显示
String status = resultSet.getString("status");
// System.out.println(polis);
if (status == null) {
status = "无";
} else if (status.equals("1")) {
status = "在读";
} else if (status.equals("2")) {
status = "退学";
} else if (status.equals("3")) {
status = "休学";
} else if (status.equals("4")) {
status = "毕业";
} else {
status = "其它";
}
// 将选择的性别从数据库中的字母对应出文字显示
String gender = resultSet.getString("gender");
if (null == gender)
gender = null;
else if (gender.equals("M"))
gender = "男";
else
gender = "女";
// 显示出生日期
String birthday = resultSet.getString("birthday");
if (null == birthday) {
birthday = "";
} else {
birthday = birthday.substring(0, 10);
}
// 显示入学时间
String enterTime = resultSet.getString("entertime");
if (null == enterTime) {
enterTime = "";
} else {
enterTime = enterTime.substring(0, 10);
}
// 将学生信息添加到vecData中,以从表格中显示出来
boolean add = vecData.add(new String[] {
resultSet.getString("studentid"),
resultSet.getString("stuname"),
gender,
resultSet.getString("age"),
polis,
// resultSet.getString("birthday"),
birthday,
resultSet.getString("contactway"),
resultSet.getString("native"),
// resultSet.getString("entertime"),
enterTime, status, resultSet.getString("classname"),
resultSet.getString("info"),
resultSet.getString("remark"),
resultSet.getString("photo") });
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "表格显示错误!");
e.printStackTrace();
} finally {
connection.close();
statement.close();
resultSet.close();
}
return vecData;
}
/**
* 查询数据,并在表格中显示
* @throws SQLException
*/
private void initTable() throws SQLException {
// 定义Vector变量,存储从数据库查询来的信息
Vector vecData = new Vector();
// 定义SQL语句
String sql1 = "select * from stu_class_major order by studentid ASC";
jTable1Model = new DefaultTableModel(row, coloumn);
this.stuTable.setModel(jTable1Model);
stuTable.getTableHeader().setBounds(0, 0, 273, 21);
// stuTable.setRowMargin(30);
// 依次将各行插入数据
vecData = getData(sql1);
for (int i = 0; i < vecData.size(); i++) {
jTable1Model.addRow((String[]) vecData.get(i));
}
}
/**
* 退出功能
*
* @param evt
*/
private void exsitButtonActionPerformed(ActionEvent evt) {
System.out.println("exsitButton.actionPerformed, event=" + evt);
// TODO add your code for exsitButton.actionPerformed
this.dispose();
}
/**
* 编辑功能
*
* @param evt
*/
private void commitButtonActionPerformed(ActionEvent evt) {
// System.out.println("commitButton.actionPerformed, event=" + evt);
// TODO add your code for commitButton.actionPerformed
int row = this.stuTable.getSelectedRow();
if (row != -1) {
String stuId = (String) this.stuTable.getValueAt(row, 0);
String studentName = (String) this.stuTable.getValueAt(row, 1);
String gender = (String) this.stuTable.getValueAt(row, 2);
String age = (String) this.stuTable.getValueAt(row, 3);
String polistatus = (String) this.stuTable.getValueAt(row, 4);
String birthday = (String) this.stuTable.getValueAt(row, 5);
String contactway = (String) this.stuTable.getValueAt(row, 6);
String native1 = (String) this.stuTable.getValueAt(row, 7);
String entertime = (String) this.stuTable.getValueAt(row, 8);
String status = (String) this.stuTable.getValueAt(row, 9);
String className = (String) this.stuTable.getValueAt(row, 10);
String stuInfo = (String) this.stuTable.getValueAt(row, 11);
String remark = (String) this.stuTable.getValueAt(row, 12);
String photoPath = (String) this.stuTable.getValueAt(row, 13);
StudentAddDialog studentAddDialog = new StudentAddDialog(null);
studentAddDialog.setTitle("修改学生信息");
studentAddDialog.setVisible(true);
studentAddDialog.addButton.setEnabled(false);
studentAddDialog.studentIdTextField.setText(stuId);
studentAddDialog.studentIdTextField.setEditable(false);
studentAddDialog.stuNameTextField.setText(studentName);
if (null == gender) {
studentAddDialog.manRadioButton.setSelected(false);
studentAddDialog.famelRadioButton.setSelected(false);
} else if (gender.equals("男"))
studentAddDialog.manRadioButton.setSelected(true);
else if (gender.equals("女"))
studentAddDialog.famelRadioButton.setSelected(true);
studentAddDialog.ageTextField.setText(age);
studentAddDialog.poliStatusComboBox.setSelectedItem(polistatus);
studentAddDialog.birthDayTextField.setText(birthday);
studentAddDialog.contactWayTextField.setText(contactway);
studentAddDialog.enterTimeTextField.setText(entertime);
studentAddDialog.nativeTextField.setText(native1);
studentAddDialog.classIdTextField.setText(className);
studentAddDialog.remarkTextArea.setText(remark);
studentAddDialog.pathTextField.setText(photoPath);
if (null != photoPath) {
ImageIcon icon = new ImageIcon(photoPath);
studentAddDialog.photoLabel.setIcon(icon);
}
} else {
JOptionPane.showMessageDialog(this, "没有选定行!");
}
// this.stuTableModel.setValueAt(stuId, row, 0);
// this.stuTableModel.setValueAt(studentName, row, 1);
// this.stuTableModel.setValueAt(gender, row, 2);
// this.stuTableModel.setValueAt(age, row, 3);
// this.stuTableModel.setValueAt(polistatus, row, 4);
// this.stuTableModel.setValueAt(birthday, row, 5);
// this.stuTableModel.setValueAt(contactway, row, 6);
// this.stuTableModel.setValueAt(native1, row, 7);
// this.stuTableModel.setValueAt(entertime, row, 8);
// this.stuTableModel.setValueAt(status, row, 9);
// this.stuTableModel.setValueAt(className, row, 10);
}
/**
* 查询功能,分为全部查询,按学号查询,按姓名查询和按性别查询
*
* @param evt
*/
private void querButtonActionPerformed(ActionEvent evt) {
// 定义boolean型标志位bisRight ,用户输入是否正确,初始为false
boolean bisRight = false;
// 定义SQL语句
String strSQL = "";
// 定义Vector变量,存储从数据库查询来的信息
Vector vecData = new Vector();
// 根据用户输入 返回SQL语句
strSQL = getSQL();
// 校验sql返回值
if (strSQL.equals(null)) {
bisRight = false;
} else {
bisRight = true;
}
if (bisRight) {
try {
vecData = getData(strSQL);
} catch (SQLException e) {
e.printStackTrace();
}
// 清空表格中的数据
jTable1Model = new DefaultTableModel(row, coloumn);
this.stuTable.setModel(jTable1Model);
// 依次为各行插入数据
for (int i = 0; i < vecData.size(); i++) {
jTable1Model.addRow((String[]) vecData.get(i));
}
if (vecData.size() == 0) {
JOptionPane.showMessageDialog(this, "无法找到该记录!");
}
}
}
/**
* 删除功能
*
* @param evt
*/
private void deleteButtonActionPerformed(ActionEvent evt) {
// System.out.println("deleteButton.actionPerformed, event=" + evt);
// TODO add your code for deleteButton.actionPerformed
int row = this.stuTable.getSelectedRow();
int resultDelete = JOptionPane.showConfirmDialog(this, "确定要删除此条记录吗?",
"删除确定框", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
System.out.println(row);
if (row != -1) {
if (resultDelete == JOptionPane.YES_OPTION)// 如果选择的是确定,则执行删除操作
{
// 从表格中获取学号
String studentId = (String) this.stuTable.getValueAt(row, 0);
try {
Connection conn = null;
String sql = "delete from student where " + " studentid="
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -