基于mysql数据库支持的留言板的实例

基于mysql数据库支持的留言板的实例

 

一、创建一个web工程名称:manhongliu

启动MyEclipse—New—WebProject—Project Name:manhongliu—Java EE 5.0—Finish--复制数据库驱动程序mysql-connector-java-5.1.6-bin.jar到工程中添加到内路径:将鼠标放到驱动程序上右键—Build Path—Add to Build Path

二、创建数据库zhoulanzhen创建表usertable

(1)启动Mysql—创建数据库zhoulanzhencreate database zhoulanzhen;

(2)创建表:usertablecreate table usertable(id int(11) auto_increment primary key, name varchar(20) not null,phone varchar(20) not null,email varchar(20), title varchar(40) not null, content varchar(200) not null, time datetime);

 

 

三、新建表单AddMessage.htm文件

启动Dreamweaver—新建htm文件制作表单:各控件的名称是它们自己,如:姓名是name

存盘到:manhongliuWebRoot--名字:AddMessage.htm—添加action=”/manhongliu/servlet/AddMessageServlet”这是添加到数据库的servlet

四、新建添加留言内容到数据库的servlet名称:AddMessageServlet.

New—Servlet—Package:com.manhongliu—Name:AddMessageServlet—Create doPost—Next—Servlet/JSP Mapping URL: /servlet/AddMessageServlet—Finish—编辑AddMessageServlet如下:

package com.manhongliu;

 

import java.io.IOException;

import java.io.PrintWriter;

import java.sql.Connection;

import java.sql.PreparedStatement;

 

import java.sql.SQLException;

import java.sql.Timestamp;

 

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

public class AddMessageServlet extends HttpServlet

    {       //1

 

    private static final long serialVersionUID = 2059282498034052536L;

 

      

       public void doPost(HttpServletRequest request,

              HttpServletResponse response) throws ServletException,IOException

           {      //2

              request.setCharacterEncoding("utf-8");

                response.setCharacterEncoding("utf-8");

                response.setContentType("text/html;charset=utf-8");

              PrintWriter out = response.getWriter();

              int result8=0;

          

              out.println("");

              out.println("  A POST Servlet");

              out.println("  ");

             

              String driver="com.mysql.jdbc.Driver"; 

              String rul="jdbc:mysql://localhost:3306/zhoulanzhen?";

              String username="root";

              String password="root";

              String sql="insert into usertable(name,phone,email,title,content,time) values(?,?,?,?,?,?)";

          

   

               try

                     {    //3

                      Class.forName(driver);

                         Connection conn= java.sql.DriverManager.getConnection (rul,username,password);

                        PreparedStatement pstmt=conn.prepareStatement(sql);

                          

                        pstmt.setString(1, StringUtil.filterHtml(request.getParameter("name")));

                      pstmt.setString(2, StringUtil.filterHtml(request.getParameter("phone")));

                      pstmt.setString(3, StringUtil.filterHtml(request.getParameter("email")));

                      pstmt.setString(4, StringUtil.filterHtml(request.getParameter("title")));

                      pstmt.setString(5, StringUtil.filterHtml(request.getParameter("content")));

                      pstmt.setTimestamp(6, new Timestamp(System.currentTimeMillis()));

                      result8= pstmt.executeUpdate();

                      

                      out.println(result8);

                      if(result8==0)

                            {

                                out.println("对不起,留言不成功");

                            }

                         else{

                            out.println("添加成功");

                            out.println("查看留言内容");

                             } 

 

                      

                      pstmt.close();

                       conn.close();

                       out.flush();

                       out.close();

                         

                     }  //3

                     catch (ClassNotFoundException e)

                         {  //4

                            e.printStackTrace();

                         }  //4

                     catch (SQLException e)

                         {  //5

                            e.printStackTrace();

                         }   //5

                    

                    

                      out.println("");

                      out.println("");

                       

             }   //2

 

    }     //1

 

 

五、创建一个工具类StringUtil包名com.manhongliu起过滤字符的作用,程序代码:

package com.manhongliu;

public class StringUtil

{

 

    public static String filterHtml(String value){

                  value=value.replaceAll("&", "&");

                  value=value.replaceAll("<", "<");

                  value=value.replaceAll(">", ">");

                  value=value.replaceAll(" ", " ");

                  value=value.replaceAll("'", "'");

                  value=value.replaceAll("/"", """);

                  value=value.replaceAll("/n", "
"
);

                  return value;

                }

 

}

 

六、新建显示留言内容的servlet名称:GetMessageServlet其程序如下:

package com.manhongliu;

 

import java.io.IOException;

import java.io.PrintWriter;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

public class GetMessageServlet extends HttpServlet

    {

 

    private static final long serialVersionUID = -2884268876112198563L;

 

        public void doGet(HttpServletRequest request,

              HttpServletResponse response) throws ServletException,IOException

           {

 

              request.setCharacterEncoding("gb2312");

              response.setCharacterEncoding("gb2312");

              response.setContentType("text/html;charset=gb2312");

              PrintWriter out=response.getWriter();

             

              String driver="com.mysql.jdbc.Driver"; 

               String rul="jdbc:mysql://localhost:3306/zhoulanzhen?";

               String username="root";

               String password="root";

               String sql="select * from usertable";

             

              out.println("");

              out.println("display Message");

              out.println("");

             

              try

                  {

                   Class.forName(driver);

                     Connection conn= java.sql.DriverManager.getConnection (rul,username,password);

                     PreparedStatement pstmt=conn.prepareStatement(sql);

                     ResultSet rs=pstmt.executeQuery();

                     //5.显示结数据库里的数据

                     while(rs.next()){

                                 printRow(out,rs);

                                         }

 

 

                   rs.close();

                   pstmt.close();

                    conn.close();

                    out.flush();

                    out.close();

 

                  }

              catch (ClassNotFoundException e)

                  {

                     e.printStackTrace();

                  }

              catch (SQLException e)

                  {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

                  } 

           }

        public void printRow(PrintWriter out,ResultSet rs)throws SQLException

        {

               out.println("

");      

               out.println("

");                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 

               out.println("

");

               out.println("

");                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              

               out.println("

");  

               out.println("

");                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              

               out.println("

");

               out.println("

");                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              

               out.println("

");

               out.println("

");                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              

               out.println("

");

               out.println("

");                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              

               out.println("

");  

               out.println("

");                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          

               out.println("

");

                out.println("

编号: "+rs.getInt("id")+"
姓名: "+rs.getString("name")+"
电话: "+rs.getString("phone")+"
邮箱: "+rs.getString("email")+"
主题: "+rs.getString("title")+"
内容: "+rs.getString("content")+"
时间: "+rs.getString("time")+"
");

        }

    }

 

七、部署运行

 

 http://localhost:8080/manhongliu/AddMessage.htm

 

                           

你可能感兴趣的:(基于mysql数据库支持的留言板的实例)