1、java学生管理系统Java学生管理系统能够题目及简介在例12-3的基础上完善程序,做图形界面,在其中有多个选项“添加”、“修改”、“删除”、“查询”等,根据用户选择的功能再输入相应的数据并完成相关功能。开发环境概述java开发工具eclipse,SQL server 2005;一:描述 1.主界面上直接显示数据库中的所有数据,有添加、删除、修改、查询各个按钮2.查询在主界面上实现。根据学号和姓名查询。没有新建类,代码在主界面类里。3.修改和添加都新建了一个类(update和add),生成一个新的窗口,以实现功能。删除的代码比较简单,也在主界面类里。4.专门新建了一个连接类connection
2、db,以实现与数据库的连接。里面还包含了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;import javax.swin
3、g.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; JButton j
4、b1,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 = new JLabel(性别); jl4 = new JLabel(出生年月); jl5 = new JLabel(专业); jp1.add(jl1); jp1.add(jl2
5、); 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.addItem(男); jc3.addItem(女); jc5 = new JComboBox(); jc5.addItem(计算); jc5.addItem(信管); jc5
6、.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(取消); jp3.add(jb1); jp3.add(jb2); /注册监听 jb1.addActionListener(this); jb2.addActionListener(thi
7、s); 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); /响应函数 public void actionPerformed(ActionEvent e) if(e.getS
8、ource().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(); connectiondb c = new connectiondb(); c.addSql(str); JOptionPane.showMessageDialog(this, 添
9、加成功!); 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 java.sql.PreparedStatement;import java.sql.SQLException;public class connectiondb String driverNam
10、e = 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 = sa; String userPwd = 123456; /返回连接数据 public Connection getdbConn() return dbConn; public connection
11、db() try Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL,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(str
12、sql); 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 str) try pstmt=dbConn.prepareStatement(delete from st1 where sno=?); pstmt.setString(1, str); p
13、stmt.executeUpdate(); catch (Exception e) e.printStackTrace(); finally allClose(); /查询信息时,用到的方法/ 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();/
14、 / / public void findSQL1(String str)/ try / pstmt=dbConn.prepareStatement(select * from st1 where sno = +str+ );/ pstmt.executeUpdate();/ catch (Exception e) / e.printStackTrace();/ finally/ allClose();/ / /修改信息时,用到的方法 public void allClose() try if(pstmt!=null) pstmt.close(); if(dbConn!=null) dbCon
15、n.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(strsql); for (int i = 0; i str.length; i+) pstmt.setString(i+1, stri); pstmt.executeUpdate();
16、 catch (Exception e) e.printStackTrace(); finally /释放语句对象 连接的对象 allClose(); Model类package chapter12;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;import javax.swing.table.AbstractTableModel;public class model e
17、xtends 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 * from st1; columnNames = new ArrayList(); columnNames.add(学号); columnNames.add(姓名); columnNames.add(性别)
18、; columnNames.add(生日); columnNames.add(专业); rowData = new ArrayList(); connectiondb c = new connectiondb(); try ct = c.getdbConn(); ps = ct.prepareStatement(sql); rs = ps.executeQuery(); while (rs.next() / rowData可以存放多行 String s = rs.toString(); List hang = new ArrayList(); hang.add(rs.getString(1);
19、 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.printStackTrace(); / rowData = new ArrayList(); / rowData.add(columnNames); public model() this.way(); publ
20、ic 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 method stub return this.rowData.size(); Override public Object getValueAt(int rowIndex, int columnIndex)
21、/ 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) this.columnNames.get(arg0); Update类package chapter12;import java.awt.Frame;import java.awt.GridLayout;imp
22、ort 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 javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;public class update extends JDialog imp
23、lements 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,model m,int rowNum) super(owner,title,modal); jp1 = new JPanel(); jl1 = new JLabel(学号); jl2 = new JLabel(姓名); jl3 = new JL
24、abel(性别); 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 JTextField(9); jtf1.setEditable(false); jtf2 = new JTextField(8); jtf3 = new JTextField(2); jtf4 = new JText
25、Field(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(rowNum, 3); jtf5.setText(String)m.getValueAt(rowNum, 4); jp2.setLayout(new GridLayout(5,1); jp2
26、.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(jb2); this.add(jp1,Center); this.add(jp2,East); this.add(jp3,South); this.setSize(
27、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.getText(); str0 = jtf2.getText(); str1 = jtf3.getText(); str2 = jtf4.getT
28、ext(); 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 java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.GridLayout;import java.awt.
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1