使用JavaBean规范,编写Guestbook类和MySQLUtil类,在MessageArchiever.jsp中使用getter和setter进行值传递。

Guestbook.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
package koi;

public class Guestbook {
	private String name;
	private String email;
	private String phone;
	private String topic;
	private String content;
	private Integer id;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getTopic() {
		return topic;
	}

	public void setTopic(String topic) {
		this.topic = topic;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

}

MySQLUtil.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
package koi;

import java.sql.SQLException;
import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

public class MySQLUtil {
	private String dataSourceName;
	private DataSource dSource;
	
	public MySQLUtil(String dataSourceName) {
		this.dataSourceName = dataSourceName;
	}
	
	public MySQLUtil() {
	}

	public void setDataSourceName(String dataSourceName) {
		this.dataSourceName = dataSourceName;
	}
	
	public void init() {
		Context initContext;
		try {
			initContext = new InitialContext();
			dSource = (DataSource)initContext.lookup(dataSourceName);
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}
	
	public int update(String sql, Object[] param) {
		int result = 0;
		QueryRunner qRunner = new QueryRunner(dSource);
		try {
			result = qRunner.update(sql, param);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public Object query(String sql, Object[] param, ResultSetHandler rSetHandler) {
		QueryRunner qRunner = new QueryRunner(dSource);
		Object result = null;
		try {
			result = qRunner.query(sql, param, rSetHandler);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
}

MessageArchiever.jsp

 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
<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ page
	import="java.text.SimpleDateFormat,koi.StringUtil"%>
<%@ include file="/header.jsp"%>
<jsp:useBean id="guestbook" class="koi.Guestbook"></jsp:useBean>
<jsp:useBean id="mysqlutil" class="koi.MySQLUtil"></jsp:useBean>
<jsp:setProperty property="*" name="guestbook"/>
<%
	int result = 0;

	// 设置编码
	request.setCharacterEncoding("utf-8");
	// 要执行的SQL语句
	String sql = "insert into book(name,email,phone,title,content,publishtime) values(?,?,?,?,?,?)";

	String name = guestbook.getName();
	String email = guestbook.getEmail();
	String phone = guestbook.getPhone();
	String topic = guestbook.getTopic();
	String content = guestbook.getContent();
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

	Object[] param = { name, email, phone, topic, content, sdf.format(new java.util.Date()) };

	mysqlutil.setDataSourceName("java:/comp/env/jdbc/mysql");
	mysqlutil.init();		
	result = mysqlutil.update(sql, param);
	response.getWriter().append(Integer.toString(result));
%>
<%@ include file="/footer.jsp"%>