Java课程设计.docx
《Java课程设计.docx》由会员分享,可在线阅读,更多相关《Java课程设计.docx(15页珍藏版)》请在冰豆网上搜索。
Java课程设计
Java课程设计
指导老师:
焦家林
学院:
计算机
姓名:
王若旭
学号:
101714119
目录
1.题目简介……………………………………………………
2.程序概要…………………………………………………….
3.程序函数简介……………………………………………….
4.源代码…………………………………………………………
5.运行截图……………………………………………………..
6.心得体会…………………………………………………….
1.题目简介
题目简介:
要求基于数据库管理系统,建立一个学生信息管理系统,有简单的显示、更新、插入等基本功能。
还有程序需有窗口,也就是运行界面中要含有窗口。
程序尽量简单,但功能得全面,界面做到尽善尽美!
2.程序概要
程序概要:
本程序中含有一个public类,三个一般类。
其主函数在public类中,一般类中每个类实现一个功能。
在主函数中调用一般类中的函数,实现不同的功能。
每个一般类也是建立在窗口上的,其中运用了窗口,组件,按钮,布局等多种知识。
3.程序函数简介
classShowRecordextendsJDialogimplementsActionListener{}//负责显示记录的类
classModifyRecordextendsJDialogimplementsActionListener{}//负责更新记录的类
classDeleteRecordextendsJDialogimplementsActionListener{}//负责删除记录的类
classInsertRecordextendsJDialogimplementsActionListener{}//负责插入记录的类
publicclassMykuextendsJFrameimplementsActionListener{}主界面函数
4.源代码
importjava.sql.*;
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.border.*;
classShowRecordextendsJDialogimplementsActionListener{//负责显示记录的类
JTabletable;
Objecta[][];
Objectname[]={"Name","Number","Age","Sex","Degree"};
JButtonshowRecord;
Connectioncon;
Statementsql;
ResultSetrs;
ShowRecord(Stringtitle)
{setTitle(title);
showRecord=newJButton("显示记录");
showRecord.addActionListener(this);
add(showRecord,BorderLayout.NORTH);
setBounds(200,60,400,250);
}
publicvoidactionPerformed(ActionEvente){
try{con=DriverManager.getConnection("jdbc:
odbc:
hh","","");
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery("SELECT*FROMStudentInformation");
rs.last();
intlastNumber=rs.getRow();
a=newObject[lastNumber][5];
intk=0;
rs.beforeFirst();
while(rs.next()){
a[k][0]=rs.getString
(1);
a[k][1]=rs.getLong
(2);
a[k][2]=rs.getLong(3);
a[k][3]=rs.getString(4);
a[k][4]=rs.getLong(5);
k++;
}
con.close();
}
catch(SQLExceptionee){
System.out.println(ee);
}
table=newJTable(a,name);
getContentPane().removeAll();
add(showRecord,BorderLayout.NORTH);
add(newJScrollPane(table),BorderLayout.CENTER);
validate();
}
}
classModifyRecordextendsJDialogimplementsActionListener{//负责更新记录的类
JLabelhintLabel,hintLabel1;
JTextFieldinputName;
JTextFieldinputNumber,inputAge,inputSex,inputDegree;
Objectname[]={"Number","Age","Sex","Degree"};
Objecta[][]=newObject[1][4];
JTabletable;
JButtonenterModify;
Connectioncon;
Statementsql;
ResultSetrs;
Stringname3,number,age,sex,degree;
ModifyRecord(Strings){
setTitle(s);
hintLabel=newJLabel("输入姓名(回车确认):
");
hintLabel1=newJLabel("输入:
");
inputName=newJTextField(5);
inputAge=newJTextField(5);
inputNumber=newJTextField(5);
inputDegree=newJTextField(5);
inputSex=newJTextField(5);
table=newJTable(a,name);
enterModify=newJButton("更新记录");
setLayout(null);
BoxbaseBox=Box.createHorizontalBox();
Boxbox=Box.createHorizontalBox();
baseBox.add(hintLabel);
baseBox.add(inputName);
baseBox.add(newJScrollPane(table));
box.add(hintLabel1);box.add(Box.createHorizontalStrut(100));
box.add(inputNumber);
box.add(inputAge);
box.add(inputSex);
box.add(inputDegree);
box.add(enterModify);
add(baseBox,BorderLayout.NORTH);
add(box,BorderLayout.CENTER);
baseBox.setBounds(10,40,520,38);
box.setBounds(10,100,580,38);
inputName.addActionListener(this);
enterModify.addActionListener(this);
setBounds(20,60,700,200);
System.out.println("gengxinjilu");
}
publicvoidactionPerformed(ActionEvente){
name3=inputName.getText().trim();
number=inputNumber.getText().trim();
age=inputAge.getText().trim();
sex=inputSex.getText().trim();
degree=inputDegree.getText().trim();
if(e.getSource()==inputName){
try{
con=DriverManager.getConnection("jdbc:
odbc:
hh","","");
sql=con.createStatement();
rs=sql.executeQuery("SELECT*FROMStudentInformationWHEREName='"+name3+"'");
booleanboo=rs.next();
if(boo==false){
JOptionPane.showMessageDialog
(this,"姓名不存在","提示",JOptionPane.WARNING_MESSAGE);
}
else{
a[0][0]=rs.getLong
(2);
a[0][1]=rs.getLong(3);
a[0][2]=rs.getString(4);
a[0][3]=rs.getLong(5);
table.repaint();
}
con.close();
}
catch(SQLExceptionee){System.out.println(ee);}}
if(e.getSource()==enterModify){
try{
con=DriverManager.getConnection("jdbc:
odbc:
hh","","");
sql=con.createStatement();
sql.executeUpdate("UPDATEStudentInformationSETStudentInformation.Number=('"+number+"')WHEREName='"+name3+"'");
sql.executeUpdate("UPDATEStudentInformationSETStudentInformation.Age=("+age+")WHEREName='"+name3+"'");
sql.executeUpdate("UPDATEStudentInformationSETStudentInformation.Sex=('"+sex+"')WHEREName='"+name3+"'");
sql.executeUpdate("UPDATEStudentInformationSETStudentInformation.Degree=("+degree+")WHEREName='"+name3+"'");
System.out.println("成功");
JOptionPane.showMessageDialog(this,"更新成功","成功",JOptionPane.PLAIN_MESSAGE);
con.close();
}
catch(SQLExceptionea){
JOptionPane.showMessageDialog
(this,"更新失败"+ea,"失败",JOptionPane.ERROR_MESSAGE);
}
}
}
}
classInsertRecordextendsJDialogimplementsActionListener{//负责插入记录的类
JLabelhintLabel;
Objectname[]={"Name","Number","Age","Sex","Degree",};
Objecta[][]=newObject[1][5];
JTabletable;
JButtonenterInsert;
Connectioncon;
Statementsql;
ResultSetrs;
InsertRecord(Strings){
setTitle(s);
hintLabel=newJLabel("输入新记录:
");
table=newJTable(a,name);
enterInsert=newJButton("插入新记录");
setLayout(null);
BoxbaseBox=Box.createHorizontalBox();
baseBox.add(hintLabel);
baseBox.add(newJScrollPane(table));
baseBox.add(as);
baseBox.add(enterInsert);
add(baseBox);
baseBox.setBounds(10,40,600,38);
enterInsert.addActionListener(this);
setBounds(120,160,700,200);
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==enterInsert){
table.requestFocus();
try{con=DriverManager.getConnection("jdbc:
odbc:
hh","","");
sql=con.createStatement();
intk=sql.executeUpdate("INSERTINTOStudentInformationVALUES('"+a[0][0]+"',"+a[0][1]+","+a[0][2]+",'"+a[0][3]+"',"+a[0][4]+")");
System.out.println(a[0][4]);
if(k==1)
JOptionPane.showMessageDialog
(this,"插入记录成功","成功",JOptionPane.PLAIN_MESSAGE);
con.close();
}
catch(SQLExceptionee){
JOptionPane.showMessageDialog
(this,"插入记录失败"+ee,"失败",JOptionPane.ERROR_MESSAGE);}}}
}
classDeleteRecordextendsJDialogimplementsActionListener{//负责删除记录的类
JLabelhintLabel;
JTextFieldinputNumber;
Objectname[]={"Number","Age","Sex","Degree"};
Objecta[][]=newObject[1][4];
JTabletable;
JButtondelete;
Connectioncon;
Statementsql;
ResultSetrs;
Stringname2;
DeleteRecord(Strings){
setTitle(s);
hintLabel=newJLabel("删除记录");
inputNumber=newJTextField(20);
table=newJTable(a,name);
delete=newJButton("删除");
setLayout(null);
BoxbaseBox=Box.createHorizontalBox();
baseBox.add(hintLabel);
baseBox.add(inputNumber);
baseBox.add(newJScrollPane(table));
baseBox.add(delete);
add(baseBox);
baseBox.setBounds(10,40,600,38);
inputNumber.addActionListener(this);
delete.addActionListener(this);
setBounds(20,60,700,200);
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==inputNumber){
try{name2=inputNumber.getText().trim();
con=DriverManager.getConnection("jdbc:
odbc:
hh","","");
sql=con.createStatement();
rs=sql.executeQuery("SELECT*FROMStudentInformationWHEREName='"+name2+"'");
booleanboo=rs.next();
if(boo==false){
JOptionPane.showMessageDialog
(this,"姓名不存在","提示",JOptionPane.WARNING_MESSAGE);
}
else{
a[0][0]=String.valueOf(rs.getLong
(2));
a[0][1]=String.valueOf(rs.getLong(3));
a[0][2]=rs.getString(4);
a[0][3]=String.valueOf(rs.getLong(5));
table.repaint();
}
con.close();
}
catch(SQLExceptionee){
System.out.println(ee);}}
if(e.getSource()==delete){
try{con=DriverManager.getConnection("jdbc:
odbc:
hh","","");
sql=con.createStatement();
sql.executeUpdate("DELETEFROMStudentInformationWHEREName='"+name2+"'");
JOptionPane.showMessageDialog
(this,"删除成功","成功",JOptionPane.PLAIN_MESSAGE);
con.close();
}
catch(SQLExceptionee){
JOptionPane.showMessageDialog
(this,"删除失败"+ee,"失败",JOptionPane.ERROR_MESSAGE);
}
}
}
}
publicclassxindeextendsJFrameimplementsActionListener
{JButtonInsert,Delete,Update,Show;
JPanelps;
JTabletable;
Objecta[][]=newObject[10][5];
Objectname[]={"Name","Number","Age","Sex","Degree"};
Connectioncon;
Statementsql;
ResultSetrs;
xinde(){
setTitle("数据库管理");
Insert=newJButton("插入");
Delete=newJButton("删除");
Update=newJButton("更新");
Show=newJButton("刷新");
Insert.addActionListener(this);
Delete.addActionListener(this);
Update.addActionListener(this);
Show.addActionListener(this);
ps=newJPanel();
ps.add(Show);
ps.add(Insert);
ps.add(Delete);
ps.add(Update);
add(ps,BorderLayout.SOUTH);
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(Exceptionce){}
try{con=DriverManager.getConnection("jdbc:
odbc:
hh","","");
sql=con.createStatement();
rs=sql.executeQuery("SELECT*FROMStudentInformationorderbyName");
intk=0;
while(rs.next()){
a[k][0]=rs.getString
(1);
a[k][1]=rs.getLong
(2);
a[k][2]=rs.getLong(3);
a[k][3]=rs.getString(4);
a[k][4]=rs.getLong(5);
k++;
}
con.close();
}
catch(Exceptionxe){}
table=newJTable(a,name);
add(newJScrollPane(table),BorderLayout.CENTER);
validate();
setBounds(500,250,600,250);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
publicvoidactionPerformed(ActionEventae){
if(ae.getSource()==Insert){
InsertRecordINsert=newInsertRecord("插入数据");
INsert.setVisible(true);
}
if(ae.getSo