Java基于MySQL实现学生管理系统

本文为大家分享了Java基于MySQL实现学生管理系统,供大家参考,具体内容如下

因为实验室要交作业然后就做了一个学生管理系统,贴个代码纪念一下,做的太急界面什么的也比较差。

还有一些小细节没有完善不过还是能实现主要的功能的。

Window是主界面

package First; 

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; 

public class Window {
 public static void main(String[] args){
 JFrame jframe = new JFrame("学生管理系统") ; //window
 Dimension d = new Dimension(400,300);
 Point p = new Point (250,350); 

 jframe.setSize(d);
 jframe.setLocation(p);
 jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 jframe.setVisible(true); 

 JButton button1 = new JButton("添加");
 JButton button2 = new JButton("修改");
 JButton button3 = new JButton("查询");
 JButton button4 = new JButton("删除");
 JButton button5 = new JButton("浏览"); 

 FlowLayout flow = new FlowLayout(FlowLayout.LEFT,10,10);
 JPanel panel = new JPanel(flow);
 panel.add(button1);
 panel.add(button2);
 panel.add(button3);
 panel.add(button4);
 panel.add(button5); 

 jframe.add(panel); 

 button1.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
  Add add = new Add(); 

  }
 }); 

 button2.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
  Change change = new Change();
  }
 }); 

 button3.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
  Ask ask = new Ask();
  }
 }); 

 button4.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
  Delete delete = new Delete();
  }
 }); 

 button5.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
  Look look = new Look();
  }
 }); 

 } 

}

Add是添加

package First; 

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; 

import com.mysql.jdbc.Driver; 

import First.Window; 

public class Add extends JFrame {
 private static final long serialVersionUID = -1928970409928880648L; 

 JLabel jlnumber = new JLabel("学号:");
 JLabel jlname = new JLabel("姓名:");
 JLabel jlsex = new JLabel("性别:");
 JLabel jlbirthday = new JLabel("出生日期:");
 JLabel jldepartment = new JLabel("学院:"); 

 JTextField jtnumber = new JTextField("",20);
 JTextField jtname = new JTextField("",20);
 JTextField jtsex = new JTextField("",20);
 JTextField jtbirthday = new JTextField("",20);
 JTextField jtdepartment = new JTextField("",20); 

 JButton buttonadd = new JButton("添加");
 JButton buttonreturn = new JButton("返回"); 

 public Add() {
 JPanel jpnumber = new JPanel();
 JPanel jpname = new JPanel();
 JPanel jpsex = new JPanel();
 JPanel jpbirthday = new JPanel();
 JPanel jpdepartment = new JPanel();
 JPanel jpforbutton = new JPanel(new GridLayout(1,1)); 

 jpnumber.add(jlnumber);
 jpnumber.add(jtnumber); 

 jpname.add(jlname);
 jpname.add(jtname); 

 jpsex.add(jlsex);
 jpsex.add(jtsex); 

 jpbirthday.add(jlbirthday);
 jpbirthday.add(jtbirthday); 

 jpdepartment.add(jldepartment);
 jpdepartment.add(jtdepartment); 

 jpforbutton.add(buttonadd);
 jpforbutton.add(buttonreturn); 

 buttonadd.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){ 

  //Add
  Connection conn = null;
  Statement stat = null;
  PreparedStatement ps=null;
  String sql = "INSERT INTO student(number,name,sex,birthday,department) "
   + "values(?,?,?,?,?)";
  try{
   Class.forName("Driver");
   System.out.println("JBDC 加载成功!");
  }catch(Exception a){
   System.out.println("JBDC 狗带!");
   a.printStackTrace();
  }
  try{
   conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaStu","root","123");
   ps=conn.prepareStatement(sql); 

   ps.setString(1,jtnumber.getText());
   ps.setString(2,jtname.getText());
   ps.setString(3,jtsex.getText());
   ps.setString(4,jtbirthday.getText());
   ps.setString(5,jtdepartment.getText()); 

   ps.executeUpdate(); 

   //System.out.println("MySQL 连接成功!");
   //stat = conn.createStatement();
   //stat.executeUpdate(sql);
   //System.out.println("插入数据成功!"); 

  }catch (SQLException b){
   b.printStackTrace();
  }finally{
   try{
   conn.close();
   System.out.println("MySQL 关闭成功");
   }catch (SQLException c){
   System.out.println("MySQL 关闭失败 ");
   c.printStackTrace();
   } 

  }    

 }} 

  ); 

 buttonreturn.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
  Window window = new Window();
  }
 }); 

 this.setTitle("添加学生信息");
 this.setLayout(new GridLayout(9,1));
 this.add(jpnumber);
 this.add(jpname);
 this.add(jpsex);
 this.add(jpbirthday);
 this.add(jpdepartment);
 this.add(jpforbutton);
 this.setLocation(400,300);
 this.setSize(350,300);
 this.setVisible(true); 

 } 

}

Ask是查询

package First; 

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; 

import First.Window; 

public class Ask extends JFrame {
 private static final long serialVersionUID = -1928970409928880648L; 

 JLabel jlnumber = new JLabel("学号:");
 JLabel jlname = new JLabel("姓名:");
 JLabel jlsex = new JLabel("性别:");
 JLabel jlbirthday = new JLabel("出生日期:");
 JLabel jldepartment = new JLabel("学院:"); 

 JTextField jtnumber = new JTextField("",20);
 JLabel jname = new JLabel();
 JLabel jsex = new JLabel();
 JLabel jbirthday = new JLabel();
 JLabel jdepartment = new JLabel(); 

 JButton buttonask = new JButton("查询");
 JButton buttonreturn = new JButton("返回"); 

 public Ask() {
 JPanel jpnumber = new JPanel();
 JPanel jpname = new JPanel();
 JPanel jpsex = new JPanel();
 JPanel jpbirthday = new JPanel();
 JPanel jpdepartment = new JPanel();
 JPanel jpforbutton = new JPanel(new GridLayout(1,1)); 

 jpnumber.add(jlnumber);
 jpnumber.add(jtnumber); 

 jpname.add(jlname);
 jpname.add(jname); 

 jpsex.add(jlsex);
 jpsex.add(jsex); 

 jpbirthday.add(jlbirthday);
 jpbirthday.add(jbirthday); 

 jpdepartment.add(jldepartment);
 jpdepartment.add(jdepartment); 

 jpforbutton.add(buttonask);
 jpforbutton.add(buttonreturn); 

 buttonask.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
  Connection conn = null;
  ResultSet res = null;
  Statement stat = null; 

  String sql = "SELECT number,name,sex,birthday,department FROM student;";
  try{
   Class.forName("com.mysql.jdbc.Driver"); 

  }catch(Exception d){
   System.out.println("jdbc fall");
   d.printStackTrace();
  }
  try{
   conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaStu","root","123");
   stat=conn.createStatement();
   res=stat.executeQuery(sql);
   while (res.next())
   {
   if (res.getString(1).equals(jtnumber.getText()))
   {
    jname.setText(res.getString(2));
    jsex.setText(res.getString(3));
    jbirthday.setText(res.getString(4));
    jdepartment.setText(res.getString(5)); 

    break;
   }
   }
  }catch (SQLException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace(); 

  }
  finally{
   try{
   conn.close();
   }catch(SQLException ar){
   ar.printStackTrace();
   } 

  }}} 

  ); 

 buttonreturn.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
  Window window = new Window();
  }
 });
 this.setTitle("查询学生信息");
 this.setLayout(new GridLayout(9,1));
 this.add(jpnumber);
 this.add(jpname);
 this.add(jpsex);
 this.add(jpbirthday);
 this.add(jpdepartment);
 this.add(jpforbutton);
 this.setLocation(400,300);
 this.setSize(350,300);
 this.setVisible(true);
 }
}

Change是修改

package First; 

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; 

import First.Window; 

public class Change extends JFrame {
 private static final long serialVersionUID = -1928970409928880648L; 

 JLabel jlnumber = new JLabel("学号:");
 JLabel jlname = new JLabel("姓名:");
 JLabel jlsex = new JLabel("性别:");
 JLabel jlbirthday = new JLabel("出生日期:");
 JLabel jldepartment = new JLabel("学院:"); 

 JTextField jtnumber = new JTextField("",20);
 JTextField jtname = new JTextField("",20);
 JTextField jtsex = new JTextField("",20);
 JTextField jtbirthday = new JTextField("",20);
 JTextField jtdepartment = new JTextField("",20); 

 JButton buttonchange = new JButton("修改");
 JButton buttonreturn = new JButton("返回"); 

 public Change() {
 JPanel jpnumber = new JPanel();
 JPanel jpname = new JPanel();
 JPanel jpsex = new JPanel();
 JPanel jpbirthday = new JPanel();
 JPanel jpdepartment = new JPanel();
 JPanel jpforbutton = new JPanel(new GridLayout(1,1)); 

 jpnumber.add(jlnumber);
 jpnumber.add(jtnumber); 

 jpname.add(jlname);
 jpname.add(jtname); 

 jpsex.add(jlsex);
 jpsex.add(jtsex); 

 jpbirthday.add(jlbirthday);
 jpbirthday.add(jtbirthday); 

 jpdepartment.add(jldepartment);
 jpdepartment.add(jtdepartment); 

 jpforbutton.add(buttonchange);
 jpforbutton.add(buttonreturn); 

 buttonchange.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
  String number = jtnumber.getText();
  String name = jtname.getText();
  String sex = jtsex.getText();
  String birthday = jtbirthday.getText();
  String department = jtdepartment.getText(); 

  Connection conn = null;
  ResultSet res = null;
  Statement stat = null; 

  String sql = "SELECT number,name,sex,birthday,department FROM student;";
  try{
   Class.forName("com.mysql.jdbc.Driver"); 

  }catch(Exception d){
   System.out.println("jdbc fall");
   d.printStackTrace();
  }
  try{
   conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaStu","root","123");
   stat=conn.createStatement();
   res=stat.executeQuery(sql);
   while (res.next())
   {
   //change
   if (res.getString(1).equals(jtnumber.getText()))
   {
   try{
    Class.forName("com.mysql.jdbc.Driver");
   }catch(Exception d){
    System.out.println("jdbc fall");
    d.printStackTrace();
   } 

    String sql2="UPDATE student SET name='"+name+"' WHERE number='"+jtnumber.getText()+"'";
    String sql3="UPDATE student SET sex='"+sex+"' WHERE number='"+jtnumber.getText()+"'";
    String sql4="UPDATE student SET birthday='"+birthday+"' WHERE number='"+jtnumber.getText()+"'";
    String sql5="UPDATE student SET department='"+department+"' WHERE number='"+jtnumber.getText()+"'";
    try {
    conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaStu","root","123");
    stat=conn.createStatement();
    stat.executeUpdate(sql2);
    stat.executeUpdate(sql3);
    stat.executeUpdate(sql4);
    stat.executeUpdate(sql5);
    } catch (SQLException g) {
    // TODO Auto-generated catch block
    g.printStackTrace();
    }try{
    stat.close();
    conn.close();
    }catch(SQLException ar){
    ar.printStackTrace();
   } 

    break;
   } 

   //change end
   }
  }catch (SQLException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace(); 

  }
  finally{
   try{
   conn.close();
   }catch(SQLException ar){
   ar.printStackTrace();
   }
  }
  }
 }); 

 buttonreturn.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
  Window window = new Window();
  }
 }); 

 this.setTitle("修改学生信息");
 this.setLayout(new GridLayout(9,1));
 this.add(jpnumber);
 this.add(jpname);
 this.add(jpsex);
 this.add(jpbirthday);
 this.add(jpdepartment);
 this.add(jpforbutton);
 this.setLocation(400,300);
 this.setSize(350,300);
 this.setVisible(true);
 }
}

Delete是删除

package First; 

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
 import javax.swing.*; 

import First.Window; 

 public class Delete extends JFrame {
 private static final long serialVersionUID = -1928970409928880648L; 

 JLabel jlnumber = new JLabel("学号:"); 

 JTextField jtnumber = new JTextField("",20); 

 JButton buttondelete = new JButton("删除");
 JButton buttonreturn = new JButton("返回"); 

 public Delete() {
  JPanel jpnumber = new JPanel();
  JPanel jpforbutton = new JPanel(new GridLayout(1,1)); 

  jpnumber.add(jlnumber);
  jpnumber.add(jtnumber); 

  jpforbutton.add(buttondelete);
  jpforbutton.add(buttonreturn); 

  buttondelete.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
   String number = jtnumber.getText(); 

   Connection conn = null;
   ResultSet res = null;
   Statement stat = null;
   String sql = "DELETE FROM student WHERE number='"+number+"'"; 

   try{
   Class.forName("com.mysql.jdbc.Driver");
   }catch(Exception a){
   a.printStackTrace();
   }
   try{
   conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaStu","root","123");
   stat = conn.createStatement();
   stat.executeUpdate(sql);
   }catch(SQLException h){
   h.printStackTrace(); 

   }finally{
   try{
    conn.close();
    System.out.println("close success!");
   }catch(SQLException j){
    System.out.println("close go die!");
    j.printStackTrace();
   } 

   } 

  } 

  }); 

  buttonreturn.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
   Window window = new Window();
  }
  }); 

  this.setTitle("删除学生信息");
  this.setLayout(new GridLayout(9,1));
  this.add(jpnumber);
  this.add(jpforbutton);
  this.setLocation(400,300);
  this.setSize(350,300);
  this.setVisible(true); 

 } 

 } 

Look是浏览

package First; 

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*; 

import First.Window; 

public class Look extends JFrame {
 private static final long serialVersionUID = -1928970409928880648L; 

 Connection conn = null;
 PreparedStatement ps = null;
 ResultSet res = null; 

 //JButton buttonlook = new JButton("浏览");
 //JButton buttonreturn = new JButton("返回"); 

 JTable jtable;
 JScrollPane jscrollpane = new JScrollPane(); 

 Vector columnNames = null;
 Vector rowData = null; 

 public Look() {
 JPanel jpforbutton = new JPanel(new GridLayout(1,1)); 

 columnNames = new Vector();
 columnNames.add("学号");
 columnNames.add("姓名");
 columnNames.add("性别");
 columnNames.add("出生日期");
 columnNames.add("学院");
 rowData = new Vector(); 

 //jpforbutton.add(buttonlook);
 //jpforbutton.add(buttonreturn); 

 try {
  Class.forName("com.mysql.jdbc.Driver");
  conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/javaStu","root","123");
  ps = conn.prepareStatement("SELECT * FROM student");
  res = ps.executeQuery();
  while (res.next())
  {
  Vector hang = new Vector();
  hang.add(res.getString(1));
  hang.add(res.getString(2));
  hang.add(res.getString(3));
  hang.add(res.getString(4));
  hang.add(res.getString(5));
  rowData.add(hang); 

  }
  System.out.println("load ok!");
 }catch (Exception q){
  q.printStackTrace();
  System.out.println("go die");
 }finally{
  try{
  res.close();
  ps.close();
  conn.close();
  System.out.println("close ok");
  }catch (SQLException o){
  o.printStackTrace();
  System.out.println("go die 2");
  }
 } 

 jtable = new JTable(rowData,columnNames);
 jscrollpane = new JScrollPane(jtable); 

 this.add(jscrollpane);
 this.setTitle("浏览学生信息");
 this.setLayout(new GridLayout(2,5));
 this.add(jpforbutton);
 this.setLocation(300,300);
 this.setSize(500,300);
 this.setVisible(true);
 this.setResizable(false); 

 } 

} 

一些运行的界面:

更多学习资料请关注专题《管理系统开发》。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

您可能感兴趣的文章:

  • 图书管理系统java代码实现
  • Java实现学生管理系统
  • Java GUI制作简单的管理系统
  • Java实现员工管理系统
  • 学生信息管理系统java版
  • 员工管理系统java版
  • Java+MySQL实现学生信息管理系统源码
  • java学生信息管理系统MVC架构详解
  • java(swing)+ mysql实现学生信息管理系统源码
  • Java学生信息管理系统设计(数据库版)
(0)

相关推荐

  • 学生信息管理系统java版

    本文为大家分享了java学生信息管理系统的源代码,供大家参考,具体内容如下 /*学生信息管理系统,实现学生信息: *增加 int[] a=new int[9] *删除 *查找 *更改 */ import java.util.Scanner;//导入java输入流 import java.lang.*; import java.io.*; class Student { private static Student[] s=new Student[2]; int n=0; private Stri

  • 员工管理系统java版

    员工管理系统要求如下: 通过面向对象的编程思想,实现员工信息的增删改查,存储结构为数组.限定数组长度为100. 实现页面: 添加员工 查找员工 修改员工 删除员工.退出 工程目录结构: 1.Employee基类 /** * @author 李广亮 * */ public class Employee { /** * 成员属性:ID.姓名.职务.请假天数.基本工资 */ private String ID; private String name; private String job; priv

  • Java实现员工管理系统

    本文实例为大家分享了Java实现员工管理系统的具体代码,供大家参考,具体内容如下 本系统主要练习到的相关内容: 1. 流程控制语句 2. 类.对象 3. 封装.继承.多态 4. 方法的重载.重写 5. 访问修饰符 6. static 需求说明: 员工信息的基本情况 ---------普通员工---------– 属性:员工编号.员工姓名.员工职务.请假天数.基本工资 普通员工工资: 在基本工资的基础上增加10%的工作餐,50%的岗位补助,200元住房补助 基本工资+基本工资*0.1+基本工资*0

  • Java GUI制作简单的管理系统

    本文实例为大家分享了Java GUI管理系统的具体代码,供大家参考,具体内容如下 1.先完成主页面MainUI(代码如下) package com.pag_1; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class MainUI extends JFrame implements ActionLis

  • java学生信息管理系统MVC架构详解

    本文实例为大家分享了java学生信息管理系统MVC架构,供大家参考,具体内容如下 一.项目结构 学生信息管理系统分三层进行实现.student.java主要提供数据,cotroller.java的功能是绑定试图和计算数据.Stuview.java用于单一的用来显示数据. 二.源码 1.1.Student 类 /* * @FileName: Student.class * @version:1.0 * @author:nazi * 描述:模型层 * */ import java.io.Serial

  • Java学生信息管理系统设计(数据库版)

    本文实例为大家分享了数据库版的Java学生信息管理系统,供大家参考,具体内容如下 package Student_system; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import

  • Java+MySQL实现学生信息管理系统源码

    基于Java swing+MySQL实现学生信息管理系统:主要实现JDBC对学生信息进行增删改查,应付一般课设足矣,分享给大家.(由于篇幅原因,代码未全部列出,如有需要留下邮箱) 鉴于太多同学要源码,实在发不过来,上传到github上 https://github.com/ZhuangM/student.git 1. 开发环境:jdk7+MySQL5+win7 代码结构:model-dao-view 2. 数据库设计--建库建表语句: CREATE DATABASE student; DROP

  • Java实现学生管理系统

    项目描述:通过管理员帐号登录,对学员信息进行管理.可以实现学员信息的增加.修改.删除.查询. 知识点:数组.do{}while循环.for循环.if语句.switch条件语句 学生管理系统的流程图 import java.util.Scanner; public class Stu{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); boolean flag = false; int stuN

  • java(swing)+ mysql实现学生信息管理系统源码

    本文实例为大家分享了java实现学生信息管理系统源码,供大家参考,具体内容如下 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import

  • 图书管理系统java代码实现

    本文实例为大家分享了java实现图书管理系统的具体代码,供大家参考,具体内容如下 /* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名称:    <图书管理系统--java>                          * 作    者:       刘江波                       * 完成日期:    2012     年  3    

随机推荐