怎么样用jsp做留言板啊 主要是不会在里面写有关于数据库的东西

怎么样用jsp做留言板啊 主要是不会在里面写有关于数据库的东西,第1张

留言板要实现的功能是:浏览的人能留言并能分页的查看留言,管理员能对留言进行处理!

这个留言板由9个小程序组成,分别是:board.jspmessage.htmlopendata.jspmanager.jsppassword.jspcheck.jspdelete.jspconvert.jsp

现在说第一个:board.jsp

他的任务是整个留言板的主程序,让使用者留言,并提供分页功能!

在开始之前,我们必须在mysql数据库建立一个mydate的数据库,在mudate中建立名为message的表:

mysql>create table message( name char(20),email char(40 ),subject char(60),time char(60),sex char(10),memo text,id int not null auto_increment,primary key(id))

board.jsp代码:

<html>

<head>

<title>流言板</title>

</head>

<body>

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

<%@ page contenttype="text/htmlcharset=gb2312"%>

<%@ include file="opendata.jsp"%>

<%

int count=0,lastp,numf,numl,prep,nextp,pageno

if(request.getparameter("pageno")==null) //pageno:代表页码

pageno=0

else

pageno=integer.parseint(request.getparameter("pageno"))

sql="select * from message"

rs=smt.executequery(sql)

while(rs.next())

count++<%-- count:多少留言--%>

lastp=(int)math.ceil((double)count/5)

<%--用来计算此表中有几页留言:ceil返回大于等于其数字参数的最小整数。

math.ceil(number)

必选项number 参数是数值表达式。

说明

返回值为大于等于其数字参数的最小整数。 --%>

if(pageno==0||pageno>lastp)

pageno=lastp

numf=pageno*5-4<%--显示留言的第一笔数据的编号_id--%>

numl=numf+4<%--numl:此页的最后的一笔数据编号id--%>

if(pageno==1)

prep=1<%-- prep:上一页--%>

else

prep=pageno-1

if(pageno==lastp)

nextp=lastp

else

nextp=pageno+1

sql="select * from message where id between "+numf+" and "+numl

rs=smt.executequery(sql)

%>

<font size=7 color=green>留言板</font>

<hr>

<center>

<form action=board.jsp method=post>

<table boder=0>

<tr>

<td>目前的页次<font color=red><%=pageno%></font>/<font color=blue><%=lastp%></font></td>

<td><a href=board.jsp?pageno=<%=prep%>>[上一页]</a></td>

<%--将参数pageno传递给程序,依据它来计算numl和numf,再将留言数据通过sql取出--%>

<td><a href=board.jsp?pageno=<%=nextp%>>[下一页]</a></td>

<td><a href=board.jsp?pageno=1>[第一页]</a></td>

<td><a href=board.jsp>[最后一页]</a></td>

<td>输入页次<input type=text size=3 name=pageno></td>

<td><input type=submit name=send value=送出></td>

<td><a href=password.jsp><font color=red size="5"><i>站长专用</i></font></a></td>

</tr>

</table>

</form>

<%

string name,email,subject,time,sex,memo

while(rs.next())

{

name=rs.getstring(1)

email=rs.getstring(2)

subject=rs.getstring(3)

time=rs.getstring(4)

sex=rs.getstring(5)

memo=rs.getstring(6)

out.print("<center>")

out.print("<table border=1>")

out.print("<tr><td bgcolor=yellow>姓名</td><td>"+name+"</td></tr>")

out.print("<tr><td bgcolor=yellow>e-mail</td><td>"+email+"</td></tr>")

out.print("<tr><td bgcolor=yellow>时间</td><td>"+time+"</td></tr>")

out.print("<tr><td bgcolor=yellow>主题</td><td>"+subject+"</td></tr>")

out.print("<tr><td bgcolor=yellow>留言</td><td>"+memo+"<img src="+sex+"></td></tr>")

out.print("</table><p>")

}

%>

<hr>

<center><a href="message.html">我要留言</a>

<a href=board.jsp>查看留言</a>

</body>

</html>

public List<ScorePo>findAllScoreList() //这里是定义了一个函数,函数名称 findAllSoreList,函数所需要的参数 没有(就是括号里面是空的),函数的返回值类型是 List 一个集合

{

List<ScorePo>list = new ArrayList<ScorePo>()//首先定义一个List集合,名字叫 list,ScorePo是一个对象类型,意思就是说 这个list集合中就放 ScorePo 这个对象的数据集

List<ScorePo>list = a.findAllScoreListNew() //这里就是声明一个 List集合,名字叫 list ;初始化赋值为一个函数的返回值,a 就是上面定义的DbData 对象,a 调用DbData 里面的函数 findAllScoreListNew(),返回值赋给 list

try{

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")

String url="jdbc:microsoft:sqlserver://127.0.0.1:1433DatabaseName=WebExam"

Connection conn = DriverManager.getConnection(url,"sa","123456")

Statement stmt = conn.createStatement()

String sql = "select * from tb_user where userName='"+sUserNam+"' and password='"+sPWD+"' "

ResultSet rs = stmt.executeQuery(sql)

if (!rs.next()) out.println("用户名或密码错误!")

else response.sendRedirect("index.jsp")

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/6645223.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-26
下一篇 2023-03-26

发表评论

登录后才能评论

评论列表(0条)

保存