使用JDBC和servlet实现留言的发布和显示
来源:互联网 发布:三国志10 兵种 数据 编辑:程序博客网 时间:2024/05/16 09:36
提交的表单用到的JavaScript代码:
<script language="javascript" >
function check()
{
if(document.getElementById("name1").value=="null")
{
alert("你现在的用户名为:"+document.getElementById("name1").value+"请先登录,再留言!");
return false;
}else{
alert("恭喜您:"+document.getElementById("name1").value+" 留言发表成功!");
return true;
}
}
</script>
发表留言的提交表单:
<!--注意onSubmit的使用,如果JS函数check( )返回的是true,则执行表单的提交;如果返回的是false,就不执行表单的提交-->
<form name="form1" method="post" action="UserManager" onsubmit="return check();">
<input type="hidden" name="action" value="ly" />
<input type="hidden" name="name1" id="name1" value="<%=(String)request.getSession().getAttribute("user_name")%>" />
标题:<input type="text" name="title" value="" size="50"/>
<textarea style="border-color:#6699FF" name="content" cols="80" rows="4"></textarea><br/>
<input type="submit" value="发表" align="right" />
</form>
servlet中的处理代码:
if("ly".equals(action))
{
String t=LYservice.nowTime();//在LYservice类中调用静态方法nowTime()获取和格式化系统时间
LY ly=new LY();//LY为留言的实体类
ly.setTitle(request.getParameter("title"));
ly.setContent(request.getParameter("content"));
ly.setLy_time(t);//留言的时间
ly.setLy_name((String) request.getSession().getAttribute("user_name"));//获取session中的留言者姓名
LYservice lys=new LYservice();
try {
if(lys.FaBiao(ly))
{
request.getRequestDispatcher("customer/ly_1.jsp").forward(request, response);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
留言相关的业务处理代码:
public class LYservice
{
public List list_LY()
{
java.sql.Connection conn=DB.getConnection();
ResultSet rs = null;
try {
String sql = "select * from market_ly";
Statement sm=conn.createStatement();
rs = sm.executeQuery(sql);
LY y = null;
Vector vector = new Vector();
// ResultSet指针最初位于第一行之前,调用next方法使下一行成为当前行
while (rs.next()) {
y = new LY();
y.setId(rs.getInt("id"));
y.setTitle(rs.getString("title"));
y.setContent(rs.getString("content"));
y.setLy_name(rs.getString("ly_name"));
y.setLy_time(rs.getString("ly_time"));
vector.addElement(y);
}
return vector;
} catch (SQLException SqlE) {
SqlE.printStackTrace();
return null;
} catch (Exception E) {
E.printStackTrace();
return null;
}
finally {
// 关闭连接,释放数据库资源:
DB.close(conn);
}
}
//获取并格式化当前系统时间
public static String nowTime()
{
Calendar c = Calendar.getInstance();
c.setTimeInMillis(new Date().getTime());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateFormat.format(c.getTime());
}
//发表留言的方法
public Boolean FaBiao(LY ly) throws SQLException
{
Boolean flag=false;
Connection con = null;
PreparedStatement pstmt = null;
//Statement stmt = null;
con = DB.getConnection();
//stmt=con.createStatement();
String sql = "insert into market_ly(title,content,ly_name,ly_time) values(?,?,?,?)";
pstmt = DB.prepare(con, sql);
String title=null;
String content=null;
String ly_name=null;
try {
title=new String(ly.getTitle().getBytes("utf-8"), "utf-8");
//System.out.println("(转换前)测试1------------"+ly.getContent().getBytes("utf-8"));
content=new String(ly.getContent().getBytes("utf-8"), "utf-8");
//System.out.println("(转换为uft-8)测试2------------"+content);
ly_name=new String(ly.getLy_name().getBytes("utf-8"), "utf-8");
} catch (Exception e) {
e.printStackTrace();}
pstmt.setString(1, title);
pstmt.setString(2, content);
pstmt.setString(3, ly_name);
pstmt.setString(4, ly.getLy_time());
//String sqltest="insert into market_ly(title,content,ly_name,ly_time) values(\'"
//+title+"\',\'"+content+"\',\'"+ly_name+"\',\'"+ly.getLy_time()+"\');";
System.out.println(sql);
int a = pstmt.executeUpdate();
//int a=stmt.executeUpdate(sqltest);
if(a==1)
{
flag=true;
System.out.println("发布留言数据成功!");
}
DB.close(pstmt);
DB.close(con);
return flag;
}
}
jsp页面中的留言显示代码:
<%
LYservice es=new LYservice();
List<LY> lys=es.list_LY();
for(LY y:lys)
{
%>
<tr>
<td bgcolor="#FFCCFF" ><span class="STYLE8"><%=y.getId() %></span></td>
<td bgcolor="#FFCCFF"><span class="STYLE8"><%=y.getTitle() %></span></td>
<td bgcolor="#FFCCFF"><span class="STYLE8"><%=y.getLy_time() %></span></td>
<td bgcolor="#FFCCFF"><span class="STYLE8"><%=y.getContent() %></span></td>
</tr>
<br/>
<%
}
%>
- 使用JDBC和servlet实现留言的发布和显示
- Servlet和JDBC的登陆实现应用
- jsp&servlet和JDBC的综合使用
- 简单留言管理平台实现中 包含的 MVC,另:javabean和servlet的区别
- 【使用jdbc和servlet实现阅览文章访问次数】
- MVC分层+JDBC+数据库+Servlet实现JSP文件上传和显示+模糊查询+分页
- jdbc和Servlet简述
- JDBC+Servlet+JSP实现简单的数据遍历和查找功能
- 原生无框架JDBC实现数据库连接和Servlet页面的基本操作
- 基于Servlet、JSP、JDBC、MySQL的登录模块(含过滤器的使用和配置)
- Servlet和jdbc登录示例
- mysql和jdbc的使用
- 使用jdbc和setvlet实现登陆验证
- 使用JDBC实现查询和转账操作
- 使用Axis2实现WebService的发布和调用
- servlet的介绍和使用
- Servlet的创建和使用
- 如何解决 JMeter 通过 JDBC 访问 Oracle 和 MySQL 的问题 (留言中有 Test Plan 实例下载)
- 解决火狐下焦点无法重复获取问题------神器版!
- stl 用法简单实例
- Unicode 、UTF-8、ascii字符编码总结
- 关于C#网络编程的基本概念
- 对session和cookie的一些理解
- 使用JDBC和servlet实现留言的发布和显示
- listView所有的属性和方法详解
- 在字符串中查找最长的数字串
- 小数点引起的数据类型转换问题
- soap访问服务器webservise以及xml解析(dom)+委托 ---封装
- iframe元素內嵌頁面如何去掉继承的html及body背景色/背景图片
- 带分数 - 第四界蓝桥杯java组
- C++多线程框架(三)--------- 消息队列
- iframe 自适应高度 去掉iframe滚动条 只留外面页面的滚动条