




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Java 学生管理系统能够学生管理系统能够 题目及简介 在例 12-3 的基础上完善程序,做图形界面,在其中有多个选项“添 加” 、 “修改” 、 “删除” 、 “查询”等,根据用户选择的功能再输入相 应的数据并完成相关功能。 开发环境概述 JAVA 开发工具 ECLIPSE,SQL SERVER 2005; 一:描述一:描述 1. 主界面上直接显示数据库中的所有数据,有添加、 删除、修改、查询各个按钮 2. 查询在主界面上实现。根据学号和姓名查询。没有 新建类,代码在主界面类里。 3. 修改和添加都新建了一个类(update 和 add) ,生成 一个新的窗口,以实现功能。删除的代码比较简单
2、, 也在主界面类里。 4. 专门新建了一个连接类 connectiondb,以实现与数 据库的连接。里面还包含了 sql 语句。 5. 关于 JTable 的实现,专门建了一个类 model,完成 对 JTable 的初始化。 二:界面截图二:界面截图 1. 主界面 2. 查询 3. 添加 4. 修改 5. 删除 三:源代码:三:源代码: add 类:类: import java.awt.BorderLayout; import java.awt.Frame; import java.awt.GridLayout; import java.awt.event.ActionEvent; impo
3、rt java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class add extends JDialog imp
4、lements ActionListener /定义组件 JPanel jp1,jp2,jp3; JLabel jl1,jl2,jl3,jl4,jl5; JButton jb1,jb2; JTextField jtf1,jtf2,jtf4; JComboBox jc3,jc5; /构造器 public add(Frame owner,String title,boolean modal) super(owner,title,modal); /定义 jp1 jp1 = new JPanel(); jl1 = new JLabel(学号); jl2 = new JLabel(姓名); jl3 =
5、new JLabel(性别); jl4 = new JLabel(出生年月); jl5 = new JLabel(专业); jp1.add(jl1); jp1.add(jl2); jp1.add(jl3); jp1.add(jl4); jp1.add(jl5); jp1.setLayout(new GridLayout(5,1); /定义 jp2 jp2 = new JPanel(); jtf1 = new JTextField(9); jtf2 = new JTextField(8); jtf4 = new JTextField(8); jc3 = new JComboBox(); jc3.
6、addItem(男); jc3.addItem(女); jc5 = new JComboBox(); 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(new GridLayout(5,1); /定义 jp3 jp3 = new JPanel(); jb1 = new JButton(确定); jb2 = new JButton(取消);
7、 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.setDefaultCloseOpera
8、tion(JFrame.DISPOSE_ON_CLO SE); /响应函数 public void actionPerformed(ActionEvent e) if(e.getSource().equals(jb1) String str = new String5; str0 = jtf1.getText(); str1 = jtf2.getText(); str2 = jc3.getSelectedItem().toString().trim(); str3 = jtf4.getText(); str4 = jc5.getSelectedItem().toString().trim();
9、 connectiondb c = new connectiondb(); c.addSql(str); JOptionPane.showMessageDialog(this, 添加成功! ); this.dispose();/关闭对话框 else if(e.getSource().equals(jb2) this.dispose(); Connectiondb 类类 package chapter12; import java.beans.Statement; import java.sql.Connection; import java.sql.DriverManager; import
10、java.sql.PreparedStatement; import java.sql.SQLException; public class connectiondb String driverName = com.microsoft.sqlserver.jdbc.SQLServerDriver; String dbURL = jdbc:sqlserver:/127.0.0.1:1433;DatabaseName=student ; Connection dbConn; Statement stmt; PreparedStatement pstmt=null; String userName
11、= sa; String userPwd = ; /返回连接数据 public Connection getdbConn() return dbConn; public connectiondb() try Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL,userName,userPwd); catch (Exception e) System.out.println(连接失败!); e.printStackTrace(); /添加信息时,调用的方法 public void addSql(String
12、str) String strsql = insert into st1 values(?,?,?,?,?); try pstmt = dbConn.prepareStatement(strsql); for (int i = 0; i str.length; i+) pstmt.setString(i+1, stri); pstmt.executeUpdate(); catch (Exception e) e.printStackTrace(); finally /释放语句对象 连接的对象 allClose(); /删除信息时,用到的方法 public void delete(String
13、str) try pstmt=dbConn.prepareStatement(delete from st1 where sno=?); pstmt.setString(1, str); pstmt.executeUpdate(); catch (Exception e) e.printStackTrace(); finally allClose(); /查询信息时,用到的方法 /public void findSQL(String str) /try /pstmt=dbConn.prepareStatement(select * from st1 where sname like +str+
14、%); /pstmt.executeUpdate(); / catch (Exception e) /e.printStackTrace(); /finally /allClose(); / / /public void findSQL1(String str) /try /pstmt=dbConn.prepareStatement(select * from st1 where sno = +str+ ); /pstmt.executeUpdate(); / catch (Exception e) /e.printStackTrace(); /finally /allClose(); / /
15、 /修改信息时,用到的方法 public void allClose() try if(pstmt!=null) pstmt.close(); if(dbConn!=null) dbConn.close(); catch (Exception ex) ex.printStackTrace(); public void updateIt(String str) String strsql = update st1 set sname=? ,ssex=?,birthday=?,speciality=? where sno=?; try pstmt = dbConn.prepareStatement
16、(strsql); for (int i = 0; i str.length; i+) pstmt.setString(i+1, stri); pstmt.executeUpdate(); catch (Exception e) e.printStackTrace(); finally /释放语句对象 连接的对象 allClose(); Model 类类 package chapter12; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.
17、util.ArrayList; import java.util.List; import javax.swing.table.AbstractTableModel; public class model extends AbstractTableModel List rowData, columnNames; PreparedStatement ps = null; Connection ct = null; ResultSet rs = null; String sql ; public void way(String sql) if (sql.equals() sql = select
18、* from st1; columnNames = new ArrayList(); columnNames.add(学号); columnNames.add(姓名); columnNames.add(性别); columnNames.add(生日); columnNames.add(专业); rowData = new ArrayList(); connectiondb c = new connectiondb(); try ct = c.getdbConn(); ps = ct.prepareStatement(sql); rs = ps.executeQuery(); while (rs
19、.next() / rowData 可以存放多行 String s = rs.toString(); List hang = new ArrayList(); 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 (Exception e) / TODO Auto-generated catch block e.p
20、rintStackTrace(); / rowData = new ArrayList(); / rowData.add(columnNames); public model() this.way(); public model(String str) this.way(str); Override public int getColumnCount() / TODO Auto-generated method stub return this.columnNames.size(); Override public int getRowCount() / TODO Auto-generated
21、 method stub return this.rowData.size(); Override public Object getValueAt(int rowIndex, int columnIndex) / TODO Auto-generated method stub return (ArrayList) this.rowData.get(rowIndex).get(columnIndex); Override public String getColumnName(int arg0) / TODO Auto-generated method stub return (String)
22、 this.columnNames.get(arg0); Update 类类 package chapter12; import java.awt.Frame; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import j
23、avax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class update extends JDialog implements ActionListener JLabel jl1,jl2,jl3,jl4,jl5; JTextField jtf1,jtf2,jtf3,jtf4,jtf5; JButton jb1,jb2; JPanel jp1,jp2,jp3; public update(Frame owner,String title,boolean modal,m
24、odel m,int rowNum) super(owner,title,modal); jp1 = new JPanel(); jl1 = new JLabel(学号); jl2 = new JLabel(姓名); jl3 = new JLabel(性别); jl4 = new JLabel(出生年月); jl5 = new JLabel(专业); jp1.setLayout(new GridLayout(5,1); jp1.add(jl1); jp1.add(jl2); jp1.add(jl3); jp1.add(jl4); jp1.add(jl5); jp2 = new JPanel()
25、; jtf1 = new JTextField(9); jtf1.setEditable(false); jtf2 = new JTextField(8); jtf3 = new JTextField(2); jtf4 = new JTextField(8); jtf5 = new JTextField(10); jtf1.setText(String)m.getValueAt(rowNum, 0); jtf2.setText(String)m.getValueAt(rowNum, 1); jtf3.setText(String)m.getValueAt(rowNum, 2); jtf4.se
26、tText(String)m.getValueAt(rowNum, 3); jtf5.setText(String)m.getValueAt(rowNum, 4); jp2.setLayout(new GridLayout(5,1); jp2.add(jtf1); jp2.add(jtf2); jp2.add(jtf3); jp2.add(jtf4); jp2.add(jtf5); jp3 = new JPanel(); jb1 = new JButton(修改); jb2 = new JButton(取消); jb1.addActionListener(this); jb2.addActio
27、nListener(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_CLO SE); public void actionPerformed(ActionEvent e) if(e.getSource().e
28、quals(jb1) String str = new String5; str4 = jtf1.getText(); str0 = jtf2.getText(); str1 = jtf3.getText(); str2 = jtf4.getText(); str3 = jtf5.getText(); connectiondb c = new connectiondb(); c.updateIt(str); JOptionPane.showMessageDialog(this, 修改成功! ); this.dispose();/关闭对话框 else if(e.getSource().equal
29、s(jb2) this.dispose(); Zhujiemian 类类 package chapter12; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JL
30、abel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; public class zhujiemian extends JFrame implements ActionListener /定义组件 JLabel jl1,jl2; JTextField jtf1,jtf2 ; JPanel jp,jp2; JTable jt; JButton j
31、b1,jb2,jb3,jb4; JScrollPane jsp; model m; /主函数 public static void main(String args) zhujiemian z = new zhujiemian(); /定义构造器 zhujiemian() super(主界面); jp2 = new JPanel(); jtf1 = new JTextField(9); jtf2 = new JTextField(8); jl1 = new JLabel( 学号); jl2 = new JLabel( 姓名); jb4=new JButton(查询); jb4.addActio
32、nListener(this); GridBagLayout gb = new GridBagLayout(); jp2.setLayout(gb); GridBagConstraints c = new GridBagConstraints(); c.fill =new GridBagConstraints().BOTH; c.weightx = 1.0; gb.setConstraints(jl1, c); jp2.add(jl1); c.weightx = 1.0; gb.setConstraints(jtf1, c); jp2.add(jtf1); c.gridwidth = Grid
33、BagConstraints.REMAINDER; / c.gridheight = 2; c.gridheight = GridBagConstraints.VERTICAL; gb.setConstraints(jb4, c); jp2.add(jb4); c.gridx = 0; c.gridy = 1; gb.setConstraints(jl2, c); jp2.add(jl2); c.gridx = 1; c.gridy = 1; c.gridwidth= 1; gb.setConstraints(jtf2, c); jp2.add(jtf2); jp = new JPanel()
34、; /定义按钮 jb1=new JButton(添加); jb2=new JButton(修改); jb3=new JButton(删除); /为按钮添加响应机制 jb1.addActionListener(this); jb2.addActionListener(this); jb3.addActionListener(this); /为 JPanel 添加组件 jp.add(jb1); jp.add(jb2); jp.add(jb3); /jp.add(jb4); m = new model(); jt = new JTable(m); jsp = new JScrollPane(jt); this.add(jsp); this.add(jp2,North); this.add(jp, South); /定义窗口属性 this.setSize(500,300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(t
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 肿瘤中医社区建设与发展
- 传染病预防与食品安全管理
- 贫血护理要点与实施策略
- 读爱和自由心得体会模版
- 【维卓】2023泰国社交媒体电商市场洞察报告342mb
- 2025年数学系大学生实习总结模版
- 2025年B站OTT大屏营销通案
- 山东省济宁市兖州区2024-2025学年高二下学期期中质量检测地理试卷(含答案)
- 新质生产力体现
- 忽略症的临床护理
- 海门村干部考试试题及答案
- (二模)2025年5月济南市高三高考针对性训练生物试卷(含答案)
- 人类面临的主要环境问题第一课时课件高一下学期地理湘教版(2019)必修二
- 2024年云南省会泽县事业单位公开招聘医疗卫生岗笔试题带答案
- 全球及中国双特异性抗体治疗行业市场发展分析及前景趋势与投资发展研究报告2025-2028版
- 2025年电工操作资格证考试复习考试题库(共583题)(含答案)
- 初中地理澳大利亚 课件-2024-2025学年七年级地理下学期(人教版2024)
- 2025-2030中国射击器材行业市场发展趋势与前景展望战略研究报告
- 2025-2030中国采耳行业市场深度调研及竞争格局与投资前景研究报告
- logo保密合同协议
- 2025春新版四年级下册语文 【期末复习:文言文专项考查】
评论
0/150
提交评论