jsp----jdbc实现留言板功能

要求:

创建用户留言模块,页面有登陆,注册,查看留言列表,查看详细留言,发布留言,安全退出等功能。先创建数据库,数据库中至少要有users和words两个表,其他可以自行设计

数据库表设计

CREATE TABLE users (
  UserID bigint(20) NOT NULL auto_increment,
  UserName varchar(20) default NULL,
  UserPassword varchar(20) default NULL,
  PRIMARY KEY (UserID)
) ;
CREATE TABLE words (
  WordsID bigint(20) NOT NULL auto_increment,
  WordsTitle varchar(100) default NULL,
  WordsContent text,
  WordsTime datetime default '0000-00-00 00:00:00',
  UserID bigint(20) NOT NULL default '0',
  PRIMARY KEY  (WordsID)
 ) ;

jsp代码

下面展示一些 关键代码
write_message.jsp

//表单提交过来的数据
String title = request.getParameter("title");
String content = request.getParameter("content");
int id = (Integer)session.getAttribute("UID");
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


//写入数据到数据库
conn = DriverManager.getConnection(url,userName,password);
stmt=conn.createStatement();
sql="INSERT INTO words (WordsTitle,WordsContent,WordsTime,UserID) values('"+title+"','"+content+"','"+df.format(new Date())+"','"+id+"')";
if(title!=null&&content!=null&&!title.equals("")&&!content.equals("")){
stmt.executeUpdate(sql);

show.jsp

<%
String url = "jdbc:mysql://localhost/message_board";
String userName = "root";
String passWord = "123";
String sql = null;
Connection conn = null;
Statement stmt = null;
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
out.println("加载驱动器时出现异常");
}

try{
conn = DriverManager.getConnection(url,userName,passWord);
//创建 Statement 语句
 stmt = conn.createStatement();
 sql = "SELECT * FROM words";
 ResultSet rs = stmt.executeQuery(sql);
 while(rs.next()){
   int id= rs.getInt(1);
   String word_title = rs.getString(2);
   String word_content = rs.getString(3);
   Date data = rs.getDate(4);
   Time time=rs.getTime(4);
   int id_user = rs.getInt(5);
   %>
   <h1 style="color:white;"><% out.print(id);%></h1>
   <h3 style="color:yellow">留言者ID:<%=id_user%></h3>
    <font size="4" color="gray">标题:<%out.print(word_title); %></font><br>
    <font size="3" color="blue">内容:</font><br>
    <font size="3" color="white"><%out.print(word_content); %></font><br>
    <br>
        <font size="2" color="red"><%out.print("时间:"+data+"  "+time); %></font><br>
    <font size="1" color="yellow"><% out.print("******************************************"); %></font><br>
   <%
 out.print("a");
  }
rs.close();
stmt.close();

}catch(SQLException e){
out.println("连接数据库的过程中出现SQL异常");
}finally{
try{
if(conn!=null)
conn.close();

}catch(SQLException e){
out.println("关闭数据库时出现SQl异常");
}
}
%>

效果展示

**注册 **
登陆
在这里插入图片描述
在这里插入图片描述写入

在这里插入图片描述展示
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值