1、Java课程设计Java 课 程 设 计指导老师:焦家林学 院:计算机姓 名:王若旭学 号:101714119目 录1. 题目简介2. 程序概要.3. 程序函数简介.4. 源代码5. 运行截图.6. 心得体会.1题目简介题目简介:要求基于数据库管理系统,建立一个学生信息管理系统,有简单的显示、更新、插入等基本功能。还有程序需有窗口,也就是运行界面中要含有窗口。程序尽量简单,但功能得全面,界面做到尽善尽美!2.程序概要程序概要:本程序中含有一个public类,三个一般类。其主函数在public类中,一般类中每个类实现一个功能。在主函数中调用一般类中的函数,实现不同的功能。每个一般类也是建立在窗口
2、上的,其中运用了窗口,组件,按钮,布局等多种知识。3.程序函数简介class ShowRecord extends JDialog implements ActionListener/负责显示记录的类class ModifyRecord extends JDialog implements ActionListener/负责更新记录的类class DeleteRecord extends JDialog implements ActionListener /负责删除记录的类class InsertRecord extends JDialog implements ActionListener
3、/负责插入记录的类public class Myku extends JFrame implements ActionListener主界面函数4.源代码import java.sql.*;import javax.swing.*;import java.awt.*;import java.awt.event.*;import javax.swing.border.*; class ShowRecord extends JDialog implements ActionListener/负责显示记录的类 JTable table; Object a; Object name=Name,Numb
4、er,Age,Sex,Degree; JButton showRecord; Connection con; Statement sql; ResultSet rs; ShowRecord(String title) setTitle(title); showRecord=new JButton(显示记录); showRecord.addActionListener(this); add(showRecord,BorderLayout.NORTH); setBounds(200,60,400,250); public void actionPerformed(ActionEvent e) tr
5、y con=DriverManager.getConnection(jdbc:odbc:hh,); sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); rs=sql.executeQuery(SELECT * FROM StudentInformation); rs.last(); int lastNumber=rs.getRow(); a=new ObjectlastNumber5; int k=0; rs.beforeFirst(); while(rs.next() ak0
6、=rs.getString(1); ak1=rs.getLong(2); ak2=rs.getLong(3); ak3=rs.getString(4); ak4=rs.getLong(5); k+; con.close(); catch(SQLException ee) System.out.println(ee); table=new JTable(a,name); getContentPane().removeAll(); add(showRecord,BorderLayout.NORTH); add(new JScrollPane(table),BorderLayout.CENTER);
7、 validate(); class ModifyRecord extends JDialog implements ActionListener /负责更新记录的类 JLabel hintLabel,hintLabel1; JTextField inputName; JTextField inputNumber,inputAge,inputSex,inputDegree; Object name=Number,Age,Sex,Degree; Object a=new Object14; JTable table; JButton enterModify; Connection con; St
8、atement sql; ResultSet rs; String name3, number,age,sex,degree; ModifyRecord(String s) setTitle(s); hintLabel=new JLabel(输入姓名(回车确认):); hintLabel1=new JLabel(输 入:); inputName=new JTextField(5); inputAge=new JTextField(5); inputNumber=new JTextField(5); inputDegree=new JTextField(5); inputSex=new JTex
9、tField(5); table=new JTable(a,name); enterModify=new JButton(更新记录); setLayout(null); Box baseBox=Box.createHorizontalBox(); Box box=Box.createHorizontalBox(); baseBox.add(hintLabel); baseBox.add(inputName); baseBox.add(new JScrollPane(table); box.add(hintLabel1);box.add(Box.createHorizontalStrut(100
10、); box.add(inputNumber); box.add(inputAge); box.add(inputSex); box.add(inputDegree); box.add(enterModify); add(baseBox,BorderLayout.NORTH); add(box,BorderLayout.CENTER); baseBox.setBounds(10,40,520,38); box.setBounds(10,100,580,38); inputName.addActionListener(this); enterModify.addActionListener(th
11、is); setBounds(20,60,700,200); System.out.println(gengxinjilu); public void actionPerformed(ActionEvent e) name3=inputName.getText().trim(); number=inputNumber.getText().trim(); age=inputAge.getText().trim(); sex=inputSex.getText().trim(); degree=inputDegree.getText().trim(); if(e.getSource()=inputN
12、ame) try con=DriverManager.getConnection(jdbc:odbc:hh,); sql=con.createStatement(); rs=sql.executeQuery(SELECT * FROM StudentInformation WHERE Name=+name3+); boolean boo=rs.next(); if(boo=false) JOptionPane.showMessageDialog (this,姓名不存在,提示,JOptionPane.WARNING_MESSAGE); else a00=rs.getLong(2); a01=rs
13、.getLong(3); a02=rs.getString(4); a03=rs.getLong(5); table.repaint(); con.close(); catch(SQLException ee) System.out.println(ee); if(e.getSource()=enterModify) try con=DriverManager.getConnection(jdbc:odbc:hh,); sql=con.createStatement(); sql.executeUpdate(UPDATE StudentInformation SET StudentInform
14、ation.Number=(+number+) WHERE Name=+name3+); sql.executeUpdate(UPDATE StudentInformation SET StudentInformation.Age=(+age+) WHERE Name=+name3+); sql.executeUpdate(UPDATE StudentInformation SET StudentInformation.Sex=(+sex+) WHERE Name=+name3+); sql.executeUpdate(UPDATE StudentInformation SET Student
15、Information.Degree=(+degree+) WHERE Name=+name3+); System.out.println(成功); JOptionPane.showMessageDialog(this,更新成功,成功,JOptionPane.PLAIN_MESSAGE); con.close(); catch(SQLException ea) JOptionPane.showMessageDialog (this,更新失败+ea,失败,JOptionPane.ERROR_MESSAGE); class InsertRecord extends JDialog implemen
16、ts ActionListener /负责插入记录的类 JLabel hintLabel; Object name=Name,Number,Age,Sex,Degree,; Object a=new Object15; JTable table; JButton enterInsert; Connection con; Statement sql; ResultSet rs; InsertRecord(String s) setTitle(s); hintLabel=new JLabel(输入新记录:); table=new JTable(a,name); enterInsert=new JB
17、utton(插入新记录); setLayout(null); Box baseBox=Box.createHorizontalBox(); baseBox.add(hintLabel); baseBox.add(new JScrollPane(table); baseBox.add(as); baseBox.add(enterInsert); add(baseBox); baseBox.setBounds(10,40,600,38); enterInsert.addActionListener(this); setBounds(120,160,700,200); public void act
18、ionPerformed(ActionEvent e) if(e.getSource()=enterInsert) table.requestFocus(); try con=DriverManager.getConnection(jdbc:odbc:hh,); sql=con.createStatement(); int k=sql.executeUpdate(INSERT INTO StudentInformation VALUES(+a00+,+a01+,+a02+,+a03+,+a04+); System.out.println(a04); if(k=1) JOptionPane.sh
19、owMessageDialog (this,插入记录成功,成功,JOptionPane.PLAIN_MESSAGE); con.close(); catch(SQLException ee) JOptionPane.showMessageDialog (this,插入记录失败+ee,失败,JOptionPane.ERROR_MESSAGE); class DeleteRecord extends JDialog implements ActionListener /负责删除记录的类 JLabel hintLabel; JTextField inputNumber; Object name=Nu
20、mber,Age,Sex,Degree; Object a=new Object14; JTable table; JButton delete; Connection con; Statement sql; ResultSet rs; String name2; DeleteRecord(String s) setTitle(s); hintLabel=new JLabel(删除记录); inputNumber=new JTextField(20); table=new JTable(a,name); delete=new JButton(删除); setLayout(null); Box
21、baseBox=Box.createHorizontalBox(); baseBox.add(hintLabel); baseBox.add(inputNumber); baseBox.add(new JScrollPane(table); baseBox.add(delete); add(baseBox); baseBox.setBounds(10,40,600,38); inputNumber.addActionListener(this); delete.addActionListener(this); setBounds(20,60,700,200); public void acti
22、onPerformed(ActionEvent e) if(e.getSource()=inputNumber) try name2=inputNumber.getText().trim(); con=DriverManager.getConnection(jdbc:odbc:hh,); sql=con.createStatement(); rs=sql.executeQuery(SELECT * FROM StudentInformation WHERE Name=+name2+); boolean boo=rs.next(); if(boo=false) JOptionPane.showM
23、essageDialog (this,姓名不存在,提示,JOptionPane.WARNING_MESSAGE); else a00=String.valueOf(rs.getLong(2); a01=String.valueOf(rs.getLong(3); a02=rs.getString(4); a03=String.valueOf(rs.getLong(5); table.repaint(); con.close(); catch(SQLException ee) System.out.println(ee); if(e.getSource()=delete) try con=Driv
24、erManager.getConnection(jdbc:odbc:hh,); sql=con.createStatement(); sql.executeUpdate(DELETE FROM StudentInformation WHERE Name=+name2+); JOptionPane.showMessageDialog (this,删除成功,成功,JOptionPane.PLAIN_MESSAGE); con.close(); catch(SQLException ee) JOptionPane.showMessageDialog (this,删除失败+ee,失败,JOptionP
25、ane.ERROR_MESSAGE); public class xinde extends JFrame implements ActionListener JButton Insert,Delete,Update,Show; JPanel ps; JTable table; Object a=new Object105; Object name=Name,Number,Age,Sex,Degree; Connection con; Statement sql; ResultSet rs; xinde() setTitle(数据库管理); Insert=new JButton(插入); De
26、lete=new JButton(删除); Update=new JButton(更新); Show=new JButton(刷新); Insert.addActionListener(this); Delete.addActionListener(this); Update.addActionListener(this); Show.addActionListener(this); ps=new JPanel(); ps.add(Show); ps.add(Insert); ps.add(Delete); ps.add(Update); add(ps,BorderLayout.SOUTH);
27、 tryClass.forName(sun.jdbc.odbc.JdbcOdbcDriver); catch(Exception ce) trycon=DriverManager.getConnection(jdbc:odbc:hh,); sql=con.createStatement(); rs=sql.executeQuery(SELECT * FROM StudentInformation order by Name); int k=0; while(rs.next() ak0=rs.getString(1); ak1=rs.getLong(2); ak2=rs.getLong(3);
28、ak3=rs.getString(4); ak4=rs.getLong(5); k+; con.close(); catch(Exception xe) table=new JTable(a,name); add(new JScrollPane(table),BorderLayout.CENTER); validate(); setBounds(500,250,600,250); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); public void actionPerformed(ActionEvent ae) if(ae.getSource()=Insert) InsertRecord INsert=new InsertRecord(插入数据); INsert.setVisible(true); if(ae.getSo
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1