学生信息系统界面的实现 - JDBC

writer:pprp

登录界面的实现:

分为两个部分:

1、LoginFrame.java :

用windowbuilder进行快速搭建界面,构建好登录的界面,并用LogConnection类构建对象,进行处理,增加监听器。

2、LogConnection.java:

用基本的JDBC步骤进行处理,其中多加了一个判断用户名和密码匹配问题。

注意的问题:

1、只有在ResultSet对象使用完毕以后才能关掉Connection对象,否则会影响数据的传输,最后将连接关闭。

2、另外要注意在使用ResultSet进行结果查询的时候,需要加入一个判断,if(!rs.next())return ;否则会报错。

3、注意close函数应该在LogConnection中写,在LoginFrame的监听器中调用关闭。

LoginFrame.java

  1. package work2;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import javax.swing.JButton;
  5. import javax.swing.JFrame;
  6. import javax.swing.JLabel;
  7. import javax.swing.JOptionPane;
  8. import javax.swing.JPanel;
  9. import javax.swing.JPasswordField;
  10. import javax.swing.JTextField;
  11. import javax.swing.UIManager;
  12. import javax.swing.border.EmptyBorder;
  13. import work3.StudentFrame;
  14. @SuppressWarnings("serial")
  15. public class LoginFrame extends JFrame {
  16. private JPanel contentPane;
  17. private JTextField userFiled;
  18. private JPasswordField pwd;
  19. private LogConnection lc = null;
  20. public static void main(String[] args) {
  21. try {
  22. UIManager
  23. .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. }
  27. LoginFrame frame = new LoginFrame();
  28. frame.setVisible(true);
  29. }
  30. public LoginFrame() {
  31. setTitle("学生信息管理系统");
  32. lc = new LogConnection();
  33. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  34. setBounds(100, 100, 450, 300);
  35. contentPane = new JPanel();
  36. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  37. setContentPane(contentPane);
  38. contentPane.setLayout(null);
  39. JLabel lblNewLabel = new JLabel("\u7528\u6237\u540D\uFF1A");
  40. lblNewLabel.setBounds(63, 68, 54, 15);
  41. contentPane.add(lblNewLabel);
  42. JLabel lblNewLabel_1 = new JLabel("\u5BC6 \u7801\uFF1A");
  43. lblNewLabel_1.setBounds(63, 128, 54, 15);
  44. contentPane.add(lblNewLabel_1);
  45. userFiled = new JTextField();
  46. userFiled.setBounds(127, 65, 224, 23);
  47. contentPane.add(userFiled);
  48. // userFiled.setColumns(10);
  49. pwd = new JPasswordField();
  50. pwd.setBounds(127, 124, 224, 23);
  51. contentPane.add(pwd);
  52. JButton loginBtn = new JButton("\u786E\u5B9A");
  53. loginBtn.setBounds(63, 204, 93, 23);
  54. contentPane.add(loginBtn);
  55. JButton cancelBtn = new JButton("\u53D6\u6D88");
  56. cancelBtn.setBounds(268, 204, 93, 23);
  57. contentPane.add(cancelBtn);
  58. loginBtn.addActionListener(new ActionListener() {
  59. @Override
  60. public void actionPerformed(ActionEvent arg0) {
  61. // TODO Auto-generated method stub
  62. String name = userFiled.getText().trim();
  63. String pwdText = String.valueOf(pwd.getPassword()).trim();
  64. if (lc.Judge(name, pwdText)) {
  65. JOptionPane.showMessageDialog(LoginFrame.this, "欢迎您,"
  66. + name);
  67. @SuppressWarnings("unused")
  68. StudentFrame tmp = new StudentFrame();
  69. tmp.setVisible(true);
  70. } else {
  71. JOptionPane.showMessageDialog(LoginFrame.this, "用 户名或密码错误");
  72. }
  73. }
  74. });
  75. cancelBtn.addActionListener(new ActionListener() {
  76. @Override
  77. public void actionPerformed(ActionEvent arg0) {
  78. // TODO Auto-generated method stub
  79. System.exit(0);
  80. lc.close();
  81. }
  82. });
  83. JOptionPane.showMessageDialog(LoginFrame.this, "欢迎登陆学生信息管理系统,请输入账号密码");
  84. }
  85. }

LogConnection.java

  1. package work2;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. public class LogConnection {
  8. Connection con = null;
  9. Statement sql = null;
  10. public LogConnection() {
  11. // 1 drive
  12. try {
  13. Class.forName("com.mysql.jdbc.Driver");
  14. System.out.println("驱动加载成功");
  15. } catch (ClassNotFoundException e) {
  16. System.out.println("驱动加载失败");
  17. e.printStackTrace();
  18. }
  19. // 2 connect
  20. String url = "jdbc:mysql://localhost/test?useSSL=true";
  21. String user = "root";
  22. String password = "root";
  23. try {
  24. con = DriverManager.getConnection(url, user, password);
  25. System.out.println("链接成功");
  26. } catch (SQLException e) {
  27. System.out.println("链接失败");
  28. e.printStackTrace();
  29. }
  30. // 3 statement object
  31. try {
  32. sql = con.createStatement();
  33. System.out.println("成功生成statement对象");
  34. } catch (SQLException e) {
  35. System.out.println("生成statement对象失败");
  36. e.printStackTrace();
  37. }
  38. }
  39. public boolean Judge(String name, String passwd) {
  40. ResultSet rs = null;
  41. String str = "select * from log where logname = '" + name + "'";
  42. try {
  43. rs = sql.executeQuery(str);
  44. if (!rs.next())
  45. return false;
  46. } catch (SQLException e) {
  47. e.printStackTrace();
  48. return false;
  49. }
  50. try {
  51. String cmpPwd = rs.getString(3);
  52. if (cmpPwd.equals(passwd))
  53. return true;
  54. } catch (SQLException e) {
  55. // TODO Auto-generated catch block
  56. e.printStackTrace();
  57. }
  58. return false;
  59. }
  60. public void close(){
  61. try {
  62. con.close();
  63. System.out.println("关闭成功");
  64. } catch (SQLException e) {
  65. // TODO Auto-generated catch block
  66. e.printStackTrace();
  67. System.out.println("关闭失败");
  68. }
  69. }
  70. }

查询界面的实现:

要求:

设计学生信息管理系统。

使用Navicat的test数据库,创建Student表,包含学生的学号、姓名、年龄信息。根据以下的功能,编写相应的函数:

① 根据学号,可以查询到学生的姓名和年龄;

② 给定学生的学号、姓名、年龄,在表中追加一行信息;

③ 给定学生的学号,可以从表中删除该学生的信息;

使用图形界面实现任务三的学生信息管理系统设计。

分析:

用到上一个部分的登录界面,在登录界面的ActionListener中处理,如果成功登录,则打开查询、更新、删除界面进行操作。

Student.java: 其中是关于JDBC编程内容,为三个需求设计了三个函数,分别实现三个功能要求。

StudentFrame.java: 界面的设计,为四个按钮注册监听器,综合实现三种功能。

知识点:

1、用到的SQL语句比较多,要注意使用的时候需要在适当的时候加双引号,例如:

"insert into Student(sname,age)

values('"+name+"',"+age+")"

其中name和age都是变量。

2、注意数据库中编号是从1开始的,不是从0开始。

3、executeUpdate语句:是执行增删改的操作。

executeQuery语句:是执行查询操作的。

StudentFrame.java

  1. package work3;
  2. import java.awt.Color;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import javax.swing.JButton;
  6. import javax.swing.JFrame;
  7. import javax.swing.JLabel;
  8. import javax.swing.JOptionPane;
  9. import javax.swing.JPanel;
  10. import javax.swing.JTextField;
  11. import javax.swing.UIManager;
  12. import javax.swing.border.EmptyBorder;
  13. import work2.LoginFrame;
  14. @SuppressWarnings("serial")
  15. public class StudentFrame extends JFrame {
  16. private JPanel contentPane;
  17. private JTextField searchStuNo;
  18. private JTextField NameTextField;
  19. private JTextField AgeTextField;
  20. LoginFrame lf = null;
  21. Student stu = null;
  22. public static void main(String[] args) {
  23. try {
  24. UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  25. } catch(Exception e){
  26. e.printStackTrace();
  27. }
  28. StudentFrame frame = new StudentFrame();
  29. frame.setVisible(true);
  30. }
  31. public StudentFrame() {
  32. stu = new Student();
  33. setTitle("学生信息查询系统");
  34. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  35. setBounds(100, 100, 450, 300);
  36. contentPane = new JPanel();
  37. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  38. setContentPane(contentPane);
  39. contentPane.setLayout(null);
  40. contentPane.setBackground(Color.PINK);
  41. JLabel label = new JLabel("\u67E5\u8BE2\u7684\u5B66\u53F7\uFF1A");
  42. label.setBounds(25, 29, 100, 27);
  43. contentPane.add(label);
  44. searchStuNo = new JTextField();
  45. searchStuNo.setBounds(135, 32, 218, 24);
  46. contentPane.add(searchStuNo);
  47. searchStuNo.setColumns(10);
  48. JButton SearchBtn = new JButton("\u67E5\u8BE2");
  49. SearchBtn.setBounds(94, 66, 77, 23);
  50. contentPane.add(SearchBtn);
  51. JButton DeleteBtn = new JButton("\u5220\u9664");
  52. DeleteBtn.addActionListener(new ActionListener() {
  53. public void actionPerformed(ActionEvent arg0) {
  54. }
  55. });
  56. DeleteBtn.setBounds(276, 66, 77, 23);
  57. contentPane.add(DeleteBtn);
  58. JLabel label_1 = new JLabel(
  59. "\u52A0\u5165\u5B66\u751F\u4FE1\u606F\uFF1A");
  60. label_1.setBounds(25, 138, 100, 37);
  61. contentPane.add(label_1);
  62. NameTextField = new JTextField();
  63. NameTextField.setBounds(177, 146, 58, 21);
  64. contentPane.add(NameTextField);
  65. NameTextField.setColumns(10);
  66. AgeTextField = new JTextField();
  67. AgeTextField.setBounds(286, 146, 58, 21);
  68. contentPane.add(AgeTextField);
  69. AgeTextField.setColumns(10);
  70. JLabel label_2 = new JLabel("\u59D3\u540D:");
  71. label_2.setBounds(135, 149, 36, 15);
  72. contentPane.add(label_2);
  73. JLabel label_3 = new JLabel("\u5E74\u9F84:");
  74. label_3.setBounds(251, 149, 54, 15);
  75. contentPane.add(label_3);
  76. JButton SureBtn = new JButton("\u786E\u5B9A");
  77. SureBtn.setBounds(94, 203, 77, 23);
  78. contentPane.add(SureBtn);
  79. JButton ExitBtn = new JButton("\u9000\u51FA");
  80. ExitBtn.setBounds(276, 203, 77, 23);
  81. contentPane.add(ExitBtn);
  82. SearchBtn.addActionListener(new ActionListener() {
  83. @Override
  84. public void actionPerformed(ActionEvent arg0) {
  85. // TODO Auto-generated method stub
  86. int ssno = Integer.parseInt(searchStuNo.getText());
  87. String tmp = stu.selectNameAge(ssno);
  88. JOptionPane.showMessageDialog(StudentFrame.this, tmp);
  89. searchStuNo.setText("");
  90. }
  91. });
  92. DeleteBtn.addActionListener(new ActionListener() {
  93. @Override
  94. public void actionPerformed(ActionEvent arg0) {
  95. // TODO Auto-generated method stub
  96. int ssno = Integer.parseInt(searchStuNo.getText());
  97. if(stu.DeleteInf(ssno)){
  98. JOptionPane.showMessageDialog(StudentFrame.this, "删除成功!");
  99. }else {
  100. JOptionPane.showMessageDialog(StudentFrame.this, "删除失败!");
  101. }
  102. searchStuNo.setText("");
  103. }
  104. });
  105. ExitBtn.addActionListener(new ActionListener() {
  106. @Override
  107. public void actionPerformed(ActionEvent arg0) {
  108. // TODO Auto-generated method stub
  109. System.exit(0);
  110. }
  111. });
  112. SureBtn.addActionListener(new ActionListener() {
  113. @Override
  114. public void actionPerformed(ActionEvent arg0) {
  115. // TODO Auto-generated method stub
  116. String ssname = NameTextField.getText();
  117. int sage = Integer.parseInt(AgeTextField.getText());
  118. stu.add(ssname,sage);
  119. NameTextField.setText("");
  120. AgeTextField.setText("");
  121. JOptionPane.showMessageDialog(StudentFrame.this, "插入成功!");
  122. }
  123. });
  124. }
  125. }

Student.java

  1. package work3;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. public class Student {
  8. Connection con = null;
  9. Statement sql = null;
  10. ResultSet rs = null;
  11. public Student() {
  12. // 1 drive
  13. try {
  14. Class.forName("com.mysql.jdbc.Driver");
  15. System.out.println("驱动加载成功");
  16. } catch (ClassNotFoundException e) {
  17. System.out.println("驱动加载失败");
  18. e.printStackTrace();
  19. }
  20. // 2 connect
  21. String url = "jdbc:mysql://localhost/test?useSSL=true";
  22. String user = "root";
  23. String password = "root";
  24. try {
  25. con = DriverManager.getConnection(url, user, password);
  26. System.out.println("链接成功");
  27. } catch (SQLException e) {
  28. System.out.println("链接失败");
  29. e.printStackTrace();
  30. }
  31. // 3 statement object
  32. try {
  33. sql = con.createStatement();
  34. System.out.println("成功生成statement对象");
  35. } catch (SQLException e) {
  36. System.out.println("生成statement对象失败");
  37. e.printStackTrace();
  38. }
  39. }
  40. public String selectNameAge(int sno){
  41. String sqls = "select sname,age from student where sno = '"+String.valueOf(sno)+"'";
  42. try {
  43. rs = sql.executeQuery(sqls);
  44. if(!rs.next()){
  45. return ("不存在!");
  46. }
  47. System.out.println("查询成功");
  48. String stuname = rs.getString(1);
  49. int stuage = rs.getInt(2);
  50. return ("学号为"+sno+"的学生的姓名为:"+stuname+" ,年龄为:" + stuage);
  51. } catch (SQLException e) {
  52. // TODO Auto-generated catch block
  53. System.out.println("查询失败");
  54. e.printStackTrace();
  55. }
  56. return "不存在";
  57. }
  58. public void add(String name,int age){
  59. String str = "insert into Student(sname,age) values('"+name+"',"+age+")";
  60. try {
  61. sql.executeUpdate(str);
  62. System.out.println("插入成功");
  63. } catch (SQLException e) {
  64. // TODO Auto-generated catch block
  65. System.out.println("插入失败");
  66. e.printStackTrace();
  67. }
  68. }
  69. public boolean DeleteInf(int no){
  70. String str = "delete from Student where sno = "+no+"";
  71. try {
  72. sql.executeUpdate(str);
  73. System.out.println("删除成功");
  74. return true;
  75. } catch (SQLException e) {
  76. // TODO Auto-generated catch block
  77. System.out.println("删除失败");
  78. e.printStackTrace();
  79. }
  80. return false;
  81. }
  82. }
如果感觉有帮助,请点个赞

nwafu - java实习 JDBC练习 - 学生信息系统界面的更多相关文章

  1. Java使用JDBC连接SQL Server数据库|实现学生成绩信息系统

    Java实验四 JDBC 使用SQL Server数据库或者MySQL数据库各自的客户端工具,完成如下任务: (1)创建数据库students: (2)在数据students中创建表scores,包括 ...

  2. Java程序设计——学生信息系统

    1.团队课程设计博客链接 http://www.cnblogs.com/YYYYYYY/p/7065278.html 2.个人负责模块说明 2.1 管理界面 2.2 清空:单击清空键,可清空数据栏 2 ...

  3. Java使用JDBC连接SQL Server数据库

    Java使用JDBC连接SQL Server数据库 1.下载驱动 1.下载Microsoft SQL Server JDBC 驱动程序 https://docs.microsoft.com/zh-cn ...

  4. 最近找java实习面试被问到的东西总结(Java方向)

    时间,就是这么很悄悄的溜走了将近两个年华,不知不觉的,研二了,作为一个一般学校的研究生,不知道该说自己是不学无术,还是说有过努力,反正,这两年里,有过坚持,有过堕落,这不,突然间,有种开窍的急迫感,寻 ...

  5. Java课设(学生信息管理系统)

    1.团队课程设计博客链接 http://www.cnblogs.com/Min21/p/7064093.html 2.个人负责模板或任务说明 设计登陆界面和学生信息界面的设计,学生信息的显示.退出等功 ...

  6. 广州三本找Java实习经历

    前言 只有光头才能变强 这阵子跑去面试Java实习生啦~~~我来简单介绍一下背景吧. 广州三本大三在读,在广州找实习.大学开始接触编程,一个非常平庸的人. 在学习编程时,跟我类似的人应该会有一个疑问: ...

  7. java用JDBC连接MySQL数据库的详细知识点

    想实现java用JDBC连接MySQL数据库.需要有几个准备工作: 1.下载Connector/J的库文件,下载Connector/J的官网地址:http://www.mysql.com/downlo ...

  8. 使用Java语言编写一个五子棋UI界面并实现网络对战功能(非局域网)

    使用Java语言编写一个五子棋UI界面并实现网络对战功能(非局域网) 一,前期准备 1,Java IDE(Eclipse)与JDK的安装与配置jdk-15.0.1-免配置路径版提取码:earu免安装版 ...

  9. Java的JDBC操作

    Java的JDBC操作 [TOC] 1.JDBC入门 1.1.什么是JDBC JDBC从物理结构上来说就是java语言访问数据库的一套接口集合,本质上是java语言根数据库之间的协议.JDBC提供一组 ...

随机推荐

  1. redis之数据操作详解

    redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...

  2. shell_03

    函数: fanction print_welcome(){ echo welcome now time is `date` } print_welcome 函数调用 print _welcome 00 ...

  3. MongoDB学习笔记系列~目录

    MongoDB学习笔记~环境搭建 (2015-03-30 10:34) MongoDB学习笔记~MongoDBRepository仓储的实现 (2015-04-08 12:00) MongoDB学习笔 ...

  4. PAT 1091 Acute Stroke [难][bfs]

    1091 Acute Stroke (30 分) One important factor to identify acute stroke (急性脑卒中) is the volume of the ...

  5. Java中二叉树存储结构实现

    一.二叉树 二叉树指的是每个节点最多只能有两个子树的有序树.通常左边的子树被称为“左子树”(left subtree),右边的子树被称为右子树. 二叉树的每个节点最多只有2棵子树,二叉树的子树次序不能 ...

  6. xe7开发的安卓程序,体积宏大--112M!

    原因没找到,但似乎可以这样解决: 解决过程:因为代码很少,所以我重新建立一个空白程序,把代码复制过去,一字不差.重新编译, 关键的时刻到了:不要连上真机,在编译完成时,系统提示是否要启动android ...

  7. 06 swap命令,进程管理,rmp命令与yum命令,源码安装python

    作业一: 1)开启Linux系统前添加一块大小为15G的SCSI硬盘 2)开启系统,右击桌面,打开终端 3)为新加的硬盘分区,一个主分区大小为5G,剩余空间给扩展分区,在扩展分区上划分1个逻辑分区,大 ...

  8. Django:学习笔记(5)——会话

    Django:学习笔记(5)——会话 配置中间件 Django中使用会话,需要配置一个中间件. 配置会话引擎 默认情况下,Django在数据库中存储sessions(使用了django.contrib ...

  9. PKU 3318 Matrix Multiplication(神奇的输入)

    #include<cstdio> using namespace std; ][]; ][],C[][]; int Read() { ; ; while((ch=getchar())==' ...

  10. AngularJS多模块开发与路由

    这里只是做一个笔记 angularjs模块(父子级)比如我有一个项目叫做shcool,那么我school下边有两个模块,student.teacher.此时school就属于主模块,其他都是子模块.子 ...