欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JavaWeb之简单的学生信息管理系统(一)

程序员文章站 2022-05-06 18:18:33
...

这是一个很简单的学生信息管理系统,会用到很多小知识,比如说:

  • 数据库连接池
  • DBUtils
  • JSP、EL、JSTL
  • MVC设计模式

一、需求分析

实现一个简单的学生信息管理系统,具体实现功能有如下:

  1. 查询学生的息并列表展示
  2. 添加学生信息
  3. 删除学生信息
  4. 更新(修改)学生信息
  5. 模糊查询

二、准备工作

1. 创建数据库stus以及创建数据库表stu
CREATE DATABASE stus;
	USE stus;
	CREATE TABLE stu (
		sid INT PRIMARY KEY  AUTO_INCREMENT,
		sname VARCHAR (20),
		gender VARCHAR (5),
		phone VARCHAR (20),
		birthday DATE,
		hobby VARCHAR(50),
		info VARCHAR(200)
	);
2. 项目框架

JavaWeb之简单的学生信息管理系统(一)

3. 导入项目需要的jar包

JavaWeb之简单的学生信息管理系统(一)

4. C3P0的配置文件c3p0-config.xml
<c3p0-config>
  <default-config>
    <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost/stus?useUnicode=true&amp;characterEncoding=utf8&amp;serverTimezone=GMT</property>
    <property name="user">root</property>
    <property name="password">root</property>
    
    <property name="initialPoolSize">10</property>
    <property name="maxIdleTime">30</property>
    <property name="maxPoolSize">100</property>
    <property name="minPoolSize">10</property>
    <property name="maxStatements">200</property>
  </default-config>

</c3p0-config>

三、代码准备

1. 实现Student类

【备:com.domain包下的Student.java】

package com.domain;

import java.util.Date;

/**
 * 这是封装的学生对象Bean
 * @author Administrator
 *
 */
public class Student {
	
	private int sid;
	private String sname;
	private String gender;
	private String phone;
	private String hobby;
	private String info;
	private Date birthday; 
	
	public Student() {
		super();
	}
	
	public Student(String sname, String gender, String phone, String hobby, String info, Date birthday) {
		super();
		this.sname = sname;
		this.gender = gender;
		this.phone = phone;
		this.hobby = hobby;
		this.info = info;
		this.birthday = birthday;	
	}
	
	public int getSid() {
		return sid;
	}
	public void setSid(int sid) {
		this.sid = sid;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getHobby() {
		return hobby;
	}
	public void setHobby(String hobby) {
		this.hobby = hobby;
	}
	public String getInfo() {
		return info;
	}
	public void setInfo(String info) {
		this.info = info;
	}
	

}

2. 写一个类,判断字符串是否相等【TestUtils.java】
package com.util;

public class TestUtils {
	/**
	 * 判断某一个字符串是否为空
	 * @param s
	 * @return
	 */
	public static boolean isEmpty(String s) {
		return s == null || s.length() == 0;
	}
}

3. JDBCUtils02.java
package com.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JDBCUtil02 {
	
	static ComboPooledDataSource dataSource = null;
	static{
		dataSource = new ComboPooledDataSource();
	}
	
	public static DataSource getDataSource() {
		return dataSource;
	}
	
	/**
	 * 获得连接对象
	 * @return
	 * @throws SQLException 
	 */
	public static Connection getConn() throws SQLException{
		return dataSource.getConnection();
	}
	
	/**
	 * 释放资源
	 * @param conn
	 * @param st
	 * @param rs
	 */
	public static void release(Connection conn , Statement st , ResultSet rs){
		closeRs(rs);
		closeSt(st);
		closeConn(conn);
	}
	public static void release(Connection conn , Statement st){
		closeSt(st);
		closeConn(conn);
	}

	
	private static void closeRs(ResultSet rs){
		try {
			if(rs != null){
				rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			rs = null;
		}
	}
	
	private static void closeSt(Statement st){
		try {
			if(st != null){
				st.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			st = null;
		}
	}
	
	private static void closeConn(Connection conn){
		try {
			if(conn != null){
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			conn = null;
		}
	}
}