学生信息管理系统模块一java代码

上传人:飞****9 文档编号:127851667 上传时间:2020-04-06 格式:PDF 页数:39 大小:159.27KB
返回 下载 相关 举报
学生信息管理系统模块一java代码_第1页
第1页 / 共39页
学生信息管理系统模块一java代码_第2页
第2页 / 共39页
学生信息管理系统模块一java代码_第3页
第3页 / 共39页
学生信息管理系统模块一java代码_第4页
第4页 / 共39页
学生信息管理系统模块一java代码_第5页
第5页 / 共39页
点击查看更多>>
资源描述

《学生信息管理系统模块一java代码》由会员分享,可在线阅读,更多相关《学生信息管理系统模块一java代码(39页珍藏版)》请在金锄头文库上搜索。

1、packagepackagepackagepackage com studentsystem2 importimportimportimport java awt importimportimportimport java awt event importimportimportimport javax swing publicpublicpublicpublic classclassclassclass StudentManagement extendsextendsextendsextends JFrame implementsimplementsimplementsimplements

2、ActionListener 定义一些控件 JPanel jp1 jp2 JLabel jl JButton jb1 jb2 jb3 jb4 JTable jt JTextField jtf JScrollPane jsp nullnullnullnull StuModel sm nullnullnullnull publicpublicpublicpublic staticstaticstaticstatic voidvoidvoidvoid main String args StudentManagementta newnewnewnew StudentManagement publicp

3、ublicpublicpublicStudentManagement jp1 newnewnewnew JPanel jl newnewnewnew JLabel 请输入名字 jtf newnewnewnew JTextField 20 jb1 newnewnewnew JButton 查询 注册监听 jb1 addActionListener thisthisthisthis 把各个控件加入到jp1 jp1 add jl jp1 add jtf jp1 add jb1 jp2 newnewnewnew JPanel jb2 newnewnewnew JButton 添加 jb3 newnew

4、newnewJButton 修改 jb4 newnewnewnewJButton 删除 注册监听 jb2 addActionListener thisthisthisthis jb3 addActionListener thisthisthisthis jb4 addActionListener thisthisthisthis jp2 add jb2 jp2 add jb3 jp2 add jb4 创建一个数据模型对象 sm newnewnewnew StuModel 初始化JTable jt newnewnewnew JTable sm 初始化jsp jsp newnewnewnew JS

5、crollPane jt 吧jsp放进到JFrame thisthisthisthis add jsp thisthisthisthis add jp1 BorderLayout NORTH thisthisthisthis add jp2 BorderLayout SOUTH thisthisthisthis setSize 500 400 thisthisthisthis setLocation 200 200 thisthisthisthis setDefaultCloseOperation JFrame EXIT ON CLOSE thisthisthisthis setVisible

6、 truetruetruetrue Override publicpublicpublicpublic voidvoidvoidvoid actionPerformed ActionEvent e TODOTODOTODOTODO Auto generated method stub 判断是那个按钮被点击 如果相应与监听在同一个类中 也可以用下面方法 ifififif e getSource jb1 因为把表的数据封装到StuModel中 我们就可 以比较简单地完成查询任务 String name thisthisthisthis jtf getText trim 写一个sql语句 Strin

7、g sql select from stu where stuName name 构建新的数据模型类并更新 ifififif name trim equals sm newnewnewnew StuModel elseelseelseelse sm newnewnewnew StuModel sql jt setModel sm 当用户点击添加 elseelseelseelse ifififif e getSource jb2 合理应该为模式的状态 否则 还没有插完就会 执行下面的语句 导致无法更新 StuAddDialogsad newnewnewnew StuAddDialog thist

8、histhisthis 添加学生 truetruetruetrue 重新再获得新的数据模型 sm newnewnewnew StuModel jt setModel sm elseelseelseelse ifififif e getSource jb3 System out print aaaa intintintint rownum thisthisthisthis jt getSelectedRow ifififif rownum 1 提示 JOptionPane showMessageDialog thisthisthisthis 请选择一行 returnreturnreturnret

9、urn 代表不要再往下面走了 谁调用就 返回给谁 显示修改对话框 newnewnewnew StudentUpdateDialog thisthisthisthis 修改对话 框 truetruetruetrue sm rownum 当前用户点击删除 elseelseelseelse ifififif e getSource jb4 得到该学生的id getSelectedRo会返回用户点击的行 如果一行都没选 则会返回 1 intintintint rownum thisthisthisthis jt getSelectedRow ifififif rownum 1 提示 JOptionPa

10、ne showMessageDialog thisthisthisthis 请选择一行 returnreturnreturnreturn 代表不要再往下面走了 谁调用就 返回给谁 得到学生的编号 String stuId String sm getValueAt rownum 0 System out print stuId 测试用的 StuModel temp newnewnewnew StuModel 创建一个sql 语句 Stringsql deletefromstuwhere stuId String paras stuId ifififif temp updateStudent sq

11、l paras 提示 JOptionPane showMessageDialog thisthisthisthis 删除失败 sm newnewnewnew StuModel 解决一次无用的查 询 jt setModel sm package com studentsystem1 import java sql Connection import java sql DriverManager import java sql PreparedStatement import java sql ResultSet import java util Vector import javax swing

12、 table AbstractTableModel 这是 stu 的模型 publicclassStudentModelextends AbstractTableModel rowData 存放行的数据 columnNames 存放列的字段 Vector rowData columnNames Connection conn null PreparedStatement ps ResultSet rs null 构造方法 用于初始化数据模型 public StudentModel this init public StudentModel String sql this init sql pu

13、blic void init String sql if sql trim equals sql select from stu 中间显示数据结果 存放列名字段 columnNames new Vector 设置列名 columnNames add 学号 columnNames add 姓名 columnNames add 性别 columnNames add 年龄 columnNames add 籍贯 columnNames add 系别 存放行 rowData new Vector try Class forName com mysql jdbc Driver Stringurl jdbc

14、 mysql localhost 3306 student String user root String psword 123456 Class forName com microsoft jdbc sqlserver SQLServerDriver Stringurl jdbc microsoft sqlserver localhost 1433 databaseName student String user sa String psword 123456 conn DriverManager getConnection url user psword ps conn prepareSt

15、atement sql if sql startsWith delete ps executeUpdate else rs ps executeQuery while rs next Vector row1 new Vector row1 add rs getString 1 row1 add rs getString 2 row1 add rs getString 3 row1 add rs getInt 4 row1 add rs getString 5 row1 add rs getString 6 rowData add row1 catch Exception e e printSt

16、ackTrace finally try if conn null conn close if ps null ps close if rs null rs close catch Exception e e printStackTrace Override 得到共有多少行 public int getColumnCount TODO Auto generated method stub return this columnNames size Override 得到共有多少列 public int getRowCount TODO Auto generated method stub return this rowData size Override public String getColumnName int column TODO Auto generated method stub return String this columnNames get column Override 得到某行某列的数据 public Object getValueAt int rowIndex

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 其它中学文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号