📄 studentadddialog.java
字号:
/**
* 通过专业名获取专业号
* @param majorName
* @return
*/
public int getmajorId(String majorName) {
try {
// Class.forName("oracle.jdbc.driver.OracleDriver");
// String url = "jdbc:oracle:thin:@192.168.111.79:1521:orcl";
// String user = "zlb";
// String pwd = "hb2080";
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
int majorid = 0;
conn = conn = JdbcConnct.fetchConnection();
String sql = "select majorid from major where majorname = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, majorName);
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;
}
}
/**
* 通过班级名获取班级的ID
* @param className
* @return
*/
public int getClassId(String className) {
try {
// Class.forName("oracle.jdbc.driver.OracleDriver");
// String url = "jdbc:oracle:thin:@192.168.111.79:1521:orcl";
// String user = "zlb";
// String pwd = "hb2080";
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;
}
}
/**
* 获取联系方式并约束输入
* @return
*/
public String getContactWay() {
String contactWay = this.contactWayTextField.getText().trim();
String regex = "\\d{11}";
if (contactWay.matches(regex))
return contactWay;
else
return "";
}
/**
* 判断姓名的输入
*/
public boolean stuNamecontrol() {
// 姓名的控制 范围在(0,100]
boolean stuNamecontrol = false;
if (this.stuNameTextField.getText().length() > 10) {
JOptionPane.showMessageDialog(this, "姓名输入超过100,请重新输入");
stuNamecontrol = false;
} else
stuNamecontrol = true;
return stuNamecontrol;
}
/**
* 判断学号输入
*/
public boolean stuIdcontrol() {
boolean stuIdcontrol = false;
String studentId = this.studentIdTextField.getText();
if (studentId.matches(REG_DIGTEAID)) {
if(isStuId(studentId)==true)
{
JOptionPane.showMessageDialog(this, "学生学号已经存在!");
stuIdcontrol = false;
}
stuIdcontrol = true;
} else {
JOptionPane.showMessageDialog(this, "学号输入错误,请重新输入");
stuIdcontrol = false;
}
return stuIdcontrol;
}
/**
* 判断学号是否已经存在
* @param stuId
* @return
*/
public boolean isStuId(String stuId)
{
boolean isStuId = false;
try
{
Connection conn = jdbc.fetchConnection();
Statement stmt = conn.createStatement();
String sql = "select * from student where "+ stuId+ " in (select studentid from student)";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next())
{
return true;
}
return isStuId;
}catch(Exception e)
{
return false;
}
}
/*
* 年龄的正则表达式控制输入
*/
public boolean agecontrol() {
//定义年龄判断值
boolean agecontrol = false;
//如果匹配
if (this.ageTextField.getText().matches(REG_DIGAGE))
agecontrol = true; //年龄判断值为真
else //如果不匹配。提示出错
{
JOptionPane.showMessageDialog(this, "年龄输入错误,请重新输入,范围在1到99之间");
agecontrol = false; //年龄判断值为真
}
return agecontrol;//返回年龄判断值
}
/**
* 添加功能
*
* @param evt
* @throws SQLException
*/
public void addButtonActionPerformed(ActionEvent evt) throws SQLException {
// 定义Vector变量,存储学生信息
Vector vecData = new Vector();
// 定义学号
int stuNum = 0;
// 定义性别
String gender1 = "";
// 定义政治面貌
int PoliStauts;
// 定义出生日期
String birthDay = "";
// 定义学生状态
int status = 0;
int majorid;
// String gender = "";
String age = "";
String classname = "";
int classid;
String path = this.pathTextField.getText();
stuNamecontrol = stuNamecontrol();
stuIdcontrol = stuIdcontrol();
agecontrol = agecontrol();
// String contactWay = get
// majorid = getmajorId();
//判断输入是否合法
if (stuNamecontrol == true && stuIdcontrol == true
&& agecontrol == true) {
//如果为空,提示出错
if (this.stuNameTextField.getText().equals("")
|| this.studentIdTextField.getText().equals(""))
JOptionPane.showMessageDialog(this, "姓名和学号为必填项,请完整输入!");
else {
String stuid = this.studentIdTextField.getText().trim();
try {
stuNum = Integer.parseInt(stuid);
} catch (Exception e) {
e.printStackTrace();
}
//获得学生姓名
String stuName = this.stuNameTextField.getText().trim();
//获取性别
if (this.manRadioButton.isSelected())
gender1 = "M";
else
gender1 = "W";
// this.manRadioButton.getText();
// String gender2=this.famelRadioButton.getSelectedObjects();
age = this.ageTextField.getText().trim();//获得年龄
//获得政治面貌
if (this.poliStatusComboBox.getSelectedItem().equals("共青团员")) {
PoliStauts = 1;
} else if (this.poliStatusComboBox.getSelectedItem().equals(
"党员")) {
PoliStauts = 2;
} else if (this.poliStatusComboBox.getSelectedItem().equals(
"群众")) {
PoliStauts = 3;
} else {
PoliStauts = 4;
}
birthDay = this.birthDayTextField.getText();
String contactWay = this.contactWayTextField.getText();
String nat = this.nativeTextField.getText();
String info = this.infoTextArea.getText();
String className = this.classIdTextField.getText();
classid = getClassId(className);
// String majorname = (String) this.majornameComboBox
// .getSelectedItem();
String entertime = this.enterTimeTextField.getText();
if (this.statusComboBox.getSelectedItem().equals("在读")) {
status = 1;
} else if (this.statusComboBox.getSelectedItem().equals("休学")) {
status = 2;
} else if (this.statusComboBox.getSelectedItem().equals("退学")) {
status = 3;
} else if (this.statusComboBox.getSelectedItem().equals("毕业")) {
status = 4;
} else {
status = 5;
}
// majorid = getmajorId(majorname);
// System.out.println(majorid);
String classId = null;
if (classid == 0)
classId = "";
else
classId = Integer.toString(classid);
String remark = this.remarkTextArea.getText();
// JDBC对象
JdbcConnct jdbcConn = new JdbcConnct();
ResultSet rs = null;
Statement stmt = null;
Connection conn = null;
Vector vec = new Vector();
try {
conn = jdbcConn.fetchConnection();
System.out.println(conn);
// String strpolistatus;
String sql = "insert into student(studentid,stuname,"
+ "gender,age,polistatus,birthday,classid,photo,contactway,native,status,entertime,info,remark)"
+ " values(" + stuNum + "," + "'" + stuName + "','"
+ gender1 + "'," + age + "," + status
+ ",to_date('" + birthDay + "'"
+ ",'yyyy-mm-dd'),'" + classId + "','" + path
+ "','" + contactWay + "','" + nat + "'," + status
+ "," + "to_date('" + entertime + "'"
+ ",'yyyy-mm-dd'),'" + info + "','" + remark + "')";
System.out.println(sql);
stmt = conn.createStatement();
int updateLine = stmt.executeUpdate(sql);
vec.addElement(stuid);
vec.addElement(stuName);
vec.addElement(gender1);
vec.addElement(age);
vec.addElement(this.poliStatusComboBox.getSelectedItem());
vec.addElement(birthDay);
vec.addElement(contactWay);
vec.addElement(nat);
vec.addElement(entertime);
vec.addElement(this.statusComboBox.getSelectedItem());
vec.addElement(classname);
JOptionPane.showMessageDialog(this, "添加成功!");
// jTable1Model = new DefaultTableModel(row, coloumn);
// this.stuTable.setModel(jTable1Model);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// rs.close();
stmt.close();
conn.close();
}
}
} else {
}
}
/**
* 退出功能
*
* @param evt
*/
public void exsitButtonActionPerformed(ActionEvent evt) {
// System.out.println("exsitButton.actionPerformed, event=" + evt);
// TODO add your code for exsitButton.actionPerformed
this.dispose();
}
/**
* 获取照片
*
* @param evt
*/
public void querypicButtonActionPerformed(ActionEvent evt) {
// System.out.println("querypicButton.actionPerformed, event=" + evt);
// TODO add your code for querypicButton.actionPerformed
PicChooser picChooser = new PicChooser();
String path = picChooser.getString();
this.pathTextField.setText(path);
ImageIcon icon = new ImageIcon(path);
this.photoLabel.setIcon(icon);
}
/**
* 取得入学时间
*
* @param evt
*/
private void enterTimeTextFieldMouseClicked(MouseEvent evt) {
// System.out.println("enterTimeTextField.mouseClicked, event=" + evt);
// TODO add your code for enterTimeTextField.mouseClicked
CalendarJDialog calenderDialog = new CalendarJDialog(null);
calenderDialog.setModal(true);
calenderDialog.setVisible(true);
this.enterTimeTextField.setText(calenderDialog.toString());
}
/**
* 取得出生日期
*
* @param evt
*/
private void birthDayTextFieldMouseClicked(MouseEvent evt) {
// System.out.println("birthDayTextField.mouseClicked, event=" + evt);
// TODO add your code for birthDayTextField.mouseClicked
CalendarJDialog calenderDialog = new CalendarJDialog(null);
calenderDialog.setModal(true);
calenderDialog.setVisible(true);
this.birthDayTextField.setText(calenderDialog.toString());
}
/**
* 添加功能
*
* @param evt
*/
private void submitButtonActionPerformed(ActionEvent evt) {
// System.out.println("submitButton.componentHidden, event=" + evt);
// TODO add your code for submitButton.componentHidden
Vector vecData = new Vector();
// 定义学号
int stuNum = 0;
// 定义性别
String gender1 = "";
// 定义政治面貌
int PoliStauts;
// 定义出生日期
String birthDay = "";
// 定义学生状态
int status;
int majorid;
// String gender = "";
String age = "";
String classname = "";
int classid;
String path = this.pathTextField.getText();
// majorid = getmajorId();
if (this.stuNameTextField.getText().equals("")
|| this.studentIdTextField.getText().equals(""))
JOptionPane.showMessageDialog(this, "姓名和学号为必填项,请完整输入!");
else {
String stuid = this.studentIdTextField.getText().trim();
try {
stuNum = Integer.parseInt(stuid);
} catch (Exception e) {
e.printStackTrace();
}
String stuName = this.stuNameTextField.getText().trim();
if (this.manRadioButton.isSelected())
gender1 = "M";
else
gender1 = "W";
// this.manRadioButton.getText();
// String gender2=this.famelRadioButton.getSelectedObjects();
age = this.ageTextField.getText().trim();
if (this.poliStatusComboBox.getSelectedItem().equals("共青团员")) {
PoliStauts = 1;
} else if (this.poliStatusComboBox.getSelectedItem().equals("党员")) {
PoliStauts = 2;
} else if (this.poliStatusComboBox.getSelectedItem().equals("群众")) {
PoliStauts = 3;
} else {
PoliStauts = 4;
}
birthDay = this.birthDayTextField.getText();
String contactWay = this.contactWayTextField.getText();
String nat = this.nativeTextField.getText();
String info = this.infoTextArea.getText();
String className = this.classIdTextField.getText();
classid = getClassId(className);
// String majorname = (String) this.majornameComboBox
// .getSelectedItem();
String entertime = this.enterTimeTextField.getText();
if (this.statusComboBox.getSelectedItem().equals("在读")) {
status = 1;
} else if (this.statusComboBox.getSelectedItem().equals("休学")) {
status = 2;
} else if (this.statusComboBox.getSelectedItem().equals("退学")) {
status = 3;
} else if (this.statusComboBox.getSelectedItem().equals("毕业")) {
status = 4;
} else {
status = 5;
}
// majorid = getmajorId(majorname);
// System.out.println(majorid);
String classId = null;
if (classid == 0)
classId = "";
else
classId = Integer.toString(classid);
String remark = this.remarkTextArea.getText();
// JDBC对象
JdbcConnct jdbcConn = new JdbcConnct();
Vector vec = new Vector();
String sql = "update student set stuname = " + "'" + stuName
+ "'," + "gender = '" + gender1 + "',age = " + age
+ ",polistatus = " + status + ",birthday = " + "to_date('"
+ birthDay + "'" + ",'yyyy-mm-dd')" + ",classid = '"
+ classId + "',photo = '" + path + "' " + ",contactway = '"
+ contactWay + "',native='" + nat + "',status=" + status
+ ",entertime = " + "to_date('" + entertime + "'"
+ ",'yyyy-mm-dd')" + ",info = '" + info + "',remark ='"
+ remark + "'" + " where studentid = " + stuNum;
System.out.println(sql);
try {
int updateLine = jdbcConn.updateData(sql);
if (updateLine > 0) {
JOptionPane.showMessageDialog(this, "添加成功!");
} else {
JOptionPane.showMessageDialog(this, "添加出错!");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -