JavaWeb實現學生信息管理系統(1)

這是一個很簡單的學生信息管理系統,會用到很多小知識,比如說:

  • 數據庫連接池
  • DBUtils
  • JSP、EL、JSTL
  • MVC設計模式

JavaWeb之簡單的學生信息管理系統(一)
JavaWeb之簡單的學生信息管理系統(二)
JavaWeb之簡單的學生信息管理系統(三)

一、需求分析

實現一個簡單的學生信息管理系統,具體實現功能有如下:

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. 項目框架

3. 導入項目需要的jar包

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;
  }
 }
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: