1、Model_1 学生管理系统-余暑廷1Model_1 学生管理系统做好后记得引包:msbase.jarMssqlserver.jarMsutil.jar否则将连接数据库失败。StModel.javapackage model_1;/StModel文件源代码:import java.util.*;import java.sql.*;import java.awt.*;import javax.swing.*;import java.awt.event.*;import javax.swing.table.*;import javax.swing.event.*;public class StMo
2、del extends AbstractTableModelVector columNames = null; /表的字段名称Vector rowDate = null; /表的数据 /数据库private Connection con = null;private PreparedStatement ps = null;private ResultSet rs = null;private static final String driver=“com.microsoft.jdbc.sqlserver.SQLServerDriver“;private static final String
3、url=“jdbc:microsoft:sqlserver:/127.0.0.1:1433;databaseName=jas“;private static final String user=“sa“;private static final String passWord=“; /对数据库进行查找public void insertDatabase(String sql)columNames = new Vector(); /表的字段名称rowDate = new Vector(); /表的数据 columNames.add(“学号“);columNames.add(“名字“);colum
4、Names.add(“性别“);columNames.add(“年龄“);columNames.add(“籍贯“);Model_1 学生管理系统-余暑廷2columNames.add(“系别“);try/加载jdbc 驱动Class.forName(driver);/连接数据源con = DriverManager.getConnection(url,user,passWord);/执行sqlps = con.prepareStatement(sql);rs = ps.executeQuery();while(rs.next()Vector row = new Vector();row.add
5、(rs.getString(1);row.add(rs.getString(2);row.add(rs.getString(3);row.add(rs.getString(4);row.add(rs.getString(5);row.add(rs.getString(6);rowDate.add(row); /将学生的一条记录(row)添加到rowDate中catch(ClassNotFoundException e)e.printStackTrace();catch(SQLException e)e.printStackTrace();finally/关闭数据库资源tryif(null !=
6、 rs)rs.close();if(null != ps)ps.close();Model_1 学生管理系统-余暑廷3if(null != con)con.close();catch(SQLException e)e.printStackTrace();Override/返回列数public int getColumnCount() / TODO Auto-generated method stubreturn this.columNames.size();Override/返回表的行数public int getRowCount() / TODO Auto-generated method
7、stubreturn this.rowDate.size();Override/得到某行某列的数据public Object getValueAt(int rowIndex, int columnIndex) / TODO Auto-generated method stubreturn (Vector)this.rowDate.get(rowIndex).get(columnIndex);/设置表的字段名public String getColumnName(int column)return (String)this.columNames.get(column);Model_1 学生管理系
8、统-余暑廷4StuMange.javapackage model_1;/model1模式开发:/StuMange文件源代码:import java.util.*;import java.sql.*;import java.awt.*;import javax.swing.*;import java.awt.event.*;import javax.swing.event.*;public class StuMange extends JFrame implements ActionListener /定义用户界面用到的组件JLabel userName = null; /要查询的学生的名字JT
9、extField textField = null; /用户输入的文本框JButton select = null; /查询按钮JButton insert = null; /添加按钮JButton update = null; /修改按钮JButton delete = null; /删除按钮JTable table = null; /用于显示数据库调出的数据的表Panel panel1 = null; Panel panel2 = null;Panel panel3 = null;JScrollPane jscrollPane = null;StModel stModel = null;
10、/st表的模型private Object PreparedStatement;public static void main(String args) StuMange window = new StuMange();public StuMange()/窗口显示的上部的组件userName = new JLabel(“学生名:“);textField = new JTextField(10); /用户输入文本框限定为最多输入 10个字符select = new JButton(“查询“ ); /查询按钮/添加到第一个面板中 panel1 = new Panel();Model_1 学生管理系
11、统-余暑廷5panel1.add(userName);panel1.add(textField);panel1.add(select);/窗口下部的组件insert = new JButton(“添加“ );delete = new JButton(“删除“ );update = new JButton(“修改“ );/添加到第三个个面板中panel3 = new Panel();panel3.add(insert);panel3.add(delete);panel3.add(update);/窗口中部的组件stModel = new StModel();stModel.insertDatab
12、ase(“select * from stud3“);table = new JTable(stModel);jscrollPane = new JScrollPane(table);/注册事件监听select.addActionListener(this);insert.addActionListener(this);update.addActionListener(this);delete.addActionListener(this);/将各个组件添加到窗体中this.add(panel1, BorderLayout.NORTH);this.add(jscrollPane, Border
13、Layout.CENTER);this.add(panel3, BorderLayout.SOUTH);/设置窗体属性this.setSize(400, 300); /设置窗口大小this.setLocation(200, 200); /设置窗口显示的位置this.setTitle(“mini学生信息管理系统“); /设置窗口标题this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /设置窗口关闭之后其后台程序同时关闭/this.setResizable(false); /设置窗口不可拉伸/设置窗口为可见this.setVisible(tru
14、e);Model_1 学生管理系统-余暑廷6public void actionPerformed(ActionEvent e)/用户点击了查找按钮if(e.getSource() = select)String paras = textField.getText().trim();stModel = new StModel();stModel.insertDatabase(“select * from stud3 where stuName=“+paras+“);table.setModel(stModel);/用户点击了添加按钮else if(e.getSource() = insert)
15、InsertStudent insertStudent = new InsertStudent(this, “添加学生“, true);/刷新窗口表的数据stModel = new StModel();stModel.insertDatabase(“select * from stud3“);table.setModel(stModel);/用户点击了修改按钮else if(e.getSource() = update)int row = this.table.getSelectedRow(); /用户选择的行数if(-1 = row) /如果用户没有选中行JOptionPane.showMe
16、ssageDialog(this, “请选择一行“ );elseUpdateDatabase updateDatabase = new UpdateDatabase(this, “修改学生“, true, stModel, row);/刷新窗口表的数据stModel =new StModel();stModel.insertDatabase(“select * from stud3“);table.setModel(stModel);/用户点击了删除按钮else if(e.getSource() = delete)/数据库Model_1 学生管理系统-余暑廷7Connection con =
17、null;PreparedStatement ps = null;String driver = “com.microsoft.jdbc.sqlserver.SQLServerDriver“; /jdbc驱动String url = “jdbc:microsoft:sqlserver:/127.0.0.1:1433;databaseName=jas“; /数据源String user = “sa“; /数据库用户密码String passWord = “y198912306263327“; /数据库用户sa的密码int row = this.table.getSelectedRow(); /用
18、户选择的行if(-1 = row) /用户没有选中行JOptionPane.showMessageDialog(this, “请选择一行“ );elsetry/加载jdbc驱动Class.forName(driver);/连接数据源con = DriverManager.getConnection(url, user, passWord);/执行sqlString sql = “delete from stud3 where stuId=?“;String id = (String)(stModel.getValueAt(row, 0); /获得用户选择的行的学生的学号ps = con.pre
19、pareStatement(sql);ps.setString(1, id);ps.executeUpdate();/刷新窗口stModel = new StModel();stModel.insertDatabase(“select * from stud3“);table.setModel(stModel);catch(ClassNotFoundException e1)e1.printStackTrace();catch(SQLException e1)e1.printStackTrace();Model_1 学生管理系统-余暑廷8finally/关闭资源tryif(null != ps
20、)ps.close();if(null != con)con.close();catch(SQLException e1)e1.printStackTrace();Model_1 学生管理系统-余暑廷9InsertStudent.javapackage model_1;/InsertStudent文件源代码:import java.sql.*;import java.awt.*;import javax.swing.*;import java.awt.event.*;import javax.swing.table.*;import javax.swing.event.*;/添加学生的对话框类
21、public class InsertStudent extends JDialog implements ActionListenerprivate JLabel id, name, sex, age, address, dep; /分别代表学生的学号,名字,性别,年龄,籍贯,系别private JTextField jtf1, jtf2, jtf3, jtf4, jtf5, jtf6; /用户输入的文本框,分别为学生的学号,名字,性别,年龄,籍贯,系别private JButton add, canel; /分别为添加按钮和取消按钮private Panel panel1, panel2,
22、 panel3;public InsertStudent(Frame owner, String title, boolean model)super(owner, title, model); /实现与父类窗口对话System.out.println(“我执行了“);/窗体的西部的组件id = new JLabel(“学号:“);name = new JLabel(“姓名:“);sex = new JLabel(“性别“);age = new JLabel(“年龄“);address = new JLabel(“籍贯“ );dep = new JLabel(“系别:“);/添加到panel1
23、面板中panel1 = new Panel(new GridLayout(6, 1);panel1.add(id);panel1.add(name);panel1.add(sex);panel1.add(age);panel1.add(address);panel1.add(dep);/窗体的东部组件jtf1 = new JTextField(10);Model_1 学生管理系统-余暑廷10jtf2 = new JTextField(10);jtf3 = new JTextField(10);jtf4 = new JTextField(10);jtf5 = new JTextField(10)
24、;jtf6 = new JTextField(10);/添加到第二个面板中panel2 = new Panel(new GridLayout(6, 1);panel2.add(jtf1);panel2.add(jtf2);panel2.add(jtf3);panel2.add(jtf4);panel2.add(jtf5);panel2.add(jtf6);/窗体北部的控件 add = new JButton(“添加“);canel = new JButton(“取消“);/注册事件监听add.addActionListener(this);canel.addActionListener(thi
25、s);/添加到panel3面板中panel3 = new Panel();panel3.add(add);panel3.add(canel);/将各个组件添加到窗体中this.add(panel1, BorderLayout.WEST);this.add(panel2, BorderLayout.CENTER);this.add(panel3, BorderLayout.SOUTH);/设置对话框属性this.setSize(300, 200);/设置对话框为可见this.setVisible(true);public void actionPerformed(ActionEvent e)/数据库Connection con = null;PreparedStatement ps = null;
Copyright © 2018-2021 Wenke99.com All rights reserved
工信部备案号:浙ICP备20026746号-2
公安局备案号:浙公网安备33038302330469号
本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。