android:id="@+id/home_xinxi"
android:layout_marginTop="40dp"
android:text="学生成绩管理系统"
android:textAppearance="android:attr/textAppearanceLarge"/>
android:id="@+id/cs"
android:layout_alignLeft="@+id/courses"
android:layout_below="@+id/courses"
android:layout_marginTop="36dp"
android:text="成绩信息"/>
android:id="@+id/courses"
android:layout_alignLeft="@+id/students"
android:layout_below="@+id/students"
android:layout_marginTop="30dp"
android:text="课程信息"/>
protectedvoidonCreate(BundlesavedInstanceState){
//TODOAuto-generatedmethodstub
super.onCreate(savedInstanceState);
setContentView(R.layout.student_layout);
setTitle("学生信息管理");
search=(Button)this.findViewById(R.id.search_student_button);
search_text=(EditText)this.findViewById(R.id.to_search_student);
listView=(ListView)findViewById(R.id.student_listview);
add_student=(Button)this.findViewById(R.id.add_student);
delete_student=(Button)this.findViewById(R.id.delete_student);
db=newMyDB(StudentsActivity.this);
updatelistview();
listView.setOnItemClickListener(newOnItemClickListener(){
publicvoidonItemClick(AdapterView<>parent,Viewview,
intposition,longid){
ListViewmyListView=(ListView)parent;
HashMapitem=(HashMap)myListView.getItemAtPosition(position);
popview(item,"modify");//添加弹出窗口
}
});
search.setOnClickListener(newOnClickListener(){
publicvoidonClick(Viewv){
3.2学生信息界面
学生信息界面主要显示学生的信息,可以在此界面对学生的信息进行查询、添加、删除和修改。
添加时直接点击添加,会弹出添加界面,进行添加即可
查询时搜索学号即可查询信息
删除时输入学号进行删除
修改时点击信息会弹出界面,直接进行修改即可
android:id="@+id/nono"
android:orientation="horizontal">
android:id="@+id/lll"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="搜索学号:"/>
android:id="@+id/to_search_student"
android:layout_width="160dp"
android:ems="10"
android:inputType="number"/>
android:id="@+id/search_student_button"
android:text="查询"/>
android:id="@+id/delete_student"
android:layout_weight="0.79"
android:text="删除"/>
android:id="@+id/add_student"
android:text="添加"/>
android:id="@+id/stu_head"
android:id="@+id/student_view_snum"
android:layout_width="80dp"
android:layout_height="30dp"
android:text="学号"/>
android:id="@+id/student_view_sname"
android:layout_width="50dp"
android:text="姓名"/>
android:id="@+id/student_view_ssex"
android:layout_width="30dp"
android:text="性别"/>
android:id="@+id/student_view_sage"
android:text="年龄"/>
android:id="@+id/student_view_sphone"
android:text="电话"/>
publicvoidAddStudent(Studentstudent){
Stringsql="insertintoStudents(Snum,Sname,Ssex,Sage,Sphone)values(,,,,)";
Object[]bindArgs={student.getSnum(),student.getSname(),student.getSsex(),student.getSage(),student.getSphone()};
mydb=this.dBhelper.getWritableDatabase();
mydb.execSQL(sql,bindArgs);
publicvoiddelStudents(Stringsnumber){
SQLiteDatabasedb=this.dBhelper.getWritableDatabase();
Stringsql="deletefromStudentswhereSnum=";//删students表中的某个学生
Object[]bindArgs={snumber};
db.execSQL(sql,bindArgs);
try{
sql="deletefromCSwhereSnum=";//删CS成绩表中该学生的全部成绩
}catch(Exceptione){
//TODO:handleexception
publicvoiddelALLStudents(){
Stringsql="deletefromStudents";//删students表中的某个学生
Object[]bindArgs={};
sql="deletefromCS";//删CS成绩表中该学生的全部成绩
publicvoidupdateStudent(Studentstudent){
Stringsql="updateStudentssetSname=,Ssex=,Sage=,Sphone=whereSnum=";
Object[]bindArgs={student.getSname(),
student.getSsex(),student.getSage(),student.getSphone(),student.getSnum()};
publicListfindALLStudent(){
Stringsql="select*fromStudentsorderbySnumasc";
String[]selectionArgs={};
mydb=this.dBhelper.getReadableDatabase();
Cursorcursor=mydb.rawQuery(sql,selectionArgs);
Liststudents=newArrayList();
while(cursor.moveToNext()){
Studentstudent=newStudent(cursor.getString(0),cursor.getString(1),
cursor.getString(2),cursor.getInt(3),cursor.getString(4));
students.add(student);
returnstudents;
publicStudentfindStudent(Stringsnumber){
Stringsql="select*fromStudentswhereSnum=";
String[]selectionArgs={snumber};
if(cursor.moveToFirst()){
returnnewStudent(cursor.getString(0),cursor.getString(1),
returnnull;
3.3课程信息界面
课程信息界面可以查看课程信息,进行课程编码、课程名称、课程学分的增删改查。
添加课程信息:过程和学生信息的一样
删除课程信息
修改课程信息
android:id="@+id/ccc"
android:text="搜索课程编码:"/>
android:id="@+id/to_search_Course"
android:ems="10"/>
android:id="@+id/search_course_button"
android:id="@+id/delete_course"
android:id="@+id/add_course"
android:id="@+id/course_view_cnum"
android:layout_width="108dp"
android:text="课程编码"/>
android:id="@+id/course_view_cname"
android:layout_width="112dp"
android:text="课程名称"/>
android:id="@+id/course_view_credit"
android:layout_width="56dp"
android:text="学分"/>
publicvoidAddCourse(Coursecourse){
Stringsql="insertintoCourses(Cnum,Cname,Ccredit)values(,,)";
Object[]bindArgs={course.getCnum(),course.getCname(),course.getCcredit()};
publicvoiddelCourse(Stringcnumber){
Stringsql="deletefromCourseswhereCnum=";
Object[]bindArgs={cnumber};
sql="deletefromCSwhereCnum=";
publicvoiddelALLCourse(){
Stringsql="deletefromCourses";
sql="deletefromCS";
publicvoidupdateCourse(Coursecourse){
Stringsql="updateCoursessetCname=,Ccredit=whereCnum=";
Object[]bindArgs={course.getCname(),course.getCcredit(),course.getCnum()};
3.4成绩信息界面
成绩界面可以查看学生的成绩,学生的成绩以学号+课程编码+分数的形式显示。同样的,在这个界面同样可以对学生的成绩信息进行增删改查。
增加学生成绩信息;
删除学生成绩信息;
修改学生成绩信息;
android:id="@+id/llllbn"
android:id="@+id/bfgbfd"
android:text="学号+课程编码:"/>
android:id="@+id/to_search_CS_S"
android:ems="5"
android:id="@+id/to_search_CS_C"
android:ems="5"/>
android:id="@+id/search_cs_button"
android:id="@+id/delete_cs"
android:id="@+id/add_cs"
android:id="@+id/cs_head"
android:id="@+id/cs_view_snum"
android:id="@+id/cs_view_cnum"
android:id="@+id/cs_view_score"
android:text="分数"/>
publicvoidAddCS(CScs)throwsException{
if(findStudent(cs.getSnum())!=null&&findCourse(cs.getCnum())!=null){
Stringsql="insertintoCS(Snum,Cnum,Score)values(,,)";
Object[]bindArgs={cs.getSnum(),cs.getCnum(),cs.getScore()};
else{
thrownewException("can'taddthisscore");
publicvoiddelCS(Stringsnumber,Stringcnumber){
Stringsql="deletefromCSwhereSnum=andCnum=";
Object[]bindArgs={snumber,cnumber};
publicvoiddelALLCS(){
Stringsql="deletefromCS";
publicvoidupdateCS(CScs){
Stringsql="updateCSsetScore=whereSnum=andCnum=";
Object[]bindArgs={cs.getScore(),cs.getSnum(),cs.getCnum()};
四、系统测试
4.1测试
1.测试系统能否正常运行
2.测试数据库能否顺利连接
3.测试系统各个页面能否正常跳转
4.测试增删改查功能能否正常使用
5.测试学生信息是否正常显示
4.2测试结果
系统能够正常运行;数据库顺利连接;页面跳转正常;增删改查能够正常使用;学生信息显示正常。
五、体验心得
通过这次的程序设计,我学到了很多,首先就是能够更加熟练的使用和深入的了解Java语言这门通用的计算机语言,还有就是可以自己编写程序了,还是很有成就感的。还有就是光靠书本的知识的确不行,还是要理论联系实践才行。因此不断的练习是必要的,上机实践更重要。通过这次的程序编写让我对编程产生了更大的兴趣,我相信以后我们都会越来越好的。
我从一开始的懵懂到现在的有点理解可以说和老师的教导分不开的。虽然俗语说师傅领进门,修行靠个人,所以师傅的作用还是不可取代的,以前上课时老师教的代码给了我很大的帮助,非常感谢老师!
附件Java主要源代码
StudentActivity.java(对学生信息界面的操作):
publicclassStudentsActivityextendsActivity{
privateButtonsearch=null;
privateEditTextsearch_text=null;
privateListViewlistView=null;
privateMyDBdb=null;
privateButtonadd_student=null;
privateButtondelete_student=null;
@Override
add_student.setOnClickListener(newOnClickListener(){
HashMapitem=null;
popview(item,"add");
delete_student.setOnClickListener(newOnClickListener(){
if(search_text.getText().toString().equals("0000")){
db.delALLStudents();
db.delStudents(search_text.getText().toString());
search_text.setText("");
Toast.makeText(StudentsActivity.this,"deletestudenterror!",Toast.LENGTH_SHORT).show();
privatevoidpopview(HashMapitem,finalStringmode){
//取得自定义View
LayoutInflaterlayoutInflater=LayoutInflater.from(StudentsActivity.this);
ViewmyLoginView=layoutInflater.inflate(R.layout.student_edit,null);
finalEditTextmodify_snum=(EditText)myLoginView.findViewById(R.id.modify_snum_txt);
finalEditTextmodify_sname=(EditText)myLoginView.findViewById(R.id.modify_sname_txt);
finalEditTextmodify_sage=(EditText)myLoginView.findViewById(R.id.modify_sage_txt);
finalEditTextmodify_sphone=(EditText)myLoginView.findViewById(R.id.modify_sphone_txt);
finalRadioButtonmaleButton=(RadioButton)myLoginView.findViewById(R.id.radio_button_male);
finalRadioButtonfemaleButton=(RadioButton)myLoginView.findViewById(R.id.radio_button_female);
modify_snum.setText("");
modify_sname.setText("");
modify_sage.setText("0");
modify_sphone.setText("");
modify_snum.setEnabled(true);
if(mode.equals("modify")){
modify_snum.setText(item.get("Snum").toString());
modify_snum.setEnabled(false);
modify_sname.setText(item.get("Sname").toString());
modify_sage.setText(item.get("Sage").toString());
modify_sphone.setText(item.get("Sphone").toString());
if(item.get("Ssex").toString().equals("男")){
maleButton.setChecked(true);
femaleButton.setChecked(true);
DialogalertDialog=newAlertDialog.Builder(StudentsActivity.this).
setView(myLoginView).
setPositiveButton("确认",newDialogInterface.OnClickListener(){
publicvoidonClick(DialogInterfacedialog,intwhich){
Stringsnum=modify_snum.getText().toString();
Stringsname=modify_sname.getText().toString();
Stringsphone=modify_sphone.getText().toString();
intsage=Integer.valueOf(modify_sage.getText().toString());
Stringssex="女";
if(maleButton.isChecked()){
ssex="男";
if(snum.equals("")){
Toast.makeText(StudentsActivity.this,"can'taddastudentwithoutSnum!",Toast.LENGTH_SHORT).show();
return;
Studentstudent=newStudent(snum,sname,ssex,sage,sphone);
db.updateStudent(student);
db.AddStudent(student);
Toast.makeText(StudentsActivity.this,"addstudenterror",Toast.LENGTH_SHORT).show();
Toast.makeText(StudentsActivity.this,"updatestudenterror",Toast.LENGTH_SHORT).show();
}).
setNegativeButton("撤销",newDialogInterface.OnClickListener(){
Toast.makeText(StudentsActivity.this,"",Toast.LENGTH_SHORT).show();
create();
alertDialog.show();
Toast.makeText(StudentsActivity.this,"runingwrong!",Toast.LENGTH_SHORT).show();
privatevoidupdatelistview(){
Stringstring=search_text.getText().toString();
List>data=newArrayList>();
if(!string.equals("")){
Studentstudent=db.findStudent(string);
students=db.findALLStudent();
Iteratorit=students.iterator();
while(it.hasNext()){
HashMaphashMap=newHashMap();
Studentstudent=it.next();
hashMap.put("Snum",student.getSnum());
hashMap.put("Sname",student.getSname());
hashMap.put("Ssex",student.getSsex());
hashMap.put("Sage",student.getSage());
hashMap.put("Sphone",student.getSphone());
data.add(hashMap);
SimpleAdapteradapter=
newSimpleAdapter(StudentsActivity.this,data,R.layout.student_view,
newString[]{"Snum","Sname","Ssex","Sage","Sphone"},
newint[]{R.id.student_view_snum,R.id.student_view_sname,R.id.student_view_ssex,R.id.student_view_sage,R.id.student_view_sphone});
listView.setAdapter(adapter);
Toast.makeText(StudentsActivity.this,"can'tfindthisstudent!",Toast.LENGTH_SHORT).show();
CourseActivity.java(对学生信息界面的操作):
publicclassCourseActivityextendsActivity{
privateButtonadd_course=null;
privateButtondelete_course=null;
setContentView(R.layout.course_layout);
setTitle("课程信息管理");
search=(Button)this.findViewById(R.id.search_course_button);
search_text=(EditText)this.findViewById(R.id.to_search_Course);
listView=(ListView)findViewById(R.id.course_listview);
add_course=(Button)this.findViewById(R.id.add_course);
delete_course=(Button)this.findViewById(R.id.delete_course);
db=newMyDB(CourseActivity.this);
add_course.setOnClickListener(newOnClickListener(){
delete_course.setOnClickListener(newOnClickListener(){
db.delALLCourse();
db.delCourse(search_text.getText().toString());
Toast.makeText(CourseActivity.this,"deletecourseerror!",Toast.LENGTH_SHORT).show();
LayoutInflaterlayoutInflater=LayoutInflater.from(CourseActivity.this);
ViewmyLoginView=layoutInflater.inflate(R.layout.course_edit,null);
finalEditTextmodify_cnum=(EditText)myLoginView.findViewById(R.id.modify_cnum_txt);
finalEditTextmodify_cname=(EditText)myLoginView.findViewById(R.id.modify_cname_txt);
finalEditTextmodify_credit=(EditText)myLoginView.findViewById(R.id.modify_credit_txt);
modify_cnum.setText("");
modify_cname.setText("");
modify_credit.setText("0");
modify_cnum.setEnabled(true);
modify_cnum.setText(item.get("Cnum").toString());
modify_cnum.setEnabled(false);
modify_cname.setText(item.get("Cname").toString());
modify_credit.setText(item.get("Ccredit").toString());
DialogalertDialog=newAlertDialog.Builder(CourseActivity.this).
Stringcnum=modify_cnum.getText().toString();
Stringcname=modify_cname.getText().toString();
floatcredit=Float.valueOf(modify_credit.getText().toString());
if(cnum.equals("")){
Toast.makeText(CourseActivity.this,"can'taddacoursewithoutCnum!",Toast.LENGTH_SHORT).show();
Coursecourse=newCourse(cnum,cname,credit);
db.updateCourse(course);
db.AddCourse(course);
Toast.makeText(CourseActivity.this,"addcourseerror",Toast.LENGTH_SHORT).show();
Toast.makeText(CourseActivity.this,"updatecourseerror",Toast.LENGTH_SHORT).show();
Toast.makeText(CourseActivity.this,"",Toast.LENGTH_SHORT).show();
Toast.makeText(CourseActivity.this,"runingwrong!",Toast.LENGTH_SHORT).show();
Listcourses=newArrayList();
Coursecourse=db.findCourse(string);
courses.add(course);
courses=db.findALLCourse();
Iteratorit=courses.iterator();
Coursecourse=it.next();
hashMap.put("Cnum",course.getCnum());
hashMap.put("Cname",course.getCname());
hashMap.put("Ccredit",course.getCcredit());
newSimpleAdapter(CourseActivity.this,data,R.layout.course_view,
newString[]{"Cnum","Cname","Ccredit"},
newint[]{R.id.course_view_cnum,R.id.course_view_cname,R.id.course_view_credit});
Toast.makeText(CourseActivity.this,"can'tfindthiscourse!",Toast.LENGTH_SHORT).show();
CSActivity.java(对成绩信息界面的操作):
publicclassCSActivityextendsActivity{
privateEditTextsearch_s=null;
privateEditTextsearch_c=null;
privateButtonadd_cs=null;
privateButtondelete_cs=null;
setContentView(R.layout.cs_layout);
setTitle("成绩信息管理");
search=(Button)this.findViewById(R.id.search_cs_button);
search_s=(EditText)this.findViewById(R.id.to_search_CS_S);
search_c=(EditText)this.findViewById(R.id.to_search_CS_C);
listView=(ListView)findViewById(R.id.cs_listview);
add_cs=(Button)this.findViewById(R.id.add_cs);
delete_cs=(Button)this.findViewById(R.id.delete_cs);
db=newMyDB(CSActivity.this);
add_cs.setOnClickListener(newOnClickListener(){
delete_cs.setOnClickListener(newOnClickListener(){
if(search_s.getText().toString().equals("0000")){
db.delCS(search_s.getText().toString(),search_c.getText().toString());
//(search_text.getText().toString());
search_s.setText("");
search_c.setText("");
Toast.makeText(CSActivity.this,"deletescoreerror!",Toast.LENGTH_SHORT).show();
LayoutInflaterlayoutInflater=LayoutInflater.from(CSActivity.this);
ViewmyLoginView=layoutInflater.inflate(R.layout.cs_edit,null);
finalEditTextmodify_score=(EditText)myLoginView.findViewById(R.id.modify_score_txt);
modify_score.setText("0");
modify_score.setText(item.get("Score").toString());
DialogalertDialog=newAlertDialog.Builder(CSActivity.this).
floatscore=Float.valueOf(modify_score.getText().toString());
if(snum.equals("")||cnum.equals("")){
Toast.makeText(CSActivity.this,"can'taddascorewithoutSnum&Cnum!",Toast.LENGTH_SHORT).show();
//Studentstudent=newStudent(snum,sname,ssex,sage,sphone);
CScs=newCS(snum,cnum,score);
db.updateCS(cs);
db.AddCS(cs);
Toast.makeText(CSActivity.this,"addscoreerror",Toast.LENGTH_SHORT).show();
Toast.makeText(CSActivity.this,"updatescoreerror",Toast.LENGTH_SHORT).show();
Toast.makeText(CSActivity.this,"",Toast.LENGTH_SHORT).show();
Toast.makeText(CSActivity.this,"runingwrong!",Toast.LENGTH_SHORT).show();
Strings=search_s.getText().toString();
Stringc=search_c.getText().toString();
ListCss=newArrayList();
List>data1=newArrayList>();
if(s.equals("")&&c.equals("")){
Css=db.findALLCS();
elseif(s.equals("")&&(!c.equals(""))){
Css=db.findCSbyCnum(c);
elseif((!s.equals(""))&&c.equals("")){
Css=db.findCSbySnum(s);
}else{
CScs=db.findCS(c,s);
Css.add(cs);
Iteratorit=Css.iterator();
inti=0;
CScs=it.next();
hashMap.put("Snum",cs.getSnum());
hashMap.put("Cnum",cs.getCnum());
hashMap.put("Score",cs.getScore());
data1.add(hashMap);
i=i+1;
Toast.makeText(CSActivity.this,"一共:"+i,Toast.LENGTH_SHORT).show();
newSimpleAdapter(CSActivity.this,data1,R.layout.cs_view,
newString[]{"Snum","Cnum","Score"},
newint[]{R.id.cs_view_snum,R.id.cs_view_cnum,R.id.cs_view_score});
Toast.makeText(CSActivity.this,"can'tfindthisscore!",Toast.LENGTH_SHORT).show();
MyDBhelper.java(创建数据库):
publicclassMyDBhelperextendsSQLiteOpenHelper{
privatestaticfinalStringname="contants";//数据库名称
privatestaticfinalintversion=1;//数据库版本
publicMyDBhelper(Contextcontext){
super(context,name,null,version);
//TODOAuto-generatedconstructorstub
publicvoidonCreate(SQLiteDatabasedb){
Stringstudents="createtableifnotexistsStudents(Snumvarchar(10)primarykey,Snamevarchar(6),Ssexvarchar(2),Sageinteger(3),Sphonevarchar(12))";
Stringcourses="createtableifnotexistsCourses(Cnumvarchar(4)primarykey,Cnamevarchar(10),Ccreditreal(3))";
Stringcs="createtableifnotexistsCS(Snumvarchar(10),Cnumvarchar(4),Scorereal(3),primarykey(Snum,Cnum))";
db.execSQL(students,bindArgs);
db.execSQL(courses,bindArgs);
db.execSQL(cs,bindArgs);
publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){
db.execSQL("DROPTABLEIFEXISTSStudents");
db.execSQL("DROPTABLEIFEXISTSCourses");
db.execSQL("DROPTABLEIFEXISTSCS");
this.onCreate(db);
MyDB.java(对数据库进行增删改查):
publicclassMyDB{
privateMyDBhelperdBhelper;
privateSQLiteDatabasemydb;
publicMyDB(Contextcontext){
this.dBhelper=newMyDBhelper(context);
/*增*/
/*删*/
/*改*/
/*查*/
publicCoursefindCourse(Stringcnumber){
Stringsql="select*fromCourseswhereCnum=";
String[]selectionArgs={cnumber};
returnnewCourse(cursor.getString(0),cursor.getString(1),cursor.getFloat(2));
publicCSfindCS(Stringcnumber,Stringsnumber){
Stringsql="select*fromCSwhereSnum=andCnum=";
String[]selectionArgs={snumber,cnumber};
returnnewCS(cursor.getString(0),cursor.getString(1),cursor.getFloat(2));
publicListfindCSbyCnum(Stringcnumber){
Stringsql="select*fromCSwhereCnum=";
Listcss=newArrayList();
CScs1=newCS(cursor.getString(0),cursor.getString(1),cursor.getFloat(2));
css.add(cs1);
returncss;
publicListfindCSbySnum(Stringsnumber){
Stringsql="select*fromCSwhereSnum=";
/*findALL函数*/
publicListfindALLCourse(){
Stringsql="select*fromCoursesorderbyCnumasc";
Coursecourse=newCourse(cursor.getString(0),cursor.getString(1),cursor.getFloat(2));