好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

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

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

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

一、需求分析

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

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

二、准备工作

1. 创建数据库stus以及创建数据库表stu

?

1

2

3

4

5

6

7

8

9

10

11

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

< 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】

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

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】

?

1

2

3

4

5

6

7

8

9

10

11

12

package com.util;

 

public class TestUtils {

  /**

   * 判断某一个字符串是否为空

   * @param s

   * @return

   */

  public static boolean isEmpty(String s) {

   return s == null || s.length() == 0 ;

  }

}

3. JDBCUtils02.java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

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 ;

   }

  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://blog.csdn.net/weixin_44270855/article/details/104236494

查看更多关于JavaWeb实现学生信息管理系统(1)的详细内容...

  阅读:32次