📄 coursemagdialog.java
字号:
// 获取用户输入信息 strcourseID = this.CourseIdTextField.getText();
// 向数据库写入数据 if (bisRight) {
// 创建SQL语句 strSQL = "insert into course ( coursename, period ,credithour) values ('"
+ course.getCourseName()
+ "',"
+ course.getPreiod()
+ ","
+ course.getCreditHour() + ")";
// 执行SQL语句 updateLine = jdbcConnection.updateData(strSQL);
if (updateLine > 0) {
System.out.println(updateLine);
JOptionPane.showMessageDialog(this, "成功增加!");
// 用户界面增加数据 CourseQueryTableModel.addRow(new String[] { strcourseID,
course.getCourseName(),
String.valueOf(course.getPreiod()),
String.valueOf(course.getCreditHour()) });
} else {
JOptionPane.showMessageDialog(this, "增加失败!请重试!");
}
// 新增、修改、删除可用、保存不可用 this.NewButton.setEnabled(true);
this.ModifyButton1.setEnabled(true);
this.DeleteButton1.setEnabled(true);
this.SaveButton.setEnabled(false);
// 学科号自动增长,显示学科号,文本框设为不可用 this.CourseIdTextField.setEnabled(false);
}
}
/**
* 修改课程信息 * @param evt
*/
private void ModifyButton1ActionPerformed(ActionEvent evt) {
// 修改 // 建立一个JDBC对象 JdbcConnct jdbcConnection = new JdbcConnct();
// 定义鼠标点击行 int nrow = 0;
// 定义boolean型变量,用户是否点击行,初始化为False
boolean bisRight = false;
// 定义SQL语句 String strSQL = "";
// 更新行数 int型 updateLine
int updateLine = 0;
// 定义Course对象 Course course = new Course();
// 获取鼠标点击行 nrow = this.CourseQueryTable.getSelectedRow();
// 判断用户是否选择 if (nrow < 0) {
bisRight = false;
JOptionPane.showMessageDialog(this, "您没有选择数据!在修改前请选择一条数据!");
} else {
bisRight = true;
}
// 获得修改信息 course = getUserInput(course);
// 用户输入校验 if (course.getCourseName().equals(null)
|| course.getPreiod() > 200
|| course.getCreditHour() > 5) {
bisRight = false;
} else {
bisRight = true;
}
// 修改数据 if (bisRight) {
// 组合SQL语句 strSQL = "update course set courseName =" + "'"
+ course.getCourseName() + "'" + ",period ="
+ String.valueOf(course.getPreiod()) + ",creditHour="
+ String.valueOf(course.getCreditHour())
+ "where courseid =" + String.valueOf(course.getCouId());
// 更新数据库 try {
updateLine = jdbcConnection.updateData(strSQL);
} catch (SQLException e) {
e.printStackTrace();
}
// 更新成功 if (updateLine > 0) {
JOptionPane.showMessageDialog(this, "修改成功!");
// 更新用户界面数据 this.CourseQueryTableModel.setValueAt(course.getCourseName(),
nrow, 1);
this.CourseQueryTableModel.setValueAt(course.getPreiod(), nrow,
2);
this.CourseQueryTableModel.setValueAt(course.getCreditHour(),
nrow, 3);
} else {
JOptionPane.showMessageDialog(this, "修改失败!请重新修改!");
}
}
}
/**
* 获得用户输入的数据 *
* @return Course对象 */
public Course getUserInput(Course course) {
// 定义变量 学科名,学时,学分 String strcourseID = "";
int ncourseID = 0;
String strcourseName = "";
String strpreiod = "";
int npreiod = 0;
String strcreditHour = "";
float fcreditHour = 0.0f;
// 定义boolean型变量,用户校验是否成功,初始化为False
boolean bisRight = false;
// 获取用户输入信息 strcourseID = this.CourseIdTextField.getText();
strcourseName = this.CourseNameTextField.getText();
strpreiod = this.PeriodTextField1.getText();
strcreditHour = this.CreditHourTextField1.getText();
// 用户信息校验 if (strpreiod.matches(REG_DIGPREIOD)) {
bisRight = true;
} else {
bisRight = false;
JOptionPane.showMessageDialog(this,
"请不要在学时中输入字母,学时在(0,200)内,请重新输入学时!");
}
if (strcreditHour.matches(REG_FLOATHOUR)) {
bisRight = true;
} else {
bisRight = false;
JOptionPane.showMessageDialog(this,
"请不要在学分中输入字母,学分在(0,5)内,请重新输入学分!");
}
if (bisRight) {
// 类型转换 try {
ncourseID = Integer.parseInt(strcourseID);
npreiod = Integer.parseInt(strpreiod);
fcreditHour = Float.parseFloat(strcreditHour);
} catch (NumberFormatException e) {
e.printStackTrace();
}
if (npreiod == 0 || npreiod > 200 || fcreditHour == 0
|| fcreditHour > 5) {
bisRight = false;
JOptionPane.showMessageDialog(this,
"您输入的学时、学分超出范围。\n学时在(0,200)内,学分在(0,5)内,请重新输入!");
} else {
bisRight = true;
}
}
// 为course对象赋值 course.setValue(ncourseID, strcourseName, npreiod, fcreditHour);
return course;
}
/**
* 删除课程信息 * @param evt
*/
private void DeleteButton1ActionPerformed(ActionEvent evt) {
// 删除 // 建立一个JDBC对象 JdbcConnct jdbcConnection = new JdbcConnct();
// 定义鼠标点击行 int nrow = 0;
// 定义boolean型变量,用户是否点击行,初始化为False
boolean bisRight = false;
// 定义boolean型变量,当前记录是否与考试表关联,初始化为False
boolean bisReferenceExam = false;
// 定义boolean型变量,当前记录是否与专业课程分配表关联,初始化为False
boolean bisReferenceCouMajrMatch = false;
// 定义SQL语句 String strSQL = "";
// 更新行数 int型 updateLine
int updateLine = 0;
// 定义用户选择 nuserSelect
int nuserSelect = 0;
// 定义变量学科号 String strcourseId = "";
// 定义Vector变量,存储从数据库查询来的信息 Vector vecData = new Vector();
// 获取鼠标点击行 nrow = this.CourseQueryTable.getSelectedRow();
// 判断用户是否选择 if (nrow < 0) {
bisRight = false;
JOptionPane.showMessageDialog(this, "您没有选择数据!在删除前请选择一行数据!");
} else {
bisRight = true;
}
// 获取行内数据 strcourseId = String.valueOf(this.CourseQueryTable.getValueAt(nrow, 0));
// 当前记录是否与其它表关联 // 组合SQL语句,查询exam表 strSQL = "select * from exam t where courseid = " + strcourseId;
try {
vecData = jdbcConnection.getData(strSQL);
} catch (SQLException e) {
e.printStackTrace();
}
if(vecData.size() > 0){
bisReferenceExam = true;
JOptionPane.showMessageDialog(this, "该课程与考试表课程关联,无法删除,请重新选择.");
}else{
bisReferenceExam = false;
}
// 组合SQL语句,查询coumajrmatch专业课程分配表 strSQL = "select * from coumajrmatch t where courseId = " + strcourseId;
try {
vecData = jdbcConnection.getData(strSQL);
} catch (SQLException e) {
e.printStackTrace();
}
if(vecData.size() > 0){
bisReferenceCouMajrMatch = true;
JOptionPane.showMessageDialog(this, "该课程与专业课程分配表关联,无法删除,请重新选择.");
}else{
bisReferenceCouMajrMatch = false;
}
//如果用户操作正确,且该课程与其它表无关联 if (bisRight
&&!bisReferenceExam
&&!bisReferenceCouMajrMatch) {
// 删除用户选择的行数据 nuserSelect = JOptionPane.showConfirmDialog(this, "是否确认删除?",
"用户确认", JOptionPane.YES_NO_OPTION);
// System.out.println("\t" + nuserSelect+"\t"+ strcourseId);
//如果用户确认删除 if (nuserSelect == 0) {
strSQL = "delete from course where courseId = "
+ strcourseId;
try {
updateLine = jdbcConnection.updateData(strSQL);
} catch (SQLException e) {
e.printStackTrace();
}
}
// 判断是否删除成功 if (updateLine > 0) {
// 从用户界面上删除数据 this.CourseQueryTableModel.removeRow(nrow);
JOptionPane.showMessageDialog(this, "成功删除!");
} else {
JOptionPane.showMessageDialog(this, "删除失败!请重新操作.");
}
}
}
private void CourseQueryComboBox1ActionPerformed(ActionEvent evt) {
// 根据用户输入字段更改匹配符 // 定义String型变量, strTemp,存储用户选择的字段 String strUserSelect = "";
// 获取用户选择字段 strUserSelect = this.CourseQueryComboBox1.getSelectedItem().toString();
if (strUserSelect.equals("科目名")) {
this.CompareComboBox.removeAllItems();
this.CompareComboBox.addItem("匹配");
this.CompareComboBox.addItem("=");
}
if (strUserSelect.equals("科目号") || strUserSelect.equals("学时")
|| strUserSelect.equals("学分")) {
this.CompareComboBox.removeAllItems();
this.CompareComboBox.addItem(">");
this.CompareComboBox.addItem("<");
this.CompareComboBox.addItem("=");
}
}
private void ClearButtonActionPerformed(ActionEvent evt) {
// TODO add your code for ClearButton.actionPerformed
// 用户查询条件数量置为1
ncount = 1;
// 清空Vector变量vecSQL vecListView清空List控件中按钮 this.vecSQL.clear();
this.vecListView.clear();
this.SQLComjList.setListData(vecListView);
// 清除按钮不可用 this.ClearButton.setEnabled(false);
}
private void SelectRageCheckBoxActionPerformed(ActionEvent evt) {
System.out.println("SelectRageCheckBox.actionPerformed, event=" + evt);
// 是否选择范围 if (SelectRageCheckBox.isSelected()) {
// 查询字段文本框不可用,清空,选择范围文本框可用 this.QueryTextField.setEnabled(false);
this.FirstTextField.setEnabled(true);
this.EndTextField1.setEnabled(true);
this.QueryTextField.setText("");
} else {
// 选择范围文本框不可用,清空,查询字段文本框可用 this.QueryTextField.setEnabled(true);
this.FirstTextField.setEnabled(false);
this.EndTextField1.setEnabled(false);
this.FirstTextField.setText("");
this.EndTextField1.setText("");
}
}
private void AddButtonActionPerformed(ActionEvent evt) {
System.out.println("AddButton.actionPerformed, event=" + evt);
// TODO add your code for AddButton.actionPerformed
// 定义SQL语句 String strSQL = "";
// 定义boolean型变量 isRight 用户操作是否正确,初始化false
boolean bisRight = false;
// 第一次必须选择”无“条件选项 if (!this.PrecisionRadioButton.isSelected() && ncount == 1) {
bisRight = false;
JOptionPane.showMessageDialog(this, "选择多条件查询时’无’选项须在第一次操作时选择,\n第一次"
+ "操作后请选择’或者’、‘并且’选项!");
} else if (this.PrecisionRadioButton.isSelected() && ncount > 1) {
// 第一次以后不能选择”无“条件选项 bisRight = false;
JOptionPane.showMessageDialog(this, "’无’选项须在第一次操作时选择,\n"
+ "第一次操作后请选择’或者’、‘并且’选项!");
} else {
bisRight = true;
// 成功则用户查询的条件数量加一 ncount++;
}
if (bisRight) {
// 获得用户选择并转化为SQL语句 strSQL = this.getUserSelect();
System.out.println("\t" + strSQL);
// 向Vector变量vecSQL中添加变量 this.vecSQL.add(strSQL);
// 清空按钮可用,范围为本框不可用,查询字段文本框置为空 this.ClearButton.setEnabled(true);
this.FirstTextField.setEnabled(false);
this.EndTextField1.setEnabled(false);
this.QueryTextField.setText("");
}
}
/**
* 将用户选择转化为SQL语句 *
* @return SQL 语句 */
private String getUserSelect() {
// 添加组合查询信息 // 定义boolean型变量 isSelectRage用户是否选择按范围查询,初始化false
boolean isSelectRage = false;
// 定义boolean型变量 isRight 用户输入是否正确,初始化false
boolean bisRight = false;
// 定义查询字段,比较符,用户输入 String strQueryField = "";
String strLogicalSymbol = "";
String strUserInput = "";
int nUserInput = 0;
double dUserInput = 0.0;
// 定义用户选择范围,Min最小,Max最大 String strMin = "";
String strMax = "";
double dMin = 0.0;
double dMax = 0.0;
// 用户在组合框中选择的Item索引 int nUserSelect;
// 定义SQL语句 String strSQL = "";
// 定义SQL显示语句 String strSQLView = "";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -