java学生管理系统.docx
《java学生管理系统.docx》由会员分享,可在线阅读,更多相关《java学生管理系统.docx(26页珍藏版)》请在冰豆网上搜索。
java学生管理系统
Java学生管理系统能够
题目及简介
在例12-3的基础上完善程序,做图形界面,在其中有多个选项“添加”、“修改”、“删除”、“查询”等,根据用户选择的功能再输入相应的数据并完成相关功能。
开发环境概述
java开发工具eclipse,SQLserver2005;
一:
描述
1.主界面上直接显示数据库中的所有数据,有添加、删除、修改、查询各个按钮
2.查询在主界面上实现。
根据学号和姓名查询。
没有新建类,代码在主界面类里。
3.修改和添加都新建了一个类(update和add),生成一个新的窗口,以实现功能。
删除的代码比较简单,也在主界面类里。
4.专门新建了一个连接类connectiondb,以实现与数据库的连接。
里面还包含了sql语句。
5.关于JTable的实现,专门建了一个类model,完成对JTable的初始化。
二:
界面截图
1.主界面
2.查询
3.添加
4.修改
5.删除
三:
源代码:
add类:
importjava.awt.BorderLayout;
importjava.awt.Frame;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JComboBox;
importjavax.swing.JDialog;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
publicclassaddextendsJDialogimplementsActionListener{
//定义组件
JPaneljp1,jp2,jp3;
JLabeljl1,jl2,jl3,jl4,jl5;
JButtonjb1,jb2;
JTextFieldjtf1,jtf2,jtf4;
JComboBoxjc3,jc5;
//构造器
publicadd(Frameowner,Stringtitle,booleanmodal){
super(owner,title,modal);
//定义jp1
jp1=newJPanel();
jl1=newJLabel("学号");
jl2=newJLabel("姓名");
jl3=newJLabel("性别");
jl4=newJLabel("出生年月");
jl5=newJLabel("专业");
jp1.add(jl1);
jp1.add(jl2);
jp1.add(jl3);
jp1.add(jl4);
jp1.add(jl5);
jp1.setLayout(newGridLayout(5,1));
//定义jp2
jp2=newJPanel();
jtf1=newJTextField(9);
jtf2=newJTextField(8);
jtf4=newJTextField(8);
jc3=newJComboBox();
jc3.addItem("男");
jc3.addItem("女");
jc5=newJComboBox();
jc5.addItem("计算");
jc5.addItem("信管");
jc5.addItem("地信");
jc5.addItem("日语");
jp2.add(jtf1);
jp2.add(jtf2);
jp2.add(jc3);
jp2.add(jtf4);
jp2.add(jc5);
jp2.setLayout(newGridLayout(5,1));
//定义jp3
jp3=newJPanel();
jb1=newJButton("确定");
jb2=newJButton("取消");
jp3.add(jb1);
jp3.add(jb2);
//注册监听
jb1.addActionListener(this);
jb2.addActionListener(this);
this.add(jp1,BorderLayout.WEST);
this.add(jp2,BorderLayout.CENTER);
this.add(jp3,BorderLayout.SOUTH);
//定义窗口
this.setSize(300,300);
this.setLocation(250,250);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
//响应函数
publicvoidactionPerformed(ActionEvente){
if(e.getSource().equals(jb1)){
String[]str=newString[5];
str[0]=jtf1.getText();
str[1]=jtf2.getText();
str[2]=jc3.getSelectedItem().toString().trim();
str[3]=jtf4.getText();
str[4]=jc5.getSelectedItem().toString().trim();
connectiondbc=newconnectiondb();
c.addSql(str);
JOptionPane.showMessageDialog(this,"添加成功!
");
this.dispose();//关闭对话框
}elseif(e.getSource().equals(jb2)){
this.dispose();
}
}
}
Connectiondb类
packagechapter12;
importjava.beans.Statement;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.PreparedStatement;
importjava.sql.SQLException;
publicclassconnectiondb{
StringdriverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
StringdbURL="jdbc:
sqlserver:
//127.0.0.1:
1433;DatabaseName=student";
ConnectiondbConn;
Statementstmt;
PreparedStatementpstmt=null;
StringuserName="sa";
StringuserPwd="123456";
//返回连接数据
publicConnectiongetdbConn(){
returndbConn;
}
publicconnectiondb(){
try{
Class.forName(driverName);
dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
}catch(Exceptione){
System.out.println("连接失败!
");
e.printStackTrace();
}
}
//添加信息时,调用的方法
publicvoidaddSql(String[]str){
Stringstrsql="insertintost1values(?
?
?
?
?
)";
try{
pstmt=dbConn.prepareStatement(strsql);
for(inti=0;ipstmt.setString(i+1,str[i]);
}
pstmt.executeUpdate();
}catch(Exceptione){
e.printStackTrace();
}finally
{
//释放语句对象连接的对象
allClose();
}
}
//删除信息时,用到的方法
publicvoiddelete(Stringstr){
try{
pstmt=dbConn.prepareStatement("deletefromst1wheresno=?
");
pstmt.setString(1,str);
pstmt.executeUpdate();
}catch(Exceptione){
e.printStackTrace();
}finally{
allClose();
}
}
//查询信息时,用到的方法
//publicvoidfindSQL(Stringstr){
//try{
//pstmt=dbConn.prepareStatement("select*fromst1wheresnamelike'"+str+"%'");
//pstmt.executeUpdate();
//}catch(Exceptione){
//e.printStackTrace();
//}finally{
//allClose();
//}
//}
//publicvoidfindSQL1(Stringstr){
//try{
//pstmt=dbConn.prepareStatement("select*fromst1wheresno='"+str+"'");
//pstmt.executeUpdate();
//}catch(Exceptione){
//e.printStackTrace();
//}finally{
//allClose();
//}
//}
//修改信息时,用到的方法
publicvoidallClose(){
try
{
if(pstmt!
=null)pstmt.close();
if(dbConn!
=null)dbConn.close();
}catch(Exceptionex)
{ex.printStackTrace();
}
}
publicvoidupdateIt(String[]str){
Stringstrsql="updatest1setsname=?
ssex=?
birthday=?
speciality=?
wheresno=?
";
try{
pstmt=dbConn.prepareStatement(strsql);
for(inti=0;ipstmt.setString(i+1,str[i]);
}
pstmt.executeUpdate();
}catch(Exceptione){
e.printStackTrace();
}finally
{
//释放语句对象连接的对象
allClose();
}
}
}
Model类
packagechapter12;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.util.ArrayList;
importjava.util.List;
importjavax.swing.table.AbstractTableModel;
publicclassmodelextendsAbstractTableModel{
ListrowData,columnNames;
PreparedStatementps=null;
Connectionct=null;
ResultSetrs=null;
Stringsql;
publicvoidway(Stringsql){
if(sql.equals("")){
sql="select*fromst1";
}
columnNames=newArrayList();
columnNames.add("学号");
columnNames.add("姓名");
columnNames.add("性别");
columnNames.add("生日");
columnNames.add("专业");
rowData=newArrayList();
connectiondbc=newconnectiondb();
try{
ct=c.getdbConn();
ps=ct.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
//rowData可以存放多行
Strings=rs.toString();
Listhang=newArrayList();
hang.add(rs.getString
(1));
hang.add(rs.getString
(2));
hang.add(rs.getString(3));
hang.add(rs.getString(4));
hang.add(rs.getString(5));
//加入到rowData
rowData.add(hang);
}
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
//rowData=newArrayList();
//rowData.add(columnNames);
}
publicmodel(){
this.way("");
}
publicmodel(Stringstr){
this.way(str);
}
@Override
publicintgetColumnCount(){
//TODOAuto-generatedmethodstub
returnthis.columnNames.size();
}
@Override
publicintgetRowCount(){
//TODOAuto-generatedmethodstub
returnthis.rowData.size();
}
@Override
publicObjectgetValueAt(introwIndex,intcolumnIndex){
//TODOAuto-generatedmethodstub
return((ArrayList)this.rowData.get(rowIndex)).get(columnIndex);
}
@Override
publicStringgetColumnName(intarg0){
//TODOAuto-generatedmethodstub
return(String)this.columnNames.get(arg0);
}
}
Update类
packagechapter12;
importjava.awt.Frame;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JDialog;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
publicclassupdateextendsJDialogimplementsActionListener{
JLabeljl1,jl2,jl3,jl4,jl5;
JTextFieldjtf1,jtf2,jtf3,jtf4,jtf5;
JButtonjb1,jb2;
JPaneljp1,jp2,jp3;
publicupdate(Frameowner,Stringtitle,booleanmodal,modelm,introwNum){
super(owner,title,modal);
jp1=newJPanel();
jl1=newJLabel("学号");
jl2=newJLabel("姓名");
jl3=newJLabel("性别");
jl4=newJLabel("出生年月");
jl5=newJLabel("专业");
jp1.setLayout(newGridLayout(5,1));
jp1.add(jl1);
jp1.add(jl2);
jp1.add(jl3);
jp1.add(jl4);
jp1.add(jl5);
jp2=newJPanel();
jtf1=newJTextField(9);
jtf1.setEditable(false);
jtf2=newJTextField(8);
jtf3=newJTextField
(2);
jtf4=newJTextField(8);
jtf5=newJTextField(10);
jtf1.setText((String)m.getValueAt(rowNum,0));
jtf2.setText((String)m.getValueAt(rowNum,1));
jtf3.setText((String)m.getValueAt(rowNum,2));
jtf4.setText((String)m.getValueAt(rowNum,3));
jtf5.setText((String)m.getValueAt(rowNum,4));
jp2.setLayout(newGridLayout(5,1));
jp2.add(jtf1);
jp2.add(jtf2);
jp2.add(jtf3);
jp2.add(jtf4);
jp2.add(jtf5);
jp3=newJPanel();
jb1=newJButton("修改");
jb2=newJButton("取消");
jb1.addActionListener(this);
jb2.addActionListener(this);
jp3.add(jb1);
jp3.add(jb2);
this.add(jp1,"Center");
this.add(jp2,"East");
this.add(jp3,"South");
this.setSize(200,300);
this.setLocation(250,250);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource().equals(jb1)){
String[]str=newString[5];
str[4]=jtf1.getText();
str[0]=jtf2.getText();
str[1]=jtf3.getText();
str[2]=jtf4.getText();
str[3]=jtf5.getText();
connectiondbc=newconnectiondb();
c.updateIt(str);
JOptionPane.showMessageDialog(this,"修改成功!
");
this.dispose();//关闭对话框
}elseif(e.getSource().equals(jb2)){
this.dispose();
}
}
}
Zhujiemian类
packagechapter12;
importjava.awt.GridBagConstraints;
importjava.awt.GridBagLayout;
importjava.awt.GridLayout;
importjava.awt.