欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

JavaWeb实现学生信息管理系统(1)

时间:2021-11-27 13:46:20|栏目:JAVA代码|点击:

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

  • 数据库连接池
  • 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;
  }
 }
}

上一篇:JAVA DOM解析XML文件过程详解

栏    目:JAVA代码

下一篇:java发送email一般步骤(实例讲解)

本文标题:JavaWeb实现学生信息管理系统(1)

本文地址:http://www.codeinn.net/misctech/184997.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有