java学生选课系统Word文档格式.docx
《java学生选课系统Word文档格式.docx》由会员分享,可在线阅读,更多相关《java学生选课系统Word文档格式.docx(11页珍藏版)》请在冰豆网上搜索。
![java学生选课系统Word文档格式.docx](https://file1.bdocx.com/fileroot1/2022-12/1/4ea26b1f-2248-4942-80b1-8687c871c2e3/4ea26b1f-2248-4942-80b1-8687c871c2e31.gif)
importjava.sql.*;
importjavax.swing.*;
importjavax.swing.table.DefaultTableModel;
importschool.schoolPicture.JdbcUtil;
publicclassStudentChooseCourseextendsJFrameimplementsActionListener{
JTextFieldjtfSearch=newJTextField(11);
String[]columnNames=newString[]{"
课程ID"
"
课程名"
学时"
学分"
学期"
性质"
};
DefaultTableModeldtmSearch=newDefaultTableModel(columnNames,27);
JTablejtSearch=newJTable(dtmSearch);
JScrollPanejspSearch=newJScrollPane(jtSearch);
JComboBoxjcbSearch=newJComboBox();
JTextFieldjtfSelectedCourse=newJTextField(10);
Connectioncon=null;
PreparedStatementps=null;
ResultSetrs=null;
privatestaticStringusrName;
privatestaticStringpasswrd;
publicStudentChooseCourse(StringusrName,Stringpasswrd){
createSearchCourse();
this.usrName=usrName;
this.passwrd=passwrd;
}
publicStringgetUsrName(){
returnusrName;
publicvoidsetUsrName(StringusrName){
publicStringgetPasswrd(){
returnpasswrd;
publicvoidsetPasswrd(Stringpasswrd){
publicvoidactionPerformed(ActionEvente){
Stringstr=e.getActionCommand();
if("
查询"
.trim().equals(str)){
intk=1;
while(k<
10){
for(inti=1;
i<
7;
i++){
jtSearch.setValueAt(null,k-1,i-1);
k++;
searchAvailableCourse();
}elseif("
提交"
.equals(str)){
booleaneffect=processBeforeCommit();
if(effect==true){tryCommit();
publicbooleanprocessBeforeCommit(){
StringuserInput=jtfSelectedCourse.getText().toString().trim()
.toLowerCase();
Stringsql="
selectcnofromcoursexxwherecno=?
"
;
booleanflagCourseExist=false;
try{
ps=con.prepareStatement(sql);
ps.setString(1,userInput);
rs=ps.executeQuery();
flagCourseExist=rs.next();
}catch(ExceptioneC){
eC.printStackTrace();
if(!
flagCourseExist){
JOptionPane.showMessageDialog(null,"
该课程不存在,请重新输入"
);
returnfalse;
sql="
selectsum(grade)"
+"
from(selectx.sname,o,k.gradegrade"
fromcoursexxkjoinchoicesxxx"
ono=oandx.sname=?
)result"
Stringgrade=null;
ps.setString(1,usrName);
while(rs.next()){
grade=rs.getString
(1);
if(grade==null){grade="
0"
}catch(Exceptionrrr){
rrr.printStackTrace();
System.out.println("
总学分:
"
+grade);
if(Integer.parseInt(grade)>
6){
你已经选满6学分,系统将退出"
this.setVisible(false);
select*fromchoicesxxwheresname=?
andcno=?
booleanflag=false;
ps.setString(1,this.getUsrName());
ps.setString(2,userInput);
flag=rs.next();
}catch(Exceptioneaa){
eaa.printStackTrace();
if(flag){
你已经选择了该课程。
请另选课程"
returntrue;
publicvoidtryCommit(){
PreparedStatementps;
select(Max-selectedCount)asRemainedCount"
fromCoursexxwherecno=?
ResultSetMetaDatameta=rs.getMetaData();
intcols=meta.getColumnCount();
intRemainedCount=-1;
RemainedCount=rs.getInt
(1);
RemainedCount:
+RemainedCount);
if(RemainedCount>
0){
insertintochoicesxxvalues(?
?
)"
ps.executeUpdate();
选课成功:
+this.getUsrName()
选了"
+userInput+"
."
+"
还有"
+RemainedCount
人可以选该课程。
updateCourseXXsetselectedCount=selectedCount+1wherecno=?
mit();
}
}catch(Exceptiones){
es.printStackTrace();
con.rollback();
}catch(Exceptioney){
ey.printStackTrace();
publicvoidsearchAvailableCourse(){
if(JDBCUtil.getConnection()!
=null){
System.out.println(JDBCUtil.getConnection());
con=JDBCUtil.getConnection();
}else{
con=JdbcUtil.getConnection();
StringuserInput=jtfSearch.getText().toString().trim().toLowerCase();
StringselectedItem=jcbSearch.getSelectedItem().toString().trim();
Usersearch:
+userInput);
selectedItem:
+selectedItem);
Stringsql=null;
.equals(selectedItem)){
selectcno,cname,hour,grade,term,isNeedfromCourseXXwherecname=?
selectcno,cname,hour,grade,term,isNeedfromCourseXXwherecno=?
selectcno,cname,hour,grade,term,isNeedfromCourseXXwherehour=?
ps.setInt(1,Integer.parseInt(userInput));
selectcno,cname,hour,grade,term,isNeedfromCourseXXwheregrade=?
selectcno,cname,hour,grade,term,isNeedfromCourseXXwhereterm=?
System.out.println(sql);
Stringresult=null;
=cols;
result=rs.getString(i);
System.out.println(result);
jtSearch.setValueAt(result,k-1,i-1);
flag=true;
if(flag==false){
return;
}}catch(Exceptionex){
ex.printStackTrace();
}catch(Exceptioner){
er.printStackTrace();
publicvoidcreateSearchCourse(){
this.setLayout(newGridLayout(3,1));
JPaneljp1=newJPanel();
jp1.setLayout(newGridLayout(4,1));
JPaneljp2=newJPanel();
JPaneljp3=newJPanel();
JPaneljp10=newJPanel();
JPaneljp11=newJPanel();
JPaneljp12=newJPanel();
JPaneljp13=newJPanel();
JLabeljlSearch=newJLabel("
学生选课系统"
jp11.add(jlSearch);
jcbSearch.addItem(newString("
));
jp12.add(jtfSearch);
jp12.add(jcbSearch);
JButtonjbOK=newJButton("
jbOK.addActionListener(this);
jbOK.setSize(90,20);
jp13.add(jbOK);
jp1.add(jp10);
jp1.add(jp11);
jp1.add(jp12);
jp1.add(jp13);
jp2.add(jspSearch);
JLabeljlSelectedCourse=newJLabel("
请输入课程ID:
JButtonjbSelectedCourse=newJButton("
jbSelectedCourse.addActionListener(this);
jp3.add(jlSelectedCourse);
jp3.add(jtfSelectedCourse);
jp3.add(jbSelectedCourse);
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.setVisible(true);
this.setSize(485,600);
publicstaticvoidmain(String[]args){
//StringusrName="
xuliang"
//Stringpasswrd="
123"
newStudentChooseCourse(usrName,passwrd);
droptableregisterXu;
createtableregisterXu(idvarchar(20),userNamevarchar(20),passWordvarchar(20),identifyvarchar(20));
insertintoregisterXuvalues('
s001'
'
xuliang'
123'
学生'
s002'
xuliang2'
1234'
j001'
jack'
12345'
001'
user'
z001'
zlm'
corejava'
老师'
select*fromregisterXu"
whereuserName=?
andpassWord=?
andidentify=?
//table2:
Coursexx课程表
droptableCoursexx;
createtableCoursexx(cnovarchar(20)primarykey,cnamevarchar(20),hourInt,
gradeInt,termvarchar(20),isNeedvarchar(20),selectedCountInt,MaxInt);
insertintoCoursexxvalues('
c001'
CoreJava'
50,5,'
201007'
NoNecessary'
0,50);
c002'
XML'
20,2,'
201008'
0,40);
c003'
HIBERNATE'
20,4,'
0,30);
c004'
SQL'
201006'
0,5);
c005'
JDBC'
0,3);
c006'
AJAX'
201010'
0,1);
c007'
JSP'
100,8,'
201009'
0,1
//table3:
学生表--
droptablestudentxx;
createtablestudentxx(sidvarchar(20),snamevarchar(20)primarykey,sexvarchar(20),birthdayvarchar(20),
classNamevarchar(20),imagevarchar(20));
insertintostudentxxvalues('
xuliang