《java学生管理系统(共32页).doc》由会员分享,可在线阅读,更多相关《java学生管理系统(共32页).doc(32页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上Java学生管理系统能够题目及简介在例12-3的基础上完善程序,做图形界面,在其中有多个选项“添加”、“修改”、“删除”、“查询”等,根据用户选择的功能再输入相应的数据并完成相关功能。开发环境概述JAVA开发工具ECLIPSE,SQL SERVER 2005;一:描述 1. 主界面上直接显示数据库中的所有数据,有添加、删除、修改、查询各个按钮2. 查询在主界面上实现。根据学号和姓名查询。没有新建类,代码在主界面类里。3. 修改和添加都新建了一个类(update和add),生成一个新的窗口,以实现功能。删除的代码比较简单,也在主界面类里。4. 专门新建了一个连接类con
2、nectiondb,以实现与数据库的连接。里面还包含了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;import java.awt.event.ActionListener;import javax.swing.JButton;impo
3、rt 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 implements ActionListener/定义组件JPanel jp1,jp2,jp3;JLabel jl1,jl2,jl3,jl4,jl5;
4、JButton jb1,jb2;JTextField jtf1,jtf2,jtf4;JComboBox jc3,jc5;/构造器public add(Frame owner,String title,boolean modal)super(owner,title,modal);/定义jp1jp1 = new JPanel();jl1 = new JLabel(学号);jl2 = new JLabel(姓名);jl3 = new JLabel(性别);jl4 = new JLabel(出生年月);jl5 = new JLabel(专业);jp1.add(jl1);jp1.add(jl2);jp1
5、.add(jl3);jp1.add(jl4);jp1.add(jl5);jp1.setLayout(new GridLayout(5,1);/定义jp2jp2 = new JPanel();jtf1 = new JTextField(9);jtf2 = new JTextField(8);jtf4 = new JTextField(8);jc3 = new JComboBox();jc3.addItem(男);jc3.addItem(女);jc5 = new JComboBox();jc5.addItem(计算);jc5.addItem(信管);jc5.addItem(地信);jc5.addI
6、tem(日语);jp2.add(jtf1);jp2.add(jtf2);jp2.add(jc3);jp2.add(jtf4);jp2.add(jc5);jp2.setLayout(new GridLayout(5,1);/定义jp3jp3 = new JPanel();jb1 = new JButton(确定);jb2 = new JButton(取消);jp3.add(jb1);jp3.add(jb2);/注册监听jb1.addActionListener(this);jb2.addActionListener(this);this.add(jp1,BorderLayout.WEST);th
7、is.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);/响应函数public void actionPerformed(ActionEvent e) if(e.getSource().equals(jb1)String str = new String5;str
8、0 = jtf1.getText();str1 = jtf2.getText();str2 = jc3.getSelectedItem().toString().trim();str3 = jtf4.getText();str4 = jc5.getSelectedItem().toString().trim();connectiondb c = new connectiondb();c.addSql(str);JOptionPane.showMessageDialog(this, 添加成功!); this.dispose();/关闭对话框else if(e.getSource().equals
9、(jb2)this.dispose();Connectiondb类package chapter12;import java.beans.Statement;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;public class connectiondbString driverName = com.microsoft.sqlserver.jdbc.SQLServerDriver;String dbUR
10、L = jdbc:sqlserver:/127.0.0.1:1433;DatabaseName=student;Connection dbConn;Statement stmt;PreparedStatement pstmt=null;String userName = sa;String userPwd = ;/返回连接数据public Connection getdbConn()return dbConn;public connectiondb()try Class.forName(driverName); dbConn = DriverManager.getConnection(dbUR
11、L,userName,userPwd); catch (Exception e) System.out.println(连接失败!);e.printStackTrace();/添加信息时,调用的方法public void addSql(String 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.executeUpd
12、ate(); catch (Exception e) e.printStackTrace();finally/释放语句对象 连接的对象allClose();/删除信息时,用到的方法public void delete(String str)try pstmt=dbConn.prepareStatement(delete from st1 where sno=?);pstmt.setString(1, str);pstmt.executeUpdate(); catch (Exception e) e.printStackTrace();finallyallClose();/查询信息时,用到的方法
13、/public void findSQL(String str)/try /pstmt=dbConn.prepareStatement(select * from st1 where sname like +str+%);/pstmt.executeUpdate();/ catch (Exception e) /e.printStackTrace();/finally/allClose();/public void findSQL1(String str)/try /pstmt=dbConn.prepareStatement(select * from st1 where sno = +str
14、+ );/pstmt.executeUpdate();/ catch (Exception e) /e.printStackTrace();/finally/allClose();/修改信息时,用到的方法public void allClose()tryif(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
15、=? ,ssex=?,birthday=?,speciality=? where sno=?;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();Model类package chapter12;import java.sql.Connection;imp
16、ort java.sql.PreparedStatement;import java.sql.ResultSet;import java.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 sq
17、l ;public void way(String sql) if (sql.equals() sql = select * 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.prepa
18、reStatement(sql);rs = ps.executeQuery();while (rs.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);/ 加入到rowDatarowData.add(hang); catch (Exception e
19、) / TODO Auto-generated catch blocke.printStackTrace();/ rowData = new ArrayList();/ rowData.add(columnNames);public model()this.way();public model(String str)this.way(str);Overridepublic int getColumnCount() / TODO Auto-generated method stubreturn this.columnNames.size();Overridepublic int getRowCo
20、unt() / TODO Auto-generated method stubreturn this.rowData.size();Overridepublic Object getValueAt(int rowIndex, int columnIndex) / TODO Auto-generated method stubreturn (ArrayList) this.rowData.get(rowIndex).get(columnIndex);Overridepublic String getColumnName(int arg0) / TODO Auto-generated method
21、 stubreturn (String) 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;i
22、mport javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;public class update extends JDialog implements ActionListenerJLabel 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,mo
23、del 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();jtf1 = new JTe
24、xtField(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.setText(String)m.getValueAt
25、(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.addActionListener(this);jp3.add(jb1);jp3.add(
26、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);public void actionPerformed(ActionEvent e) if(e.getSource().equals(jb1)String str = new String5;str4 = jtf1.g
27、etText();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().equals(jb2)this.dispose();Zhujiemian类package chapter12;import ja
28、va.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.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import jav
29、ax.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 jb1,jb2,jb3,jb4;JScrollPane jsp;model m;/主函数public static void main(String args) zhu
30、jiemian 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.addActionListener(this);GridBagLayout gb = new GridBagLayout();jp2.setLayout(gb);GridBagConstraints c = n
31、ew 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 = GridBagConstraints.REMAINDER; / c.gridheight = 2; c.gridheight = GridBagConstraints.VERTICAL; gb.setConstr
32、aints(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();/定义按钮jb1=new JButton(添加);jb2=new JButton(修改);jb3=new JButton(删除);/为按钮添加响应机制jb1.addActionListener(this);j
33、b2.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.EXI
34、T_ON_CLOSE);this.setVisible(true);this.setLocation(450,200); /响应函数public void actionPerformed(ActionEvent e) /判断用户点击哪个按钮/当点击 “添加”时if(e.getSource().equals(jb1)add a = new add(this,添加信息,true);m = new model();jt.setModel(m);/当点击 “修改”时else if(e.getSource().equals(jb2)int rowNum = this.jt.getSelectedRow(
35、);if(rowNum = -1)JOptionPane.showMessageDialog(this, 请选择一行);return;new update(this,修改,true,m,rowNum);m = new model();jt.setModel(m);/当点击 “删除”时else if(e.getSource().equals(jb3)int rowNum = this.jt.getSelectedRow();if(rowNum = -1)JOptionPane.showMessageDialog(this, 请选择一行);return;String sno = (String)
36、m.getValueAt(rowNum, 0);connectiondb c = new connectiondb();c.delete(sno);JOptionPane.showMessageDialog(this, 删除成功!);m = new model();jt.setModel(m);/当点击 “查询”时else if(e.getSource().equals(jb4)String s1 = jtf1.getText();String s2 = jtf2.getText();String s;if(s1.equals()& s2.equals()s = ;JOptionPane.showMessageDialog(this, 请输入要查询的内容!); else if(s2.equals()s = select * from st1 where sno = +s1+;else if(s1.equals()s = select * from st1 where sname like +s2+%;elses = select * from st1 where sno = +s1+and sname like +s2+%;m = new model(s);jt.setModel(m);专心-专注-专业