一.创建数据库
1).打开Access2000,创建一个新的数据库,我将这个数据库命名为 foxdb.mdb,存在C:\tomcat\fox\global\foxdb.mdb。接下来在 eagle.mdb中创建一个表,命名为 foxtable,表中有五个字段,全为文本格式:
其中“URL”用于记录留言者的 IP 。至于各字段的长度,我把“留言”定为200,其它四个各为20。
2).指定ODBC数据源,其名为foxdb ,指向 C:\tomcat\fox\global\foxdb.mdb。
二.编写用户的留言界面 foxnote.html,存于C:\tomcat\fox\foxnote.html:
<html>
<body>
<form method="post" action="foxnoteinsert.jsp">
姓名:
<input name=username size=15 value="">
邮箱:
<input name=email size=15 value="">
留言:
<textarea name=doc rows="5" cols="40">
</textarea>
<input type=submit value="递交">
<input type=reset value="重填">
</form>
</bocy>
</html>
在IE中键入 http://ip/fox/foxnote.html 看看是否显示正常(ip是你机器的ip地址)
三.编写 foxnoteinsert.jsp ,将用户的留言写进数据库表中:
<body bgcolor="#FFFFFF">
<%@ page import="java.sql.*,MyUtil,java.util.*"%>
<%
Connection con=null;
String username=MyUtil.gb2312ToUnicode(request.getParameter("username"));
String email=MyUtil.gb2312ToUnicode(request.getParameter("email"));
String doc=MyUtil.gb2312ToUnicode(request.getParameter("doc"));
String url=request.getRemoteAddr();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:foxdb","","");
String str="insert into foxtable values(?,?,?,?);";
PreparedStatement pstmt=con.prepareStatement(str);
pstmt.setString(1,username);
pstmt.setString(2,email);
pstmt.setString(3,doc);
pstmt.setString(4,url);
pstmt.executeUpdate();
pstmt.close();
con.close();
}
catch(Exception e) {
out.println(e.getMessage());
}
%>
这个程序中有一些要说明的地方,就是其中用到了一个 JavaBean :MyUtil.class 。
MyUtil 的作用是字符串之间的转换。必需关注的是JSP的字符串以Unicode码表示,而留言板界面的表单却是以 gb2312 码表示。所以将用户的留言写进数据库还需要码间的转换。如果不转换而把留言直接写到数据库表,则会产生乱码。下面是 MyUtil 的原代码,存于C:\tomcat\fox\WEB-INF\classes\MyUtil.java ,编译后的MyUtil.class文件也存于此。
import java.io.*;
public class MyUtil{
public static String gb2312ToUnicode(String s){
try{
return new String(s.getBytes("ISO8859_1"),"gb2312");
}
catch(UnsupportedEncodingException uee){
return s;
}
}
public static String unicodeTogb2312(String s){
try{
return new String(s.getBytes("gb2312"),"ISO8859_1");
}
catch(UnsupportedEncodingException uee){
return s;
}
}
}
四.编写 foxnoteview.jsp ,用于浏览数据库表中已有的留言,存于C:\tomcat\fox\foxnoteview.jsp ,代码如下:
<html>
<body>
<%@ page contentType="text/html;charset=gb2312" language="java" import="java.sql.*"%>
<%
Connection con=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:foxdb","","");
Statement statement=con.createStatement();
ResultSet rs=statement.executeQuery("select * from foxtable");
%>
<table border="1" width="100%" cellspacing="0" cellpadding="0" align="center" bordercolorlight="#CCCCFF" bordercolordark="#FFFFFF">
<tr bgcolor="#FFFFFF">
<td width="15%" height="25" align="center"><i>作者</i></td>
<td width="28%" height="25" align="center"><i>发表时间</i></td>
<td width="22%" height="25" align="center"><i>Email</i></td>
<td width="35%" height="25" align="center"><i>留言内容</i></td>
<%
while(rs.next()){
out.println("<TR><td align=center><font size=2 color=#999999>"+rs.getString("作者")+"</TD>");
out.println("<TD><font size=2color=#999999>"+rs.getString("Email")+"</font></TD>");
out.println("<TD><font size=2 color=#999999>"+rs.getString("留言")+"</font></TD>");
out.println("<TD><font size=2 color=#999999>"+rs.getString("URL")+"</font></TD></TR>");
}
rs.close();
con.close();
}
catch(Exception e)
{
out.println(e.getMessage());
}
%>
</table>
</body>
</html>
到此,整个留言板程序就算是完工了。留言板在数据库应用中比较简单,但是加以变化则可以编写出各种各样的应用程序,操作各种各样的数据库^_^