学生信息管理系统(JAVA)_第1页
学生信息管理系统(JAVA)_第2页
学生信息管理系统(JAVA)_第3页
免费预览已结束,剩余14页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、/* e y?Y?a d ? o 10 ? dd */import java.sql.Connection。import java.sql.DriverManager。import java.sql.ResultSet。import java.sql.SQLException。import java.sql.Statement。public class DB / ?卩?e y?Y?ad ? opublic static Connection getConn(> / x0 2d mysql 卩?jdbc?y?-try Class.forName("com.mysql.jdbc.D

2、river"> catch (ClassNotFoundException e> e.printStackTrace(>。/'' ?"e y?Y?ad ? o ? oString ip = "localhost"。+ dbName 。 /"root",String dbName = "jdbc"。String url = "jdbc:mysql:/"+ ip + "/"'?' | e ?mysql jdbc d ?D- oeC

3、onnection conn = null。try conn = DriverManager.getConnection(url, "1234"> 。 catch (SQLException e> e.printStackTrace(>。return conn 。public static void close(ResultSet rs> if (rs != null> try rs.close(> 。 catch (SQLException e> e.printStackTrace(>。public static void

4、close(Statement pstmt> if (pstmt != null> try pstmt.close(> 。 catch (SQLException e> e.printStackTrace(>。public static void close(Connection conn> if (conn != null> try conn.close(> 。 catch (SQLException e> e.printStackTrace(> public static void rollback(Connection conn

5、> if (conn != null> try conn.rollback(> catch (SQLException e> e.printStackTrace(> /* 删除类 */ import java.sql.Connection 。import java.sql.SQLException 。import java.sql.Statement 。public class Drop public void drop(MainFrame frame, String number, String choice> Connection con = null

6、。 Statement s = null 。con = try DB.getConn(> 。if(number.isEmpty(>>frame.text.append(" 关 键 字 不 能 为 空 ! 删 除 失 败 !nn"> 。return 。s = con.createStatement(> 。String str = "delete from student where " + choice + "='"+ numberI Hill+ "'" 。try s.e

7、xecuteUpdate(str> 。frame.text.append(" 删除完成! nn"> 。 catch (Exception e> / TODO Auto-generated catch block frame.text.append(" 删除失败! nn"> 。 e.printStackTrace(> 。DB.close(s> 。DB.close(con> 。 catch (SQLException e> / TODO Auto-generated catch blocke.printStac

8、kTrace(> 。/* 添加类 */import java.sql.Connection。import java.sql.PreparedStatement。import java.sql.SQLException。public class Insert public void insertData(MainFrame frame, String number, String name,String dept, String grade> / public void insertData(> throws SQLException Connection con = null

9、。PreparedStatement s = null 。int grades = 0 。if (number.isEmpty(> | name.isEmpty(>> frame.text.append(" 学号或姓名不能为空 , 插入失败! "> 。 return 。if(!grade.isEmpty(>>grades = Integer.parseInt(grade>。String str = "insert into student values (?,?,?,?>"。con = DB.getConn

10、(> 。try s = con.prepareStatement(str> 。s.setString(1, number> 。s.setString(2, name>。s.setString(3, dept>。s.setInt(4, grades> 。try s.executeUpdate(> 。frame.text.append(" 插入成功! "> 。 catch (Exception e> / TODO Auto-generated catch block frame.text.append(" 插入失败

11、! "> 。 catch (SQLException e> DB.close(s> 。DB.close(con> 。import java.awt.Button。import java.awt.Choice。import java.awt.Color。import java.awt.FlowLayout。import java.awt.Font。import java.awt.Frame 。import java.awt.Graphics。import java.awt.Image 。import java.awt.Label 。import java.awt

12、.Menu 。import java.awt.MenuBar 。import java.awt.MenuItem 。import java.awt.Panel 。import java.awt.TextArea 。import java.awt.TextField 。import java.awt.Toolkit 。import java.awt.event.ActionEvent 。import java.awt.event.ActionListener 。import java.awt.event.WindowAdapter 。import java.awt.event.WindowEve

13、nt 。public class MainFrame extends Frame implements ActionListener */private static final long serialVersionUID = 1Lprivate MenuItem select, insert, delete, updateprivate TextField rescan, scan, number, name, dept, grade private Panel panel 。private Choice choice。private int flag = 0 。private MenuBa

14、r jmb 。TextField username, password, repassword 。Button submit, zhuce 。TextArea text 。public static void main(String args> / TODO Auto-generated method stubMainFrame frame = new MainFrame(> 。frame.frame(> 。/* 创建主窗口 */ public void frame(> setSize(400, 400> 。 setTitle(" 学生信息管理 &quo

15、t;> 。 setLocation(400, 200> 。/ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE> / pack(> 。this.addWindowListener(new WindowAdapter(> public void windowClosing(WindowEvent e> setVisible(false> 。 System.exit(-1> 。>。setResizable(false> 。 setVisible(true> 。 denglu(> 。pu

16、blic void paint(Graphics g> Image image Toolkit.getDefaultToolkit(>.getImage("src/welcom.jpg">。g.drawImage(image, 0, 20, 400, 400, this>。/* 对窗口的菜单栏标签进行初始化并添加监听 */ public void init(> panel = new Panel(> 。 jmb = new MenuBar(> 。 setMenuBar(jmb> 。 Menu file = new Menu(&

17、quot; 菜单 "> 。 Menu exit = new Menu(" 选项 "> 。 jmb.add(file> 。 jmb.add(exit> 。file.add(select = new MenuItem("查询 ">> 。file.add(insert = new MenuItem("添加 ">> 。file.add(delete = new MenuItem("删除 ">> 。file.add(update = new MenuIte

18、m("修改 ">> 。exit.add(new MenuItem(" 退出登陆 ">> 。 exit.add(new MenuItem(" 返回 ">> 。 exit.addActionListener(this> 。 select.addActionListener(this>。insert.addActionListener(this>。delete.addActionListener(this>。update.addActionListener(this>。/*

19、事件监听 */Overridepublic void actionPerformed(ActionEvent e> / TODO Auto-generated method stubString actionCommand = e.getActionCommand(> 。boolean flags = false。if (" 登陆 ".equals(actionCommand>> flags = new User(>.isRight(this, username.getText(>.trim(>, password.getText(

20、>.trim(>> 。if (flags> init(> 。 this.removeAll(> 。if (" 注册 ".equals(actionCommand>> addUser(> 。if (" 提交 ".equals(actionCommand>> flags = false 。flags = this.isEmpty(username.getText(>.trim(>,password.getText(>.trim(>, repassword.getText

21、(>.trim(>>if (flags> flags = false 。newflags =User(>.addUser(this,username.getText(>.trim(>, password.getText(>.trim(>> 。if(flags> this.removeAll(> 。 denglu(> 。if (" 重置 ".equals(actionCommand>> username.setText(""> 。password.setText

22、(""> 。repassword.setText(""> 。if (" 退出登陆 ".equals(actionCommand>> this.removeAll(> 。this.remove(jmb> 。denglu(> 。if (" 返回 ".equals(actionCommand>> this.removeAll(> 。 if (" 查询 ".equals(actionCommand>> selects(> 。

23、flag = 1 。 if (" 添加 ".equals(actionCommand>> insert(> 。 flag = 2 。 if (" 删除 ".equals(actionCommand>> delete(> 。 flag = 3 。 if (" 修改 ".equals(actionCommand>> update(> 。 flag = 4 。 if (" 确定 ".equals(actionCommand>> if (flag = 1&g

24、t; text.setText(""> 。new Select(>.select(this, scan.getText(>.trim(>, choice.getSelectedItem(>> 。 else if (flag = 2> text.setText(""> 。 new Insert(>.insertData(this, number.getText(>.trim(>, name.getText(>.trim(>, dept.getText(>.trim(>

25、, grade.getText(>.trim(>> 。 else if (flag = 3> text.setText(""> 。 new Drop(>.drop(this, scan.getText(>.trim(>,choice.getSelectedItem(>> 。Illinew Select(>.select(this, "all"> 。 else text.setText(""> 。scan.getText(>.trim(>, res

26、can.getText(>.trim(>,choice.getSelectedItem(>> 。/* 创建登录窗口 */ public void denglu(> panel = new Panel(> 。 Font font = new Font("宋体 ", 20, 18> 。panel.setBackground(Color.gray> 。 panel.setLayout(null> 。 username = new TextField(20> 。 password = new TextField(20>

27、; 。 Label lusername = new Label("用户名: "> 。Label lpassword = new Label("密 码: "> 。submit = new Button(" 登陆 "> 。 zhuce = new Button(" 注册 "> 。 lusername.setFont(font> 。 username.setFont(font> 。 lpassword.setFont(font> 。 password.setFont(font&g

28、t; 。 lusername.setBounds(90, 70, 60, 50> 。 username.setBounds(180, 80, 120, 25>。lpassword.setBounds(90, 108, 60, 50>。password.setBounds(180, 113, 120, 25> 。 submit.setBounds(180, 150, 50, 30>。zhuce.setBounds(240, 150, 50, 30> 。 panel.add(lusername> 。 panel.add(username> 。 pan

29、el.add(lpassword> 。 panel.add(password> 。 panel.add(submit> 。 panel.add(zhuce> 。 submit.addActionListener(this> 。 zhuce.addActionListener(this> 。 add(panel> 。 init(> 。 this.remove(jmb> 。/* 注册窗口 */ public void addUser(> this.removeAll(> 。 panel = new Panel(> 。 Font

30、 font = new Font(" 宋体 ", 20, 18> 。 panel.setBackground(Color.gray> 。 panel.setLayout(null> 。 username = new TextField(20> 。 password = new TextField(20> 。 repassword = new TextField(20> 。 / username.setFont(> 。Label lusername = new Label(" 用户名: "> 。 Label

31、lpassword = new Label(" 密 码: "> 。 Label rlpassword = new Label(" 重 复: "> 。 submit = new Button(" 提交 "> 。 zhuce = new Button(" 重置 "> 。 lusername.setFont(font> 。 username.setFont(font> 。 lpassword.setFont(font> 。 password.setFont(font> 。

32、rlpassword.setFont(font> 。 repassword.setFont(font> 。 lusername.setBounds(90, 70, 60, 50> 。 username.setBounds(180, 80, 130, 25>。lpassword.setBounds(90, 108, 60, 50>。password.setBounds(180, 120, 130, 25>。rlpassword.setBounds(90, 152, 60, 50>。repassword.setBounds(180, 160, 130, 2

33、5> 。 submit.setBounds(180, 200, 50, 30>。zhuce.setBounds(250, 200, 50, 30> 。 panel.add(lusername> 。 panel.add(username> 。/ username.setLocation(100, 100> 。 panel.add(lpassword> 。 panel.add(password> 。panel.add(rlpassword> 。 panel.add(repassword> 。 panel.add(submit> 。

34、panel.add(zhuce> 。 submit.addActionListener(this> 。 zhuce.addActionListener(this> 。 add(panel> 。init(> 。this.remove(jmb> 。/* 创建查询窗口 */public void selects(> this.removeAll(> 。 panel = new Panel(> 。 panel.add(new Label(" 查找的关键字 :">> 。 panel.setLayout(new Flow

35、Layout(FlowLayout.LEFT>> submit = new Button(" 确定 "> 。 scan = new TextField(30> 。 choice = new Choice(> 。text = new TextArea(10, 50> 。 text.setEditable(false> 。 choice.add("name"> 。 choice.add("number"> 。 choice.add("dept"> 。 choi

36、ce.add("all"> 。 panel.add(scan> 。 panel.add(new Label(" 查找方式 :">> 。 panel.add(choice> 。 panel.add(submit> 。 panel.add(text> 。 submit.addActionListener(this> 。 add(panel> 。 init(> 。/* 创建插入窗口 */ public void insert(> this.removeAll(> 。 submit = ne

37、w Button(" 确定 "> 。 panel = new Panel(> 。panel.setLayout(new FlowLayout(FlowLayout.LEFT>> panel.add(new Label(" 学号 :">> 。number = new TextField(40> 。 panel.add(number> 。panel.add(new Label(" 姓名 :">> 。 name = new TextField(40> 。 panel.add(

38、name> 。panel.add(new Label(" 系别 :">> 。 dept = new TextField(40>。panel.add(dept> 。panel.add(new Label(" 成绩 :">> 。 grade = new TextField(40> 。 panel.add(grade> 。 panel.add(submit> 。text = new TextArea(10, 50> 。 text.setEditable(false> 。panel.add(

39、text> 。 submit.addActionListener(this> 。 add(panel> 。init(> 。/* 创建删除窗口 */ public void delete(> this.removeAll(> 。 panel = new Panel(> 。 panel.add(new Label(" 删除的关键字 ">> 。 panel.setLayout(new FlowLayout(FlowLayout.LEFT>> submit = new Button(" 确定 "&

40、gt; 。 text = new TextArea(10, 50> 。 text.setEditable(false> 。 scan = new TextField(30> 。 choice = new Choice(> 。choice.add("name"> 。 choice.add("number"> 。 choice.add("dept"> 。 panel.add(scan> 。 panel.add(new Label(" 删除方式 :">> 。 p

41、anel.add(choice> 。 panel.add(submit> 。panel.add(text> 。 submit.addActionListener(this> 。 add(panel> 。init(> 。/* 创建修改窗口 */ public void update(> this.removeAll(> 。 panel = new Panel(> 。 panel.setLayout(new FlowLayout(FlowLayout.LEFT>> submit = new Button(" 确定 &quo

42、t;> 。 scan = new TextField(30> 。 rescan = new TextField(30>。choice = new Choice(> 。text = new TextArea(10, 50> 。 text.setEditable(false> 。 choice.add("name"> 。 choice.add("number"> 。 choice.add("dept"> 。 choice.add("grade"> 。panel

43、.add(new Label(" 要修改的学号: ">> 。panel.add(scan> 。 panel.add(new Label(" 修改后的内容: ">> 。 panel.add(rescan> 。panel.add(new Label(" 要修改的内容: ">> 。 panel.add(choice> 。panel.add(submit> 。 panel.add(text> 。 submit.addActionListener(this> 。 add(p

44、anel> 。init(> 。/* 对注册的窗口输入内容进行判断是否为空 */ public boolean isEmpty(String username, String password, repassword> if (username.isEmpty(>> this.username.setText(" 用户名不能为空 "> 。 return false 。if (password.isEmpty(>> this.password.setText(" 密码不能为空 "> 。 return fa

45、lse 。if (password.equals(repassword> = false> this.password.setText(" 密码不一致 "> 。 return false 。return true 。1*2 e ?aa */ 。 。public class Select public void select(MainFrame frame, String number, choice> Connection con = null 。Statement s = null 。ResultSet rs = null 。boolean fla

46、g = false 。if (number.isEmpty(> && "all".equals(choice> = false>StringStringframe.text.append("1?ux ?2? u ?a? £ ? £ ?">returncon = DB.getConn(>。String str = "select * from student where " + choice + " = '" + numberninif (&q

47、uot;all".equals(choice>> str = "select * from student" try s = con.createStatement(> rs = s.executeQuery(str>frame.text.append("?§ o?ttD?tt? 土 ett3 e ? " nn">while (rs.next(>> byte namebuffer = new byte512 byte deptbuffer = new byte512namebuffe

48、r = rs.getBytes("name">。deptbuffer = rs.getBytes("dept">。String name = new String(namebuffer> String dept = new String(deptbuffer> flag = true 。frame.text.append(rs.getString("number"> + "tt" + name + "tt""tt"+ dept + rs.getS

49、tring("grade"> + "n">。if (flag = false>(”?T' ? £ ? £ ? £ ?">DB.close(rs> 。DB.close(s> 。 DB.close(con> 。 catch (SQLException e> /* 更新类 */import java.sql.Connection 。import java.sql.SQLException 。import java.sql.Statement 。public clas

50、s Update public void update(MainFrame frame, String scan, String rescan,String choice> Connection con = null。Statement s = null 。 if(scan.isEmpty(>|rescan.isEmpty(>>frame.text.append(" 内容不能为空!,更新失败! "> 。 return 。con = DB.getConn(> 。String sql = "update student set &q

51、uot; + choice + "='" + rescan + "' where number =" + "'" + scan + "'" if ("grade".equals(choice>> sql = "update student set " + choice + "="+ " wherenumber+ Integer.parseInt(rescan>" + "'" + scanI Hill+ "'" 。 try s = con.createStatement(> 。 s.executeUpdate(sql> 。 frame.text.append(" 修改成功! DB.close(s> 。DB.close(s> 。 catch (SQLException e> / TODO Auto-generated catch block frame.text.append(" 修改失败!

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论