1. 程式人生 > 程式設計 >JavaWeb實現學生資訊管理系統(1)

JavaWeb實現學生資訊管理系統(1)

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

  • 連線池
  • DBUtils
  • P、EL、JSTL
  • MVC設計模式

Web之簡單的學生資訊管理系統(一)
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. 專案框架

JavaWeb實現學生資訊管理系統(1)

3. 匯入專案需要的jar包

JavaWeb實現學生資訊管理系統(1)

4. C3P0的配置檔案c3p0-config.xml

<c3p0-config>
  <default-config>
    <property name="driverClass">com..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 nam
e="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.D客棧ate;

/**
 * 這是封裝的學生物件Bean
 * @author Administrator
 *
 */
public class Student {
 
 private int sid;
 private String sname;
 private http://www.cppcns.comString 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.printStackThttp://www.cppcns.comrace();
  }finally{
   conn = null;
  }
 }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。