教师信息管理系统开发案例及代码

教师管理数据库系统:
建立步骤:
1)导入jar包,建立数据库建立用到的一些基本信息表,与数据库连接。

2)将一些常用函数导入到DbOperator类中,形成一个抽象工具包!

package cn.edu.hpu.util;
public class DBOperator {
    public static final String DBDRIVER = "com.mysql.jdbc.Driver" ;
    public static final String DBDURL = "jdbc:mysql://localhost:3306/tennis" ;
    public static final String DBUSER = "root" ;
    public static final String DBPASS = "root" ;

    static
    {
        try {
            Class.forName(DBDRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection()
    {
        Connection coon = null;
        try {
            coon = DriverManager.getConnection(DBDURL,DBUSER,DBPASS);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return coon;
    }
    public static void close(ResultSet rs, Statement st, Connection conn)
    {
        try
        {
            if(rs != null)
            {
                rs.close();
            }
            if(st != null)
            {
                st.close();
            }
            if(conn != null)
            {
                conn.close();
            }

        }catch(Exception ex)
        {
            ex.printStackTrace();
        }

    }

    public static void close(Statement st, Connection coon)
    {
        close(null,st,coon);
    }


}

3)建立面向对象的 增删查修等用到的语句!
<1>建立接口

package cn.edu.hpu.service;

import java.util.List;

import cn.edu.hpu.model.TeacherStaff;

public interface TeacherStaffManager {

    public boolean add(TeacherStaff ts);

    public boolean del(int id);

    public boolean update(TeacherStaff ts);

    public List getTeacherStaffs();

    public TeacherStaff getTeacherStaffByName(String name);

    public TeacherStaff getTeacherStaffById(int id);

}

<2>实现接口中定义的所有的方法

package cn.edu.hpu.service;

import java.io.Closeable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import cn.edu.hpu.model.TeacherStaff;
import cn.edu.hpu.util.DbOperator;

public class TeacherStaffManagerImpl implements TeacherStaffManager{

    @Override
    public boolean add(TeacherStaff ts) {
        boolean flag=false;
        Connection con = null;
        PreparedStatement pst = null;

        String sql = "insert into teacherstaff values(?,?,?,?,?,?)";
        con = DbOperator.getConnection();
        try {
            pst = con.prepareStatement(sql);
            pst.setInt(1, ts.getId());
            pst.setString(2, ts.getName());
            pst.setString(3, ts.getSex());
            pst.setString(4, ts.getBirthdate());
            pst.setString(5, ts.getLocation());
            pst.setString(6, ts.getPosition());

            int row = pst.executeUpdate() ;
            if (row>0) {
                flag = true;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // TODO Auto-generated method stub
        return flag;
    }

    @Override
    public boolean del(int id) {
        boolean flag = false;
        Connection con = null;
        PreparedStatement pst = null;

        String sql = "delete from teacherstaff where id=?";
        con = DbOperator.getConnection();
        try {
            pst = con.prepareStatement(sql);
            pst.setInt(1, id);

            int row = pst.executeUpdate();
            if (row>0) {
                flag=true;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // TODO Auto-generated method stub
        return flag;
    }

    @Override
    public boolean update(TeacherStaff ts) {
        boolean flag = false;
        Connection con = null;
        PreparedStatement pst = null;

        String sql = "update teacherstaff set name=?,sex=?,birthdate=?,location=?,position=? where id=?";
        con = DbOperator.getConnection();
        try {
            pst = con.prepareStatement(sql);
            pst.setString(1, ts.getName());
            pst.setString(2, ts.getSex());
            pst.setString(3, ts.getBirthdate());
            pst.setString(4, ts.getLocation());
            pst.setString(5, ts.getPosition());
            pst.setInt(6, ts.getId());

            int row = pst.executeUpdate();
            if (row > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // TODO Auto-generated method stub
        return flag;
    }

    @Override
    public List getTeacherStaffs() {
        List list = new ArrayList();
        Statement st = null;
        Connection con = null;
        ResultSet rs = null;

        String sql = "select * from teacherstaff";
        con = DbOperator.getConnection();
        try {
            st = con.createStatement();
            rs = st.executeQuery(sql);
            while (rs.next()) {
                TeacherStaff ts = new TeacherStaff();
                ts.setId(rs.getInt("id"));
                ts.setName(rs.getString("name"));
                ts.setSex(rs.getString("sex"));
                ts.setBirthdate(rs.getString("birthdate"));
                ts.setLocation(rs.getString("location"));
                ts.setPosition(rs.getString("position"));
                list.add(ts);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // TODO Auto-generated method stub
        return list;
    }



    @Override
    public TeacherStaff getTeacherStaffByName(String name) {
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        TeacherStaff ts = new TeacherStaff();

        String sql = "select * from teacherstaff where name='"+name+"'";
        con = DbOperator.getConnection();
        try {
            st = con.createStatement();
            rs = st.executeQuery(sql);
            if (rs.next()) {
                ts.setId(rs.getInt("id"));
                ts.setName(rs.getString("name"));
                ts.setSex(rs.getString("sex"));
                ts.setBirthdate(rs.getString("birthdate"));
                ts.setLocation(rs.getString("location"));
                ts.setPosition(rs.getString("position"));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // TODO Auto-generated method stub
        return ts;
    }

    @Override
    public TeacherStaff getTeacherStaffById(int id) {
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        TeacherStaff ts = new TeacherStaff();

        String sql = "select * from teacherstaff where id="+id;
        con = DbOperator.getConnection();
        try {
            st = con.createStatement();
            rs = st.executeQuery(sql);
            if (rs.next()) {
                ts.setId(rs.getInt("id"));
                ts.setName(rs.getString("name"));
                ts.setSex(rs.getString("sex"));
                ts.setBirthdate(rs.getString("birthdate"));
                ts.setLocation(rs.getString("location"));
                ts.setPosition(rs.getString("position"));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // TODO Auto-generated method stub
        return ts;
    }

}

3)构建前台的网页对应的jsp(对应的网页代码太多不再一一显示)
jsp文件用于存放一些静态代码,也可以用来添加一些动态代码,与html是不同的! web.xml配置文件,是专门用来配置servlet文件的!
SQLYog 是MySQL 的图形化页面,最基础的调用还是 通过dos窗口(黑框);做应用程序的时候特别是web应用程序的时候要写好包名,跟架构有关系。

你可能感兴趣的:(导入,方法,系统,java基础开发)