Java程序设计——学生基本信息管理系统

1.团队课程设计博客链接

http://www.cnblogs.com/handsome321/p/7067121.html

2.个人负责模块说明

查询和修改功能

3.个人代码提交记录

4.自己负责模块或任务详细说明

代码结构为:
,DB包为数据库操作,sims包为操作界面以及各个功能。
本人负责的主要功能展示:
一.查询功能
查询界面:

按姓名查询:

运行结果:

按学号查询:
运行结果与按姓名查询一样。
当数据库中没有所要查询人的信息的时候:

主要代码:
查询:
private void SearchButActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LoginActionPerformed
String[][] strs = Search.serach(Searchname.getText(),Searchstuno.getText());
if(strs[0][0]==null){
JOptionPane.showMessageDialog(null, "查无此人");
Searchname.setText("");
Searchstuno.setText("");
}
else{
new SInfor(strs).setVisible(true);
this.setVisible(false);
}

用二维数组显示数据库信息:
public SInfor(String[][] strs) {
this.strs = strs;
initComponents();

	Vector<String> columnNames = new Vector<>();

	columnNames.add("学号");
	columnNames.add("姓名");
	columnNames.add("性别");
	columnNames.add("生日");
	columnNames.add("政治面貌");
	columnNames.add("地址");
	columnNames.add("电话");
	columnNames.add("宿舍号");

	Vector<Vector<String>> rowData = new Vector<Vector<String>>();

	for (int i = 0; i < 100; i++) {
		Vector<String> rowStrings = new Vector<>();

		for (int j = 0; j < 8; j++) {
			rowStrings.add(strs[i][j]);
		}
		rowData.add(rowStrings);
	}

	jTable2 = new JTable(rowData, columnNames);

	jScrollPane2.setViewportView(jTable2);//滚动面板

}

查询语句:
public static String[][] serach(String name,String stuno){
if(name.length()!=0){

	String str = "select * from sims where name= '"+name+"';";

	String[][] strs = new String[100][8];
	
	try{
		ResultSet resultSet = DBUtil.query(str);//str->数据库
		int cnt = 0;
		while (resultSet.next()) {
			for (int i = 0; i < 8; i++) {
				strs[cnt][i] = resultSet.getString(i + 1);
			}
			cnt++;
		}
	}catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return strs;
	
	}

操作数据库,返回结果集:
public static ResultSet query(String sql) throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
// 1.获得数据库的连接
Connection conn = DriverManager.getConnection(URL, NAME, PASSWORD);
// 2.通过数据库的连接操作数据库,实现增删改查
Statement stmt = conn.createStatement();

	try {
		return stmt.executeQuery(sql);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;

}

}
二.修改功能
修改界面:

性别输入错误时:

生日输入错误时:

确认修改:
修改成功:

修改后:
主要代码:
查询:
private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
String str = "select * from sims where stuno = '"+getstuno.getText().trim()+"';";
String[][] strs = Strsselect.select(str);
if(strs[0][0]!=null){
AModifystu adm = new AModifystu(strs);
adm.setVisible(true);
this.setVisible(false);
}else{
JOptionPane.showMessageDialog(null, "查无此人!");
getstuno.setText("");
}

数据库修改:
public class Modify {

public static void modify(String stuno, String name, String gender, String birthdate, String poutlook,
		String address, String phone, String dormitory){
String str = "update sims set stuno = '" + stuno + "',name = '"+name+"',gender='" + gender + "'," + "birthdate='" + birthdate
		+ "'," + "poutlook='" + poutlook + "'," + "address='" + address + "'," + "phone='" + phone
		+ "'," + "dormitory='" + dormitory + "' " + "where name=" + "'" + name + "';";
try {
	DBUtil.exec(str);
	JOptionPane.showMessageDialog(null, "修改成功!");
} catch (ClassNotFoundException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (SQLException e) {
	// TODO Auto-generated catch block

// e.printStackTrace();
JOptionPane.showMessageDialog(null, "修改成功!");
}
}
}

判断修改输入格式:
private void modifyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifyActionPerformed

	String[] str = bir.getText().split("-");
	String birthday = "" ;
	for (int i = 0; i < str.length; i++) {
		birthday += str[i];
	}
	
	if(stuno.getText().length()==0)
    	JOptionPane.showMessageDialog(null, "学号不能为空!");
    else if(Search.isNumer(stuno.getText())==false)
    	JOptionPane.showMessageDialog(null, "学号格式有误,请重新输入");
    else if(name.getText().length()==0)
    	JOptionPane.showMessageDialog(null, "姓名不能为空!");
    else if(gender.getText().length()==0)
    	JOptionPane.showMessageDialog(null, "性别不能为空!");
    else if(gender.getText().length()>1){
    	JOptionPane.showMessageDialog(null, "性别格式有误,请输入m/f");
    	gender.setText("");
    }
    else if (bir.getText().length()==0){
    	JOptionPane.showMessageDialog(null, "生日不能为空!");
    }
    else if((bir.getText().length()!=0&&Search.isNumer(birthday)==false)||bir.getText().length()!=10){
    	JOptionPane.showMessageDialog(null, "生日格式有误,请输xxxx-xx-xx格式");
    	bir.setText("");
    }else if(Integer.parseInt(str[1])>12){
    	JOptionPane.showMessageDialog(null, "月份输入有误");
    	bir.setText("");
    }
    else if(Integer.parseInt(str[2])>31){
    	JOptionPane.showMessageDialog(null, "日期输入有误");
    	bir.setText("");
    }

    else if(Search.isNumer(phone.getText())==false){
    	JOptionPane.showMessageDialog(null, "电话号码格式有误!");
    	phone.setText("");

    } else{
    	int yes =JOptionPane.showConfirmDialog(this, "确认修改?", "提示",JOptionPane.YES_NO_OPTION); 
    	if(yes==JOptionPane.YES_OPTION){
    		
    		Modify.modify(stuno.getText(), name.getText(), gender.getText(),birthday,
            		poutlook.getText(), address.getText(), phone.getText(), dormitory.getText());
    		
    	}
    	else{
    	}
	
	}

5.课程设计感想

这次课设让我知道了自己在学习上的很多不足,特别是在敲代码这块,很多代码不会敲,才导致我们课设延迟到周一才得以提交,以后应加强练习,实践才是检验真理的唯一标准。

posted @ 2017-06-22 20:24  我吃猫饼干  阅读(2036)  评论(0编辑  收藏  举报