要求:
创建用户留言模块,页面有登陆,注册,查看留言列表,查看详细留言,发布留言,安全退出等功能。先创建数据库,数据库中至少要有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异常");
}
}
%>
效果展示
**注册 **
登陆
写入
展示