留言板jsp+Mysql

留言板應該很容易吧...用了一點h5

 

 

上面留言板三個大字是我設計的圖片

 

1.首先要有個界面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>留言板</title>
<style type="text/css">

#Layer1 {
	
	margin: 0 auto;
	
}
</style>
</head>
<style>
body{
	background-image:url(image/jianyue2.jpg);
	background-repeat:no-repeat;
	background-size:100% 100%;
	background-attachment:fixed;
	}
#title1{
	border-radius:10px;
	height:35px ;
	width:509px;
	border:1px solid #0CF;
	background-color:#EAEAEA;
	transition:border-color ease-in-out .15s,box-shadow ease-in-out .15s;	
}
#textarea{
	border-radius:10px;
	
	border:1px solid #0CF;
	background-color:#EAEAEA;
	
}
#submit{
 width:112px; 
 height: 42px; 
 background-color:#d93c3c; 
 color:#fff; 
 border:none; 
 border-radius:6px; 
 position: relative; 
 font-size:16px; 
 font-weight: bold;
}
.bt{
width:112px; 
 height: 42px; 
 background-color:#d93c3c; 
 color:#fff; 
 border:none; 
 border-radius:6px; 
 position: relative; 
 font-size:16px; 
 font-weight: bold;}
#Layer1 form table tr td {
	text-align: justify;
}
</style>
<body>
<div style="text-align:center"><img src="image/lyb.png" width="250" height="95" /></div>


<div id="Layer1">
<center>
  <form action="add_message.jsp" >
  <table align="center">
  <tr>
  	<td style="font-weight:bold;font-family:"宋體";width="10%"" >標題:</td>
 	<td width="90%" ><input id="title1" name="title" /></td>
  </tr>
  
  <tr>
  	<td style="font-weight:bold;font-family:"宋體";width="10%"" > 內容:</td>
  	<td >  <textarea id="textarea" cols="60" rows="10"  name="content"></textarea> </td>
  </tr>
  
    <tr >
    <td colspan="2">
  	<p>&nbsp;</p>
  	<center>
  	  <input type="submit" name="submit"id="submit" value="留言" />
  	  
  	</center>
  	
  	</td>
  </tr>	
  </table>
  </form>
  <center><a href="view_message.jsp"><button class="bt"> 查看留言</button></a></center>
 </center>
</div>
</body>
</html>

2.連接數據庫 add_message.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<TITLE>add message into table </TITLE>  
</HEAD>  
<BODY>  
<%  


//數據庫
String title=request.getParameter("title");  
String content=request.getParameter("content"); 
java.util.Date date=new java.util.Date();  
String datetime=new Timestamp(date.getTime()).toString();
 
String userName="root";
String psw="123456";
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url="jdbc:mysql://localhost:3306/liuyan?useUnicode=true&characterEncoding=utf-8";    //JDBC的URL 
Connection con=DriverManager.getConnection(url,userName,psw); 
out.println("<script language='javascript'>alert('數據庫連接成功!');</script>"); 
PreparedStatement stm=con.prepareStatement("Insert into message values(?,?,?)");
stm.setString(1,title);  
stm.setString(2,content);  
stm.setString(3,datetime);  
 int rst = stm.executeUpdate();  
          if (rst != 0){  
                out.println("<script language='javascript'>alert('發佈成功');window.location.href='liuyan.html';</script>");    
          }
          else{  
                out.println("<script language='javascript'>alert('發佈失敗');window.location.href='liuyan.html';</script>");    
          }  

con.close();  
stm.close();
%>  
</body>  
</html>  

3.查看留言 view_message.jsp

<%@ page import="java.sql.*"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<head>
<meta charset="utf-8">
<title>表格</title>
<style type="text/css">
* {
	margin: 0;
	padding: 0;
}

body {
	font: italic 20px Georgia, serif;
	letter-spacing: normal;
	background-image:url(image/jianyue2.jpg);
}

#content {
	width: 750px;
	margin: 0 auto;
	background-color: #fff;
	border-left: 1px solid #ddd;
	border-right: 1px solid #ddd;
	box-shadow: 0px 0px 16px #aaa;
}

#table1 {
	font: bold 16px/1.4em "Trebuchet MS", sans-serif;
}

#table1 thead th {
	padding: 15px;
	border: 1px solid #93CE37;
	
	text-shadow: 1px 1px 1px #568F23;
	color: #fff;
	background-color: #9DD929;
	border-radius: 5px 5px 0px 0px;
}

#table1 thead th:empty {
	background-color: transparent;
	border: none;
}

#table1 tbody th {
	padding: 0px 10px;
	border: 1px solid #93CE37;
	border-right: 3px solid #9ED929;
	text-shadow: 1px 1px 1px #568F23;
	color: #666;
	background-color: #9DD929;
	border-radius: 5px 0px 0px 5px;
}

#table1 tbody td {
	padding: 10px;
	border: 2px solid #E7EFE0;
	text-align: center;
	text-shadow: 1px 1px 1px #fff;
	color: #666;
	background-color: #DEF3CA;
	border-radius: 2px;
}

#table1 tbody span.check::before {
	content: url(images/check0.png);
}

#table1 tfoot td {
	padding: 10px 0px;
	font-size: 32px;
	color: #9CD009;
	text-align: center;
	text-shadow: 1px 1px 1px #444;
}
.bt{
width:112px; 
 height: 42px; 
 background-color:#d93c3c; 
 color:#fff; 
 border:none; 
 border-radius:6px; 
 position: relative; 
 font-size:16px; 
 font-weight: bold;}
#Layer1 form table tr td {
	text-align: justify;
}
</style>
</head>
<body>
<p align="center">&nbsp;</p>
<center><img src="image/lyb.png" width="250" height="95"></center>
	<br />
	<br />
	<%
		try {
			String userName = "root";
			String psw = "123456";
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			String url = "jdbc:mysql://localhost:3306/liuyan"; //JDBC的URL 
			Connection con = DriverManager.getConnection(url, userName, psw);

			Statement stm = con.createStatement();
			ResultSet result = stm.executeQuery("select count(*) from message");
			int message_count = 0;

			if (result.next()) {
				message_count = result.getInt(1);
				result.close();
			}
			if (message_count > 0) {
				result = stm.executeQuery("select * from message order by time desc");
				while (result.next()) {
					String title = result.getString("title");

					Timestamp time = result.getTimestamp("time");
					Date date_time = new Date(time.getTime());
					Time time_time = new Time(time.getTime());
					String content = result.getString("content");
	%>

<div id="content">
		<table width="99%" id="table1">
			<tbody>
				<tr>

					<td>
					<%
							out.println("(" + message_count + ")");
					%>主題</td>
					<td colspan=2><%=title%></td>
				</tr>
				<tr>

					<td>內容</td>
					<td><%=content%></td>

				</tr>
				<tr>

					<td>留言時間</td>
					<td>
						<%
							out.println("<font size=2>" + date_time + " " + time_time + "</font>");
						%>
					</td>

				</tr>
				

			</tbody>
		</table>
		<%
			
						message_count--;
					}
					result.close();
					con.close();
				} else {
					out.println("目前還沒有任何留言!");
					con.close();
				}
			} catch (Exception e) {
				out.println(e);
			}
		%>
	</div>
	<br/>
	<br/>
	<center><a href="index.jsp"><button class="bt"> 返回首頁</button></a>
  	<a href="liuyan.html"><button class="bt"> 繼續留言</button></a></center>
  	<br/>
	<br/>
  	
</body>
</html>

結束!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章