JSP(连接数据库)实现个人博客的上传、查看、删除

最近jsp期末大作业,我在做个人博客网站,向数据库存取删除数据的时候遇到了一些麻烦,于是借鉴了csdn上各位前辈的经验,写出了一个略微简陋的博客上传,删除功能。才疏学浅,欢迎指正。

编写页面jsp

<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style>
*{
background:#B0E0E6;
}
.ma{
height:500px;
width:100%;  
background: 
url("image/bb.jpg");height: 500px;width:100%;
 position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: -10;
        zoom: 1;
        background-color: #fff;
        background-repeat: no-repeat;
        background-size: cover;
        -webkit-background-size: cover;
        -o-background-size: cover;
        background-position: center 0;
}
.me{
height:250px;
width:40%;
margin:0 auto;
}
.md{
height:30px;
width:50px;
}
</style>
</head>
<body>
<div class="ma" >
<div class="me">
<h3>Write your blog</h3>
<form action="succ.jsp" method="post">
<font  face="楷体">撰写作者:</font><input type="text" name="author" size="20">
<br>
<font  face="楷体">博客标题:</font><input type="text" name="title" size="20">
<br>
<font  face="楷体">博客内容:</font><textarea name="context" rows="10" cols="40" class="mc" ></textarea>
<br>
<input type="submit" value="发布" class="md">
<input type="reset" value="重置" class="md">
</form>
</div>
</div>
</body>
</html>

上传数据到数据库的jspd代码

<%@ page language="java" import="java.util.*,java.sql.*" 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><%
String author = new String(request.getParameter("author").getBytes("ISO-8859-1"),"UTF-8");  
String context = new String(request.getParameter("context").getBytes("ISO-8859-1"),"UTF-8");  
String title = new String(request.getParameter("title").getBytes("ISO-8859-1"),"UTF-8"); 
         
            String driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
            String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=db_database08";
            String username = "sa";
            String password = "***";
            Class.forName(driverClass);
            Connection conn = DriverManager.getConnection(url,username,password);
            PreparedStatement tmt = conn.prepareStatement("Insert into man values('" + author + "','" + title + "','"+context+"')");
                        int rst = tmt.executeUpdate();
                        if (rst != 0){
                            out.println("<script language='javascript'>alert('发布成功!');window.location.href='writre.jsp';</script>"); 
                        }
                        %>
</body>
</html>

查看博客的jsp代码

<%@ page contentType="text/html"%>  
<%@page pageEncoding="utf-8"%>  
<%@page import="java.sql.*" %>  
<%@page import="java.util.*" %>
<html>
<head>
<title >查看blog</title>
</head>
<style>
*{
/* background:#B0E0E6; */
background: 	#FAEBD7;

}
</style>
<body>
<%  
String driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=db_database08";
String username = "sa";
String password = "***";
Class.forName(driverClass);
Connection conn = DriverManager.getConnection(url,username,password);

%>


<%
                Statement stmt = null;  
                ResultSet rs = null;  
                String sql = "SELECT * FROM man;";  
                stmt = conn.createStatement();  
                rs = stmt.executeQuery(sql);             
				
                String author=request.getParameter("author");
            
                
                while (rs.next()) {
%>
 <form action="clear.jsp?author=<%=rs.getString("author")%>" method="post">
    <font  face="楷体">撰写作者:<%=rs.getString("author") %></font>  
    <p>
    <font  face="楷体">博客标题:<%=rs.getString("title") %></font>  
    <p>
    <font  face="楷体">博客内容:</font>
		<textarea rows="10" cols="60" readonly><%=rs.getString("context") %></textarea>
		<p>
		<input type="submit" value="删除">
		<p>
<% }%>
</form>
</body>
</html>

删除博客的jsp代码

<%@ page language="java" import="java.sql.*" 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%  
String driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=db_database08";
String username = "sa";
String password = "***";
Class.forName(driverClass);
Connection conn = DriverManager.getConnection(url,username,password);

%>
<%	String author=new String(request.getParameter("author").getBytes("ISO-8859-1"),"utf-8");
Statement stmt=conn.createStatement();
int rt =stmt.executeUpdate("delete man where author='"+author+"'");
 out.println("<script language='javascript'>alert('删除成功!');window.location.href='watch.jsp';</script>"); 
%>
</body>
</html>

最后附上几张效果图

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jhsf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值