一、一个学生管理系统,连一个像样的后台都没有,这怎么能忍 难道就真的没有一个可以拿的出手的?
既然你诚心诚意的发问了,那么我就大发慈悲的告诉你,这个真的有!
今天我为大家带来的是一个高端的,钻石版本的学生管理系统。好了,闲话不扯一些,下面进入正题:
先来屡一下思路:
1.首先需要先导jar包:一共需要两个 一个是ojdbc14.jar,它是用于实现用jdbc连接数据库的。另一个是c3p0-0.9.1.2.jar,它是使用数据库连接池需要导入的一个包。
2.需要建立连接数据库的配置文件,即db.properties
3.建db包:这个包中有两个类,一个是DBHelper,一个是DBManager 这俩都是用来进行数据库连接和管理的。
4.建vo包:这个包里面全是一些实体类,只有get和set方法
5.建mapper包:这是个新名词,以前没见过,感觉好高大上的样子,其实这是一个对查询得到的结果集的一个映射,理解了也就那么回事,每个表对应一个mapper类,具体使用下面代码中会提及。
6.建dao包:这个包中每个类对应一个表,每个类中就是对这个表的增删改查等方法的一个封装。
7.建show包:这个是通过图形化界面对我们表的一个展示。
8.之后就是建立add包update包...等等一些具体界面的展示了,在这里就不一一介绍了。
二.介绍了这么多,连个效果图都没有,还说什么呀说,有图有真相,下面我们就先来看一下效果图。
其实也是有登录的,只不过界面比较丑,就先不摆出来了
三、接下来就进入编程阶段:
1.导jar包就不多说一些了,把下载下来的jar包放到WebRoot下的WEB-INF下的lib包中。
2.然后是建立配置文件,选中src右击选择new找到File然后写上你的配置文件名xxx.properties就行了
配置文件主要由4部分组成:
a.驱动字符串: DRIVER = oracle.jdbc.driver.OracleDriver 每个单词分别代表:要连接的数据库 连接协议 驱动类
b.连接字符串: URL = jdbc:oracle:thin:@localhost:1521:orcl 每个单词分别代表:连接协议 要连接的数据库 连接方式(直连) 要连接ip oracle数据库的端口号 数据库实例
c.用户名: USER = scott 这个登录用户名可以改成自己的
d.密码:PASSWORD = tiger 这个密码也可以改成与登录用户相对应的密码
3.先来看一下数据库连接的第一个类DBHelper
package com.db;
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DBHelper {
public static String DRIVER;
public static String URL;
public static String USER;
public static String PASSWORD;
public static ComboPooledDataSource cpd= null;//数据库连接池
static{
Properties pro = new Properties();
//用输入流获取配置文件信息
InputStream in = DBHelper.class.getClassLoader().getResourceAsStream("db.properties");
try {
//将获取到的信息加载到Properties的对象中
pro.load(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//通过Properties对象的getProperties()方法获取具体值
DRIVER = pro.getProperty("DRIVER");
URL = pro.getProperty("URL");
USER = pro.getProperty("USER");
PASSWORD = pro.getProperty("PASSWORD");
//初始化数据库连接池
cpd = new ComboPooledDataSource();
try {
//加载驱动字符串
cpd.setDriverClass(DRIVER);
} catch (PropertyVetoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//加载连接字符串
cpd.setJdbcUrl(URL);
//加载用户名
cpd.setUser(USER);
//加载用户密码
cpd.setPassword(PASSWORD);
cpd.setInitialPoolSize(20);//初始化连接池可连接数量为20
cpd.setAcquireIncrement(5);//当不够时每次增加5
cpd.setMaxPoolSize(100);//设置最大连接数量为100
}
public static void main(String args[]){
DBHelper db = new DBHelper();
}
}
4.在写数据库连接的第二个类之前我们需要进行一些准备工作,即建立Student2类和IMapper接口和StudentMapper类,这里我们只是象征性的展示一部分,至于相关的Clazz类 和ClazzMapper等类大家自己可以比着写一下。
Student2类:
package com.vo;
public class Student2 {
private int stuid;
private String stuname;
private String sex;
private int claid;
//使用公共方法操作私有属性
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getClaid() {
return claid;
}
public void setClaid(int claid) {
this.claid = claid;
}
}
IMapper接口:
package com.mapper;
import java.sql.ResultSet;
import java.util.List;
public interface IMapper {
List map(ResultSet rst);//小括号不是尖括号
}
StudentMapper类:
package com.mapper;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.vo.Student2;
public class StudentMapper implements IMapper {
//用于将结果集中的一行数据封装成一个vo实体类,将整个结果集转换成一个集合。
public List map(ResultSet rst) {
List list = new ArrayList();
try{
while(rst.next()){
Student2 st = new Student2();
st.setStuid(rst.getInt("STUID"));//通过列名进行获取
st.setStuname(rst.getString("STUNAME"));
st.setSex(rst.getString("SEX"));
st.setClaid(rst.getInt("CLAID"));
list.add(st);
}
return list;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
package com.db;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.mapper.IMapper;
public class DBManager {
public Connection getConnection(){
try {
return DBHelper.cpd.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public int executeUpdate(String sql,Object[] obj){
Connection conn = null;
PreparedStatement pst = null;
try{
conn = getConnection();//建连接
pst = conn.prepareStatement(sql);//建通道
if(obj!=null){
for(int i = 0;i
package com.dao;
import java.util.List;
import com.db.DBManager;
import com.mapper.StudentMapper;
import com.vo.Student2;
public class StudentDao {
public int add(Student2 st){
//占位符特点:1、下标从1开始 2、使用Object[]存放参数3.可以代替所有类型的参数
String sql = "insert into student2 values(?,?,?,?)";
DBManager db = new DBManager();
Object[] obj = {st.getStuid(),st.getStuname(),st.getSex(),st.getClaid()};
int i = db.executeUpdate(sql, obj);
return i;
}
public int delete(int id){
String sql = "delete from student2 where stuid = ?";
Object[] obj = {id};
DBManager db = new DBManager();
int i = db.executeUpdate(sql, obj);
return i;
}
public int update(Student2 st){
String sql = "update student2 set stuname = ?,sex = ?,claid = ? where stuid = ?";
Object[] obj = {st.getStuname(),st.getSex(),st.getClaid(),st.getStuid()};
DBManager db = new DBManager();
int i = db.executeUpdate(sql, obj);
return i;
}
public Student2 findById(int id){
String sql = "select * from student2 where stuid = ?";
DBManager db = new DBManager();
Object[] obj = {id};
StudentMapper sm = new StudentMapper();
List list = db.executeQuery(sql, obj,sm);
if(list.size()!=0){
return list.get(0);
}else{
return null;
}
}
public List findAll(){
String sql = "select * from student2 order by(stuid)";
DBManager db = new DBManager();
StudentMapper sm = new StudentMapper();
List list = db.executeQuery(sql, null, sm);
return list;
}
}
package com.show;
import java.util.List;
import javax.swing.JOptionPane;
import com.add.AddStudent;
import com.dao.ClazzDao;
import com.dao.StudentDao;
import com.update.UpdateStduent;
import com.vo.Clazz;
import com.vo.Student2;
public class ShowStudent extends javax.swing.JFrame {
public ShowStudent() {
initComponents();
this.setLocationRelativeTo(null);
}
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
add = new javax.swing.JButton();
delete = new javax.swing.JButton();
update = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
sd = new StudentDao();
List list = sd.findAll();
Object[][] obj = new Object[list.size()][];
for(int i = 0;i
private void addActionPerformed(java.awt.event.ActionEvent evt) {
AddStudent as = new AddStudent();
as.setVisible(true);
as.setLocationRelativeTo(null);
setVisible(false);
}
private void deleteActionPerformed(java.awt.event.ActionEvent evt) {
int j = jTable1.getSelectedRow();//获取到选中的行
int id = (Integer) jTable1.getValueAt(j, 0);//获取选中行中id对应单元格的值
int i = sd.delete(id);
if(i>=0){
System.out.println("删除成功");
ShowStudent sst = new ShowStudent();
sst.setVisible(true);
sst.setLocationRelativeTo(null);
setVisible(false);
}else{
JOptionPane.showMessageDialog(null, "删除失败");
}
}
private void updateActionPerformed(java.awt.event.ActionEvent evt) {
int j = jTable1.getSelectedRow();
int id = (Integer) jTable1.getValueAt(j,0);
UpdateStduent us = new UpdateStduent(id);
us.setVisible(true);
us.setLocationRelativeTo(null);
setVisible(false);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ShowStudent().setVisible(true);
}
});
}
private javax.swing.JButton add;
private javax.swing.JButton delete;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JButton update;
private StudentDao sd;
}
package com.add;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import com.dao.ClazzDao;
import com.dao.StudentDao;
import com.show.ShowStudent;
import com.vo.Clazz;
import com.vo.Student2;
public class AddStudent extends JFrame{
public JLabel label1 = new JLabel();
public JLabel id1 = new JLabel("学号:");
public JLabel name1 = new JLabel("姓名:");
public JLabel sx1 = new JLabel("性别:");
public JLabel claname1 = new JLabel("班级:");
public JTextField id = new JTextField();
public JTextField name = new JTextField();
public JComboBox sx = new JComboBox();
public JComboBox claname = new JComboBox();
public JButton insert = new JButton("插入");
public AddStudent (){
this.setVisible(true);
this.setSize(400,340);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);//设置居中显示
this.setTitle("学生管理系统-添加");
this.setResizable(false);
this.setLayout(null);
label1.setText("请填写你要插入的信息");
this.add(label1);
label1.setBounds(100,30,200,30);//横向起始点,纵向起始点,宽度,高度
add(id1);
id1.setBounds(60,70,60,30);
add(id);
id.setBounds(130,70,150,30);
add(name1);
name1.setBounds(60,120,60,30);
add(name);
name.setBounds(130,120,150,30);
add(sx1);
sx1.setBounds(60,170,60,30);
add(sx);
sx.setBounds(130,170,150,30);
sx.setModel(new DefaultComboBoxModel(new String[]{"男","女"}));
add(claname1);
claname1.setBounds(60,220, 60, 30);
add(claname);
claname.setBounds(130,220,150,30);
ClazzDao cd = new ClazzDao();
List list = cd.findAll();
Object[] items = new Object[list.size()];
for(int i = 0;i=0){
System.out.println("添加成功");
ShowStudent stt = new ShowStudent();
stt.setVisible(true);
stt.setLocationRelativeTo(null);
setVisible(false);
}else{
System.out.println("添加失败");
}
}
});
}
public static void main(String []args){
AddStudent as = new AddStudent();
}
}
package com.update;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import com.dao.ClazzDao;
import com.dao.StudentDao;
import com.show.ShowStudent;
import com.vo.Clazz;
import com.vo.Student2;
public class UpdateStduent extends JFrame{
public JLabel label1 = new JLabel();
public JLabel name1 = new JLabel("姓名:");
public JLabel sx1 = new JLabel("性别:");
public JLabel claname1 = new JLabel("班级:");
public JTextField name = new JTextField();
public JComboBox sx = new JComboBox();
public JComboBox claname = new JComboBox();
public JButton update = new JButton("修改");
public Student2 st;
public UpdateStduent(int id){
this.setVisible(true);
this.setSize(400,360);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);//设置居中显示
this.setTitle("学生管理系统-修改");
this.setResizable(false);
this.setLayout(null);
label1.setText("请填写你要修改的信息");
this.add(label1);
label1.setBounds(120,30,200,30);//横向起始点,纵向起始点,宽度,高度
add(name1);
name1.setBounds(60,90,60,30);
add(name);
name.setBounds(130,90,150,30);
add(sx1);
sx1.setBounds(60,150,60,30);
StudentDao sd = new StudentDao();
st = sd.findById(id);
name.setText(st.getStuname());
add(sx);
sx.setBounds(130,150,150,30);
sx.setModel(new DefaultComboBoxModel(new String[]{"男","女"}));
if(st.getSex().equals("男")){
sx.setSelectedIndex(0);//显示未改之前的性别
}else{
sx.setSelectedIndex(1);
}
add(claname1);
claname1.setBounds(60,210, 60, 30);
add(claname);
claname.setBounds(130,210,150,30);
ClazzDao cd = new ClazzDao();
List list = cd.findAll();
Object[] items = new Object[list.size()];
for(int i = 0;i=0){
System.out.println("修改成功");
ShowStudent stt = new ShowStudent();
stt.setVisible(true);
stt.setLocationRelativeTo(null);
setVisible(false);
}else{
JOptionPane.showMessageDialog(null, "修改失败");
}
}
});
}
}