Java学生信息管理系统Swing窗体Word格式.docx
《Java学生信息管理系统Swing窗体Word格式.docx》由会员分享,可在线阅读,更多相关《Java学生信息管理系统Swing窗体Word格式.docx(46页珍藏版)》请在冰点文库上搜索。
可以按照id删除学生的信息,然后显示找到该人。
若查无此人,显示相应的错误信息。
1.3(控制台方式)完善应用程序,实现相应的功能如下:
添加学生信息、修改指定学号的学生信息、显示所有学生信息、按学号查找、按姓名查找、按学号删除、按成绩排序和退出。
1.4(Swing窗体方式)完善应用程序,实现相应的功能如下:
(2)处理思路及关键技术说明
(3)程序完整源码(要求格式规范,适当注释)
(4)运行结果截图(按每个操作分别截图)
分三个模块:
一是jdbc的代码,另一个是Swing和AWT的代码,剩下是Student类。
Jdbc一共有两个类:
DBUtil(连接工具类)和Sql类(处理jdbc功能的静态方法)
Swing和AWT一个有两个类:
MyJFrame构建总窗口,MyJPanel对窗口内的面板行修饰。
然后就是Student类和Main主方法类。
关键部分:
1)在主窗体中add三个Jpanel,底层和第二层左右三个,右边的Jpanel实现cardLayout布局,左边的Jpanel添加按钮并添加ActionListener来实现对象的card切换达到不同按钮进入不同功能面板的目的。
2)在MyJPanel类中,所有的init方法均传递参数JPanelp,对传递过来的Panel进行加工修饰,并且调用Sql里面对应功能的方法。
3)Sql中的显示全部信息的方法中使用了ArrayList来存储从studenttbl中读取的对象信息,存储之后为了按照String的方式显示在JtextArea中,又用了listToString方法,通过for循环并追加回车,使每个对象都转化为String并在后面加上了回车(separator),
4)在Sql中的查找,删除,添加都使用了jdbc中的预处理PreparedStatement
在操作完毕后执行sts.executeUpdate();
5)在Sql排序方法中,把所有的信息都存储到list中以后,调用工具类Collections.sort:
根据指定比较器产生的顺序对指定列表进行排序。
classDBUtil
packagetest;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.SQLException;
publicclassDBUtil{
privatestaticStringdriver;
privatestaticStringurl;
privatestaticStringusername;
privatestaticStringpassword;
static{
driver="
com.mysql.jdbc.Driver"
;
url="
jdbc:
mysql:
//localhost:
3306/mydb"
username="
root"
password="
123456"
}
publicstaticConnectionopen(){
try{
Class.forName(driver);
returnDriverManager.getConnection(url,username,password);
}catch(Exceptione){
e.printStackTrace();
}
returnnull;
publicstaticvoidclose(Connectionconn){
if(conn!
=null){
try{
conn.close();
}catch(SQLExceptione){
e.printStackTrace();
}
}
classSql
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.Statement;
importjava.util.ArrayList;
importjava.util.Collections;
importjava.util.Comparator;
importjava.util.List;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JTextArea;
publicclassSql{
publicstaticStringlistToString(List<
Student>
list,charseparator){//使list返回String类型并加入控制符(换行)
StringBuildersb=newStringBuilder();
for(inti=0;
i<
list.size();
i++){
sb.append(list.get(i)).append(separator);
}
returnsb.toString().substring(0,sb.toString().length()-1);
publicstaticList<
disAll(){//打印显示所有信息
//查询
Connectionconn=DBUtil.open();
Stringsql="
selectid,name,frctionJava,fractionMath,fractionOSfromstudentTbl"
Statementstmt;
stmt=conn.createStatement();
ResultSetrs=stmt.executeQuery(sql);
List<
list=newArrayList<
();
//遍历操作
while(rs.next()){
intid=rs.getInt
(1);
Stringname=rs.getString
(2);
doublea=rs.getDouble(3);
doubleb=rs.getDouble(4);
doublec=rs.getDouble(5);
Studentu=newStudent();
u.setId(id);
u.setName(name);
u.setFractionJava(a);
u.setFractionMath(b);
u.setFractionOS(c);
list.add(u);
}
conn.close();
returnlist;
}catch(SQLExceptione){
publicstaticvoidFindByID(longid,JLabellblNewLabel){//按ID查找
ResultSetrs=null;
Stringsql="
select*fromStudentTblwhereid=?
"
PreparedStatementsts=conn.prepareStatement(sql);
sts.setLong(1,id);
rs=sts.executeQuery();
inti=rs.getInt
(1);
lblNewLabel.setText("
|"
+i+"
+name+"
+a+"
+b+"
+c);
}catch(Exceptione){
}finally{
if(rs.first()!
=true)
lblNewLabel.setText("
~~~~~~~查无此人!
~~~~~~~~"
);
publicstaticvoidFindByName(Strings,JLabellblNewLabel){//按Name查找
select*fromStudentTblwherename=?
sts.setString(1,s);
while(rs.next())
{
+id+"
try{
}catch(SQLExceptione)
{
publicstaticvoidDeleteByID(longid){//删除
PreparedStatementsts=null;
inta=0;
sts=conn.prepareStatement("
deletefromStudentTblwhereid=?
a=sts.executeUpdate();
if(a>
0)
JOptionPane.showMessageDialog(null,"
删除成功!
"
WO"
JOptionPane.INFORMATION_MESSAGE);
if(a==0)
无此人!
}catch(Exceptione){
DBUtil.close(conn);
publicstaticvoidSort(JTextAreata,intindex){//排序
//查询
Connectionconn=DBUtil.open();
Statementstmt;
stmt=conn.createStatement();
ResultSetrs=stmt.executeQuery(sql);
List<
//遍历操作
while(rs.next()){
intid=rs.getInt
(1);
Stringname=rs.getString
(2);
doublea=rs.getDouble(3);
doubleb=rs.getDouble(4);
doublec=rs.getDouble(5);
Studentu=newStudent();
u.setId(id);
u.setName(name);
u.setFractionJava(a);
u.setFractionMath(b);
u.setFractionOS(c);
list.add(u);
}
Collections.sort(list,newComparator<
()
{
publicintcompare(Studento1,Studento2)
{
inti=1;
if(index==1)
{i=(int)(o2.getFractionJava()-o1.getFractionJava());
if(index==2){
i=(int)(o2.getFractionMath()-o1.getFractionMath());
}
if(index==3){
i=(int)(o2.getFractionOS()-o1.getFractionOS());
returni;
}
});
Strings=listToString(list,'
\n'
ta.setText(s);
}
publicstaticvoidadd(Studentstu){//添加
PreparedStatementsts;
sts=conn.prepareStatement("
insertintoStudentTblvalues(?
?
)"
sts.setLong(1,stu.getId());
sts.setString(2,stu.getName());
sts.setDouble(3,stu.getFractionJava());
sts.setDouble(4,stu.getFractionMath());
sts.setDouble(5,stu.getFractionOS());
sts.executeUpdate();
}
DBUtil.close(conn);
publicstaticvoidUpdateStudent(Studentstu){//更新
Connectionconn=DBUtil.open();
updatestudenttblsetname='
+stu.getName()+"
'
FrctionJava='
+stu.getFractionJava()+"
FractionMath='
+stu.getFractionMath()+"
FractionOS='
+stu.getFractionOS()+"
whereid='
+stu.getId()+"
}catch(Exceptione){
}
classStudent
importjava.io.Serializable;
publicclassStudentimplementsSerializable{
privatestaticfinallongserialVersionUID=4558876142427402513L;
privateStringname;
privatelongid;
privatedoublefractionOS;
privatedoublefractionJava;
privatedoublefractionMath;
publicStringgetName(){
returnname;
publicvoidsetName(Stringname){
this.name=name;
publiclonggetId(){
returnid;
publicvoidsetId(longid){
this.id=id;
publicdoublegetFractionOS(){
returnfractionOS;
publicvoidsetFractionOS(doublefractionOS){
this.fractionOS=fractionOS;
publicdoublegetFractionJava(){
returnfractionJava;
publicvoidsetFractionJava(doublefractionJava){
this.fractionJava=fractionJava;
publicdoublegetFractionMath(){
returnfractionMath;
publicvoidsetFractionMath(doublefractionMath){
this.fractionMath=fractionMath;
publicStringtoString(){
return(name+"
"
Java:
+fractionJava+
"
Math:
+fractionMath+"
OS:
+fractionOS);
classMyJFrame
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjava.awt.BorderLayout;
importjava.awt.CardLayout;
importjava.awt.Insets;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjava.awt.Color;
importjavax.swing.SwingConstants;
importjava.awt.Font;
publicclassMyJFram