个人信息管理系统

这篇博客介绍了一个基于MySQL数据库的个人信息管理系统,包括学生管理模块,涵盖了学生信息的增删查改操作。系统使用Java编程,包含DatabaseConnection类用于数据库连接,Student类表示学生信息,StudentAction类处理业务逻辑,StudentServlet处理JSP与数据交互,同时文章提到了Markdown编辑器的相关功能和快捷键。
摘要由CSDN通过智能技术生成

个人信息管理系统

  1. 链接数据库
    package com.student.dbc ;
    import java.sql.* ;
    public class DatabaseConnection {
    private static final String DBDRIVER = “org.gjt.mm.mysql.Driver” ;
    private static final String DBURL = “jdbc:mysql://localhost:3306/java_web?useUnicode=true&characterEncoding=UTF-8” ;
    private static final String DBUSER = “root” ;
    private static final String DBPASSWORD = “root” ;
    private Connection conn = null ;
    public DatabaseConnection() throws Exception{
    try{
    Class.forName(DBDRIVER) ;
    this.conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
    }catch(Exception e){
    throw e ;
    }
    }
    public Connection getConnection(){
    return this.conn ;
    }
    public void close() throws Exception{
    if(this.conn != null){
    try{
    this.conn.close() ;
    }catch(Exception e){
    throw e ;
    }
    }
    }
    }

)

2.个人
package com.student.vo;

public class Student {
private String id;
private String name;
private int age;
private int sex;
private String major;
private String college;
private String introduction;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
public String getIntroduction() {
return introduction;
}
public void setIntroduction(String introduction) {
this.introduction = introduction;
}

}
3.信息增删查改
package com.student.action;package com.student.action;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.mysql.jdbc.Connection;
import com.student.dbc.DatabaseConnection;
import com.student.vo.Student;
public class StudentAction {

private static Connection conn = null ;


/**
 * 增加学生
 * @param id
 * @param name
 * @param age
 * @param sex
 * @param major
 * @param college
 * @param introduction
 * @return
 */
public static boolean addStudent(String id,String name,int age,int sex,String major,String college,String introduction) {

    try {
        java.sql.Connection conn=new DatabaseConnection().getConnection();
        PreparedStatement st=conn.prepareStatement("insert into student values(?,?,?,?,?,?,?)");


        st.setString(1, id);
        st.setString(2, name);
        st.setInt(3, age);
        st.setInt(4, sex);
        st.setString(5, major);
        st.setString(6, college);
        st.setString(7, introduction);
        st.execute();
        conn.close();
        return true;
    } catch (Exception e) {
        // TODO: handle exception
        return false;
    }
}
/**
 * 更新学生
 * @param id
 * @param name
 * @param age
 * @param sex
 * @param major
 * @param college
 * @param introduction
 * @return
 */
public static boolean updateStudent(String id,String name,int age,int sex,String major,String college,String introduction) {
    try {
        java.sql.Connection conn=new DatabaseConnection().getConnection();
        PreparedStatement st=conn.prepareStatement("update student set name=?,age=?,sex=?,major=?,college=?,introduction=? where id=?");
        st.setString(1, name);
        st.setInt(2, age);
        st.setInt(3, sex);
        st.setString(4, major);
        st.setString(5, college);
        st.setString(6, introduction);
        st.setString(7, id);

        st.execute();
        conn.close();
        return true;
    } catch (Exception e) {
        // TODO: handle exception
        return false;
    }
}
/**
 * 删除
 * @param id
 * @return
 */
public static boolean deleteStudent(String id) {
    try {
        java.sql.Connection conn=new DatabaseConnection().getConnection();
        PreparedStatement st=conn.prepareStatement("delete from student where id=?");
        st.setString(1, id);
        st.execute();
        conn.close();
        return true;
    }catch (Exception e) {
        // TODO: handle exception
        return false;
    }
}
/**
 * 获取全部学生
 * @return
 */

public static  ArrayList getAllstudent() {
    ArrayList students=new ArrayList();

    try {
        java.sql.Connection conn=new DatabaseConnection().getConnection();
        PreparedStatement st=conn.prepareStatement("select * from student");
        st.execute();
        ResultSet rs=st.getResultSet();
        while(rs.next()){
            Student student=new Student();
            student.setId(rs.getString("id"));
            student.setName(rs.getString("name"));
            student.setAge(rs.getInt("age"));
            student.setSex(rs.getInt("sex"));
            student.setMajor(rs.getString("major"));
            student.setCollege(rs.getString("college"));
            student.setIntroduction(rs.getString("introduction"));
            students.add(student);
        }
        conn.close();
    } catch (Exception e) {
        // TODO: handle exception
    }
    return students;
}
/**
 * 按学号查询学生
 * @param id
 * @return
 */
public static  Student getStudent(String id) {

    Student student=null;
    try {
        java.sql.Connection conn=new DatabaseConnection().getConnection();
        PreparedStatement st=conn.prepareStatement("select * from student where id=?");
        st.setString(1,    id);
        st.execute();
        ResultSet rs=st.getResultSet();
        while(rs.next()){
            student=new Student();

            student.setId(rs.getString("id"));
            student.setName(rs.getString("name"));
            student.setAge(rs.getInt("age"));
            student.setSex(rs.getInt("sex"));
            student.setMajor(rs.getString("major"));
            student.setCollege(rs.getString("college"));
            student.setIntroduction(rs.getString("introduction"));
        }
        conn.close();
    } catch (Exception e) {
        // TODO: handle exception
    }
    return student;
}

}
4.JSP与数据交换层
package com.student.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.student.action.StudentAction;
public class StudentServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding(“utf-8”);

    if(request.getRequestURI().endsWith("/viewStudent")){
        RequestDispatcher dispatcher = request.getRequestDispatcher("viewstudent.jsp");
        dispatcher .forward(request, response);
    }else if(request.getRequestURI().endsWith("/addStudent")){

        doAddStudent(request,response);
    }
    else if (request.getRequestURI().endsWith("/updateStudent")) {
        doUpdateStudent(request,response);
    }else if (request.getRequestURI().endsWith("/deleteStudent")) {
        doDeleteStudent(request,response);
    }
}
private void doAddStudent(HttpServletRequest request, HttpServletResponse response) throws IOException{
    String id=request.getParameter("id");
    String name=request.getParameter("name");
    String age=request.getParameter("age");
    String sex=request.getParameter("sex");
    String major=request.getParameter("major");
    String college=request.getParameter("college");
    String introduction=request.getParameter("introduction");
    StudentAction.addStudent(id, name,new Integer(age), new Integer(sex), major, college, introduction);
    response.sendRedirect("index.jsp");
}
private void doUpdateStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String id=request.getParameter("id");
    String name=request.getParameter("name");
    String age=request.getParameter("age");
    String sex=request.getParameter("sex");
    String major=request.getParameter("major");
    String college=request.getParameter("college");
    String introduction=request.getParameter("introduction");
    StudentAction.updateStudent(id, name, new Integer(age), new Integer(sex), major, college, introduction);
    response.sendRedirect("index.jsp");
}
private void doDeleteStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String id=request.getParameter("id");
    StudentAction.deleteStudent(id);
    response.sendRedirect("index.jsp");
}

}
5.JSP页面
(1)index
<%@page import=“com.student.vo.Student”%>
<%@ page language=“java” import=“java.util.*” pageEncoding=“utf-8”%>
<%@ page import=“com.student.action.StudentAction”%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"😕/"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<title>学生管理系统</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<link id="bs-css" href="css/bootstrap-cerulean.min.css" rel="stylesheet">
<link href="css/charisma-app.css" rel="stylesheet">

学生管理系统

<% ArrayList students=StudentAction.getAllstudent(); for(int i=0;i
    学号     姓名     年龄     性别     专业     学院     简介     操作
(2)addstudent <%@page import="com.student.vo.Student"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page import="com.student.action.StudentAction"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
<title>添加学生信息</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
    <link id="bs-css" href="css/bootstrap-cerulean.min.css" rel="stylesheet">
<link href="css/charisma-app.css" rel="stylesheet">    

学生信息

--------性别--------男女
提交
(3)updatestudent <%@page import="com.student.vo.Student"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page import="com.student.action.StudentAction"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
<title>添加学生信息</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
    <link id="bs-css" href="css/bootstrap-cerulean.min.css" rel="stylesheet">

<link href="css/charisma-app.css" rel="stylesheet">
<% String id=request.getParameter("id"); Student student=StudentAction.getStudent(id); %>

修改学生信息

<% if(student.getSex()==1){%> 男<%}else{ %> 女 <%} %> --------性别--------男女
                    <label>学院</label>  <input type="text" class="form-control"  name="college" value="<%=student.getCollege()%>">
  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值