JDBC+GUI实现简单学生管理系统
刚学完JDBC不久,做了一个简单的学生管理系统,可能还有不完善的地方,望各路大神见谅。废话不多说,我先贴个图让大家让大家瞅瞅,觉得是你想要的再看下去吧。
创新互联建站 - 西部信息服务器托管,四川服务器租用,成都服务器租用,四川网通托管,绵阳服务器托管,德阳服务器托管,遂宁服务器托管,绵阳服务器托管,四川云主机,成都云主机,西南云主机,西部信息服务器托管,西南服务器托管,四川/成都大带宽,服务器机柜,四川老牌IDC服务商
我是以管理者的身份去做的,适合初学者去学习。
在做之前,先捋一遍思路,简单来说分为三大步。
一、在数据库里建Student表存放学生信息
二、用JDBC来连接、操作数据库
三、展示Student数据,实现增删改查功能。
思路是非常简单的,但是要实现还是有很多细节需要注意,下面我就贴上我的代码,结合着代码给大家一步步的分析说明。
实现:
一、在数据库建表:这个不用细说,直接贴图。
二、用JDBC连接数据库:这一块对于刚刚学JDBC的同学来说可能比较绕,所以我把这一块又分成了四部分(最后的db.properties跟com.student.db一起的),我会逐个说明。看图。
(1)com.student.db包里有两个类,一个是DBHelper 一个是DBManager,这俩类是用JDBC连接数据库的,固定写法。
DBManager类
package com.student.db; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.student.mapper.IMapper; public class DBManager { //这里把JDBC连接数据库的步骤(找驱动,建连接,建通道,执行SQL)封装在DBHelper类里面,在DBManager里用getConnection()调用。这样写的目的是方便 public Connection getConnection(){ try { return DBHelper.getConnection();//得到DBHelper类里面写好的连接 } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return null; } //增删改结果集。因为sql语句是变化的,所以设为参数比较方便。params是占位符的,没学的可以忽略。 public int executeUpdate(String sql,Object[] params){ Connection conn=null; PreparedStatement pst=null; try { conn=getConnection();//连接 pst=conn.prepareStatement(sql);//通道 if(params != null){//占位符的应用。 for(int i=0;i
DBHelper类。在写之前先建一个properties文件,名字为db.properties(如图),注意不要建在包里面。
package com.student.db; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; public class DBHelper { private static String DRIVER; private static String URL; private static String USER; private static String PASSWORD; static{ Properties pro=new Properties(); InputStream in=DBHelper.class.getClassLoader() .getResourceAsStream("db.properties");//读取文件数据 try { pro.load(in); } catch (IOException e) { e.printStackTrace(); } DRIVER=pro.getProperty("DRIVER"); URL=pro.getProperty("URL"); USER=pro.getProperty("USER"); PASSWORD=pro.getProperty("PASSWORD"); } public static Connection getConnection() throws ClassNotFoundException, SQLException{ Class.forName(DRIVER);//找驱动 return DriverManager.getConnection(URL, USER, PASSWORD);//建连接 } }
(2)com.student.vo包。这里面有一个vo类,我们是要把数据库里的数据放到java里展示,用一个类对象把数据库里的信息一一对应起来就可以很容易的操作。数据库里的一个列对应类对象的一个属性。
package com.student.vo; public class Student { private String stuid; private String name; private String age; private String sex; public String getStuid(){ return stuid; } public void setStuid(String stuid){ this.stuid=stuid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Student(String stuid,String name,String sex,String age){ super(); this.stuid=stuid; this.name=name; this.age=age; this.sex=sex; } public Student(){ super(); } }
(3)com.student.mapper包。这里面一个接口,一个实现类。
接口:
package com.student.mapper; import java.sql.ResultSet; import java.util.List; public interface IMapper { List map(ResultSet rst);//声明一个方法存着结果集。 }
实现类:
package com.student.mapper; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.student.vo.Student; public class StuMapper implements IMapper {//实现接口方法 public List map(ResultSet rst) { Listlist=new ArrayList ();//建一个集合,里面是Student类里的信息。 try { while(rst.next()){// Student stu=new Student(); stu.setStuid(rst.getString("STUID"));//类对象每一个属性对应数据库的每一列。 stu.setName(rst.getString("STUNAME")); stu.setAge(rst.getString("AGE")); stu.setSex(rst.getString("SEX")); list.add(stu);//把类对象放到集合里 } } catch (SQLException e) { e.printStackTrace(); } return list; } }
(4)com.student.dao包:这里面的StuDAO类放着增删改查分页等功能
package com.student.dao; import java.util.List; import com.student.db.DBManager; import com.student.mapper.IMapper; import com.student.mapper.StuMapper; import com.student.vo.Student; public class StuDAO { public Listcheck(){//查看 String sql="select * from STUDENT";//sql语句 DBManager db=new DBManager(); IMapper mapper=new StuMapper();//实现StuMapper List list=db.executeQuery(sql, mapper,null);//null是指占位符为null,因为查看的是所有信息 return list; } public boolean add(Student stu){//添加 String sql="insert into STUDENT values(?,?,?,?)"; Object[] params={stu.getStuid(),stu.getName(),stu.getAge(),stu.getSex()}; DBManager db=new DBManager(); int i=db.executeUpdate(sql, params); if(i>=0){ System.out.println("成功"); }else{ System.out.println("失败"); } return true; } public boolean update(Student stu){//修改 String sql="update STUDENT set stuname=?,age=?,sex=? where stuid=?"; Object params[]={stu.getName(),stu.getAge(),stu.getSex(),stu.getStuid()}; DBManager db=new DBManager(); int i=db.executeUpdate(sql, params); if(i>=0){ System.out.println("成功"); }else{ System.out.println("失败"); } return true; } public boolean delete(Student stu){//删除 String sql="delete from STUDENT where stuid=?"; Object params[]={stu.getStuid()}; DBManager db=new DBManager(); int i=db.executeUpdate(sql, params); if(i>=0){ System.out.println("成功"); }else{ System.out.println("失败"); } return true; } public List findPage(int pagesize,int pagenow){//分页 String sql="select * from (select rownum rn ,stu .* from stu) " + "where rownum<=? and rn>?";//分页公式 Object []params={pagesize,(pagenow-1)*pagesize}; DBManager db=new DBManager(); IMapper mapper=new StuMapper(); return db.executeQuery(sql, mapper, params); } public int findcount(){ String sql="select count(*) from stu"; DBManager db=new DBManager(); return db.count(sql); } }
当把这一块写完之后,其实大部分就已经完成了,JDBC连接数据库基本上是固定的,多写几遍就明白了。
三、展示Student信息,实现增删改查。看图:
(1)com.student.show包,展示界面:这里面内容比较多,但是都很容易理解
package com.student.show; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTable; import com.student.add.Add; import com.student.check.Check; import com.student.dao.StuDAO; import com.student.delete.Delete; import com.student.update.Update; import com.student.vo.Student; public class Show extends JFrame { public static int pagesize=5;//每页显示5条信息 public static int pagenow=1;//当前页为第一页 public Show() { setSize(500, 430); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE);//点X号就是关闭 setResizable(false);//不可改变窗口大小 setLocationRelativeTo(null);//默认居中显示 setLayout(null);//采用坐标布局 StuDAO dao = new StuDAO();//前面我们已经把增删改查分页写到StuDAO里面,现在就直接拿出来用 Listlist =dao.findPage(pagesize, pagenow); Student stu = new Student(); for (int i = 0; i < list.size(); i++) { stu = list.get(i); } String[] rowName = { "学号", "姓名", "年龄", "性别" };//从这里开始是二维数组的遍历使用 Object[][] data = new Object[list.size()][4]; for (int i = 0; i < list.size(); i++) { Student s = list.get(i); Object st[] = { s.getStuid(), s.getName(), s.getAge(), s.getSex() }; data[i] = st; } final JTable table = new JTable(data,rowName); JScrollPane JSP=new JScrollPane(table);//这一步不能省去,否则显示不出列名 JSP.setBounds(20, 10, 400, 200); add(JSP); JButton jb11=new JButton("首页"); jb11.setBounds(40,220,80,30); add(jb11); JButton jb22=new JButton("上一页"); jb22.setBounds(130,220,80,30); add(jb22); JButton jb33=new JButton("下一页"); jb33.setBounds(220,220,80,30); add(jb33); JButton jb44=new JButton("尾页"); jb44.setBounds(310,220,80,30); add(jb44); JButton jb1 = new JButton("查看信息"); jb1.setBounds(50, 270, 100, 30); add(jb1); JButton jb2 = new JButton("修改信息"); jb2.setBounds(280, 270, 100, 30); add(jb2); JButton jb3 = new JButton("添加信息"); jb3.setBounds(50, 320, 100, 30); add(jb3); JButton jb4 = new JButton("删除信息"); jb4.setBounds(280, 320, 100, 30); add(jb4); JButton jb5 = new JButton("退出"); jb5.setBounds(280, 360, 100, 30); add(jb5); jb1.addActionListener(new ActionListener() {//查看 public void actionPerformed(ActionEvent event) { int row = table.getSelectedRow();//选中第几行 int index = 0; if(row==-1){ JOptionPane.showMessageDialog(null,"您没有选中信息"); return; } String id = (String) table.getValueAt(row, index);// 跟Check联系起来 Check check=new Check(id); check.setVisible(true); setVisible(false); } }); jb2.addActionListener(new ActionListener() {//修改 public void actionPerformed(ActionEvent event) { int row = table.getSelectedRow(); int index = 0; if(row==-1){ JOptionPane.showMessageDialog(null,"您没有选中信息"); return; } String id = (String) table.getValueAt(row, index);// 跟Update联系起来 Update up=new Update(id); up.setVisible(true); setVisible(false); } }); jb3.addActionListener(new ActionListener() {//添加 public void actionPerformed(ActionEvent event) { Add add = new Add(); add.setVisible(true); setVisible(false); } }); jb4.addActionListener(new ActionListener() {//删除 public void actionPerformed(ActionEvent event) { int row = table.getSelectedRow(); int index = 0; if(row==-1){ JOptionPane.showMessageDialog(null,"您没有选中信息"); return; } String num=(String) table.getValueAt(row, index); Delete d=new Delete(num); d.setVisible(true); setVisible(false); } }); jb11.addActionListener(new ActionListener() {//首页 public void actionPerformed(ActionEvent event) { pagenow=1; Show show=new Show(); setVisible(false); show.setVisible(true); } }); jb22.addActionListener(new ActionListener() {//上一页 public void actionPerformed(ActionEvent event) { if(pagenow != 1){ pagenow=pagenow-1; }else{ return; } Show show=new Show(); setVisible(false); show.setVisible(true); } }); jb33.addActionListener(new ActionListener() {//下一页 public void actionPerformed(ActionEvent event) { StuDAO dao=new StuDAO(); int count=dao.findcount(); int pageCount=(count-1)/pagesize+1;//pageCount表示最后一页 if(pagenow != pageCount){ pagenow=pagenow+1; }else{ return; } Show show=new Show(); setVisible(false); show.setVisible(true); } }); jb44.addActionListener(new ActionListener() {//尾页 public void actionPerformed(ActionEvent event) { StuDAO dao=new StuDAO(); int count=dao.findcount(); int pageCount=(count-1)/pagesize+1; pagenow=pageCount; Show show=new Show(); setVisible(false); show.setVisible(true); } }); } public static void main(String args[]) { Show s = new Show(); } }
(2)增删改查:大同小异,因为我们在StuDAO里面已经写好了,在用的时候就方便多了。
①添加:
package com.student.add; import java.sql.SQLException; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import com.student.dao.StuDAO; import com.student.db.DBManager; import com.student.show.Show; import com.student.vo.Student; public class Add extends JFrame{ public Add(){ setSize(300,400); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); setResizable(false); setLocationRelativeTo(null); setLayout(null); JLabel j0=new JLabel("添加信息"); j0.setBounds(100,20,80,30); add(j0); JLabel j1=new JLabel("学号:"); j1.setBounds(30,70,50,30); add(j1); final JTextField jt1=new JTextField(); jt1.setBounds(100,70,130,30); add(jt1); JLabel j2=new JLabel("姓名:"); j2.setBounds(30,120,50,30); add(j2); final JTextField jt2=new JTextField(); jt2.setBounds(100,120,130,30); add(jt2); JLabel j3=new JLabel("性别:"); j3.setBounds(30,170,50,30); add(j3); final JTextField jt3=new JTextField(); jt3.setBounds(100,170,130,30); add(jt3); JLabel j4=new JLabel("年龄:"); j4.setBounds(30,220,50,30); add(j4); final JTextField jt4=new JTextField(); jt4.setBounds(100,220,130,30); add(jt4); JButton jb1=new JButton("添加"); jb1.setBounds(50,280,80,30); add(jb1); JButton jb2=new JButton("返回"); jb2.setBounds(150,280,80,30); add(jb2); jb1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ String a=jt1.getText();//获取输入的信息 String b=jt2.getText(); String c=jt3.getText(); String d=jt4.getText(); Student stu=new Student(a,b,c,d); StuDAO dao=new StuDAO(); Listlist=dao.check();//调用StuDAO里面的check()方法 for(Student st:list){//遍历集合 if(st.getStuid().equals(a)){ JOptionPane.showMessageDialog(null,"该账号存在"); return; } } dao.add(stu); JOptionPane.showMessageDialog(null,"添加成功"); Show show=new Show(); show.setVisible(true); setVisible(false); } }); jb2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ Show s=new Show(); s.setVisible(true); setVisible(false); } }); } public static void main(String []args){ Add add=new Add(); } }
②修改:
package com.student.update; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import com.student.dao.StuDAO; import com.student.db.DBManager; import com.student.show.Show; import com.student.vo.Student; public class Update extends JFrame{ public Update(final String id){ setSize(300,400); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); setResizable(false); setLocationRelativeTo(null); setLayout(null); JLabel j0=new JLabel("修改信息"); j0.setBounds(100,20,80,30); add(j0); JLabel j1=new JLabel("学号:"); j1.setBounds(30,70,50,30); add(j1); final JLabel jt1=new JLabel(); jt1.setBounds(100,70,130,30); add(jt1); JLabel j2=new JLabel("姓名:"); j2.setBounds(30,120,50,30); add(j2); final JTextField jt2=new JTextField(); jt2.setBounds(100,120,130,30); add(jt2); JLabel j3=new JLabel("年龄:"); j3.setBounds(30,170,50,30); add(j3); final JTextField jt3=new JTextField(); jt3.setBounds(100,170,130,30); add(jt3); JLabel j4=new JLabel("性别:"); j4.setBounds(30,220,50,30); add(j4); final JTextField jt4=new JTextField(); jt4.setBounds(100,220,130,30); add(jt4); JButton jb1=new JButton("修改"); jb1.setBounds(50,280,80,30); add(jb1); JButton jb2=new JButton("返回"); jb2.setBounds(150,280,80,30); add(jb2); StuDAO dao=new StuDAO(); Listlist=dao.check(); Student stu=new Student(); for(int i=0;i
③查看:
package com.student.check; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import com.student.dao.StuDAO; import com.student.show.Show; import com.student.vo.Student; public class Check extends JFrame{ public Check(String id) { setSize(300,400); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); setResizable(false); setLocationRelativeTo(null); setLayout(null); JLabel j0=new JLabel("学生信息"); j0.setBounds(100,20,80,30); add(j0); JLabel j1=new JLabel("学号:"); j1.setBounds(30,70,50,30); add(j1); final JLabel jt1=new JLabel(); jt1.setBounds(100,70,130,30); add(jt1); JLabel j2=new JLabel("姓名:"); j2.setBounds(30,120,50,30); add(j2); final JLabel jt2=new JLabel(); jt2.setBounds(100,120,130,30); add(jt2); JLabel j3=new JLabel("年龄:"); j3.setBounds(30,170,50,30); add(j3); final JLabel jt3=new JLabel(); jt3.setBounds(100,170,130,30); add(jt3); JLabel j4=new JLabel("性别:"); j4.setBounds(30,220,50,30); add(j4); final JLabel jt4=new JLabel(); jt4.setBounds(100,220,130,30); add(jt4); JButton jb1=new JButton("确认"); jb1.setBounds(50,280,80,30); add(jb1); JButton jb2=new JButton("返回"); jb2.setBounds(150,280,80,30); add(jb2); StuDAO dao=new StuDAO(); Listlist=dao.check(); Student stu=new Student(); for(int i=0;i
④删除:
package com.student.delete; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import com.student.dao.StuDAO; import com.student.db.DBManager; import com.student.show.Show; import com.student.vo.Student; public class Delete extends JFrame{ public Delete(final String num){ setSize(300,400); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); setResizable(false); setLocationRelativeTo(null); setLayout(null); JLabel j0=new JLabel("您确认要删除该信息吗"); j0.setBounds(100,20,200,30); add(j0); JLabel j1=new JLabel("学号:"); j1.setBounds(30,70,50,30); add(j1); final JLabel jt1=new JLabel(); jt1.setBounds(100,70,130,30); add(jt1); JLabel j2=new JLabel("姓名:"); j2.setBounds(30,120,50,30); add(j2); final JLabel jt2=new JLabel(); jt2.setBounds(100,120,130,30); add(jt2); JLabel j3=new JLabel("年龄:"); j3.setBounds(30,170,50,30); add(j3); final JLabel jt3=new JLabel(); jt3.setBounds(100,170,130,30); add(jt3); JLabel j4=new JLabel("性别:"); j4.setBounds(30,220,50,30); add(j4); final JLabel jt4=new JLabel(); jt4.setBounds(100,220,130,30); add(jt4); JButton jb1=new JButton("确认"); jb1.setBounds(20,280,80,30); add(jb1); JButton jb2=new JButton("返回"); jb2.setBounds(180,280,80,30); add(jb2); StuDAO dao=new StuDAO(); Listlist=dao.check(); Student stu=new Student(); for(int i=0;i
最后贴一下登录页面,因为是以管理者的身份登录的不需要判断,就非常简单:
package com.student.login; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPasswordField; import javax.swing.JTextField; import com.student.show.Show; public class Login extends JFrame{ public Login(){ setSize(300,250); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); setResizable(false); setLocationRelativeTo(null); setLayout(null); JLabel j=new JLabel("登录窗口"); j.setBounds(100,20,80,30); add(j); JLabel j1=new JLabel("用户名:"); j1.setBounds(50,80,60,30); add(j1); final JTextField jt1=new JTextField(); jt1.setBounds(120,80,120,30); add(jt1); JLabel j2=new JLabel("密 码:"); j2.setBounds(50,130,60,30); add(j2); final JPasswordField jp=new JPasswordField(); jp.setBounds(120,130,120,30); add(jp); JButton jb1=new JButton("登录"); jb1.setBounds(70,180,60,30); add(jb1); JButton jb2=new JButton("重置"); jb2.setBounds(170,180,60,30); add(jb2); jb1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ String id=jt1.getText(); char ch[]=jp.getPassword(); String pass=new String(ch); if(id.equals(abcdefj){//设置用户名为abcdefj if(pass.equals(123456)){//设置密码为123456 JOptionPane.showMessageDialog(null,"登录成功"); Show s=new Show();//成功后跳到Show s.setVisible(true); setVisible(false); }else{ JOptionPane.showMessageDialog(null,"密码错误"); jt1.setText(""); return; } }else{ JOptionPane.showMessageDialog(null,"您输入的账号有误"); jt1.setText(""); jp.setText(""); return; } } }); } public static void main(String []args){ Login lo=new Login(); } }
写在最后:
刚开始学的时候感觉很绕,尤其是JDBC那,后来发现,是因为前面java基础掌握的不行,我又回去好好复习了java基础,才发现JDBC是死的,固定的写法,背过就行了。所以再做这个学生管理系统,就感觉不复杂了。先有一个大的思路,然后顺着思路往下走,逐步实现每个功能。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。
网页名称:JDBC+GUI实现简单学生管理系统
文章地址:http://ybzwz.com/article/gohhoi.html