项目开发的准备工作已经差不多了,现在需要的是与数据表对应的数据类。
1. 数据类
按照编程习惯首先创建package,项目名称右键选择New->Java package; package name填写com.blog
在com.blog下面创建与数据表对应的Blog,Comment,Category以及User这4个类
blog.java
1 /*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6 package com.blog;
7
8 import java.util.Date;
9
10 /**
11 *
12 * @author Chucky
13 */
14 public class Blog {
15 private int id;
16 private String title;
17 private String content;
18 private Date date;
19 private int categoryId;
20 private String category;
21
22 public String getCategory() {
23 return category;
24 }
25
26 public void setCategory(String category) {
27 this.category = category;
28 }
29
30 public int getCategoryId() {
31 return categoryId;
32 }
33
34 public void setCategoryId(int categoryId) {
35 this.categoryId = categoryId;
36 }
37
38 public String getContent() {
39 return content;
40 }
41
42 public void setContent(String content) {
43 this.content = content;
44 }
45
46 public Date getDate() {
47 return date;
48 }
49
50 public void setDate(Date date) {
51 this.date = date;
52 }
53
54 public int getId() {
55 return id;
56 }
57
58 public void setId(int id) {
59 this.id = id;
60 }
61
62 public String getTitle() {
63 return title;
64 }
65
66 public void setTitle(String title) {
67 this.title = title;
68 }
69
70 }
71
Category.java
1 /*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6 package com.blog;
7
8 /**
9 * This is category class which records id and name
10 * @author Chucky
11 */
12 public class Category {
13 private int id;
14 private String name;
15
16 public int getId() {
17 return id;
18 }
19
20 public void setId(int id) {
21 this.id = id;
22 }
23
24 public String getName() {
25 return name;
26 }
27
28 public void setName(String name) {
29 this.name = name;
30 }
31
32 }
33
Comment.java
1 /*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6 package com.blog;
7
8 import java.util.Date;
9
10 /**
11 * this is comment class which records id,name,content,date and blog_id
12 * @author Chucky
13 */
14 public class Comment {
15 private int id;
16 private String name;
17 private String content;
18 private Date date;
19 private int blog_id;
20
21 public int getBlog_id() {
22 return blog_id;
23 }
24
25 public void setBlog_id(int blog_id) {
26 this.blog_id = blog_id;
27 }
28
29 public String getContent() {
30 return content;
31 }
32
33 public void setContent(String content) {
34 this.content = content;
35 }
36
37 public Date getDate() {
38 return date;
39 }
40
41 public void setDate(Date date) {
42 this.date = date;
43 }
44
45 public int getId() {
46 return id;
47 }
48
49 public void setId(int id) {
50 this.id = id;
51 }
52
53 public String getName() {
54 return name;
55 }
56
57 public void setName(String name) {
58 this.name = name;
59 }
60
61 }
62
User.java
1 /*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6 package com.blog;
7
8 /**
9 *
10 * @author Chucky
11 */
12 public class User {
13 private int id;
14 private String userName;
15 private String password;
16
17 public int getId() {
18 return id;
19 }
20
21 public void setId(int id) {
22 this.id = id;
23 }
24
25 public String getPassword() {
26 return password;
27 }
28
29 public void setPassword(String password) {
30 this.password = password;
31 }
32
33 public String getUserName() {
34 return userName;
35 }
36
37 public void setUserName(String userName) {
38 this.userName = userName;
39 }
40
41 }
42
值得注意的是为了开发的方便,在Blog.java里与数据表不同,多了一个category属性。
2. Servlet
考虑到如果按照以后jsp的功能来写servlet的话,文件多了既混乱也不便于管理,所以按照数据类创建
BlogServlet(与blog相关的Servlet,如:添加,删除,修改,浏览,查询),
CommentServlet,
CategoryServlet和UserServlet,4个Servlet。
因为默认添加servlet信息到deployment文件,所以打开web.xml,相应的servlet信息已经被添加进去了。