最近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>
最后附上几张效果图