这篇文章本来是写给zyan看的,粉丝可见,后来发现CSDN需要一千字才可粉丝可实现见这个功能,所以;
shuishuiwenwenshuishuiwenwen
shuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenweshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwenshuishuiwenwen
- 这里的关注功能没写,需求不太好,想写的更好又懒,由于数据库的设计,只有三个表,因此如果想实现点赞不累加,需要另外的表进行检索判断
- 数据库的设计非常的重要
package com.edu.zut.dao;
import com.edu.zut.entity.Liuyan;
import com.edu.zut.utils.DateUtil;
import com.edu.zut.utils.JDBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class LiuyanDao {
public static List getLiuyans(Integer tid){
List list = new ArrayList();
Connection connection = JDBUtil.getCon();
PreparedStatement ps= null;
ResultSet rs=null;
Liuyan liuyan=null;
try {
ps=connection.prepareStatement("select * from liuyan where tid=?");
ps.setInt(1,tid);
rs=ps.executeQuery();
while (rs.next()){
liuyan= new Liuyan(rs.getInt(1), DateUtil.getForDate(rs.getTimestamp(2)), rs.getInt(3),rs.getInt(4) ,rs.getString(5),rs.getInt(6),rs.getString(7));
list.add(liuyan);
}
} catch (SQLException e) {
e.printStackTrace();
}
JDBUtil.close(connection,rs,ps);
return list;
}
public static int insertLiuYan(Integer tid, String uname, String lneirong){
Connection connection = JDBUtil.getCon();
PreparedStatement ps= null;
int result=0;
try {
ps=connection.prepareStatement("insert into liuyan(lneirong,tid,uname) value(?,?,?)");
ps.setString(1,lneirong);
ps.setInt(2,tid);
ps.setString(3,uname);
result = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
JDBUtil.close(connection,null,ps);
return result;
}
//修改赞/踩
public static int updateSupport(Integer tid,Integer lid){
Connection connection = JDBUtil.getCon();
PreparedStatement ps= null;
int result=0;
try {
ps=connection.prepareStatement("update liuyan set support=support+1 where tid=? and lid=?");
ps.setInt(1,tid);
ps.setInt(2,lid);
result = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
JDBUtil.close(connection,null,ps);
return result;
}
//修改赞/踩
public static int updateOppose(Integer tid,Integer lid){
Connection connection = JDBUtil.getCon();
PreparedStatement ps= null;
int result=0;
try {
ps=connection.prepareStatement("update liuyan set oppose=oppose+1 where tid=? and lid=?");
ps.setInt(1,tid);
ps.setInt(2,lid);
result = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
JDBUtil.close(connection,null,ps);
return result;
}
}
话题.dao
package com.edu.zut.dao;
import com.edu.zut.entity.Topic;
import com.edu.zut.utils.DateUtil;
import com.edu.zut.utils.JDBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class TopicDao {
public static List getTopics(){
List list = new ArrayList();
Connection connection = JDBUtil.getCon();
PreparedStatement ps= null;
ResultSet rs=null;
Topic topic=null;
try {
ps=connection.prepareStatement("select * from topic");
rs=ps.executeQuery();
while (rs.next()){
topic= new Topic(rs.getInt(1), rs.getString(2), rs.getString(3), DateUtil.getForDate(rs.getTimestamp(4)), rs.getInt(5), rs.getString(6));
list.add(topic);
}
} catch (SQLException e) {
e.printStackTrace();
}
JDBUtil.close(connection,rs,ps);
return list;
}
//根据tid查询话题详情
public static Topic getTopic(Integer tid){
Connection connection = JDBUtil.getCon();
PreparedStatement ps= null;
ResultSet rs=null;
Topic topic=null;
try {
ps=connection.prepareStatement("select * from topic where tid=?");
ps.setInt(1,tid);
rs=ps.executeQuery();
if (rs.next()){
topic= new Topic(rs.getInt(1), rs.getString(2), rs.getString(3), DateUtil.getForDate(rs.getTimestamp(4)), rs.getInt(5), rs.getString(6));
return topic;
}
} catch (SQLException e) {
e.printStackTrace();
}
JDBUtil.close(connection,rs,ps);
return null;
}
//获取最新创建的话题的id,原理:登录者创建的话题的id倒序,获取第一个,即是最新创建的话题
public static Integer getTid(String uname){
Connection connection = JDBUtil.getCon();
PreparedStatement ps= null;
ResultSet rs=null;
try {
ps=connection.prepareStatement("select tid from topic where uname=? order by tid desc limit 1");
ps.setString(1,uname);
rs=ps.executeQuery();
if(rs.next()){
//直接返回id
return rs.getInt("tid");
}
} catch (SQLException e) {
e.printStackTrace();
}
JDBUtil.close(connection,rs,ps);
//没有创建成功返回-1
return -1;
}
public static int deleteTopic(Integer tid,String uname){
Connection connection = JDBUtil.getCon();
PreparedStatement ps= null;
int result=0;
try {
ps=connection.prepareStatement("delete from topic where tid=? and uname=?");
ps.setInt(1,tid);
ps.setString(2,uname);
result=ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
JDBUtil.close(connection,null,ps);
return result;
}
public static int insertTopic(String title,String tneirong,String uname){
Connection connection = JDBUtil.getCon();
PreparedStatement ps= null;
int result=0;
try {
ps=connection.prepareStatement("insert into topic(title,tneirong,uname) value(?,?,?)");
ps.setString(1,title);
ps.setString(2,tneirong);
ps.setString(3,uname);
result=ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
JDBUtil.close(connection,null,ps);
return result;
}
}
用户.dao
package com.edu.zut.dao;
import com.edu.zut.entity.User;
import com.edu.zut.utils.JDBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDao {
public static int selectUser(String uname,String upwd){
Connection con= JDBUtil.getCon();
String sql="select * from user where uname=? and upwd =?";
PreparedStatement ps=null;
ResultSet rs=null;
try {
ps=con.prepareStatement(sql);
ps.setString(1,uname);
ps.setString(2,upwd);
rs=ps.executeQuery();
if(rs.next()){
return 1;
}
} catch (SQLException e) {
e.printStackTrace();
}
JDBUtil.close(con,rs,ps);
return 0;
}
}
Entity
User.java
package com.edu.zut.entity;
import java.io.Serializable;
public class User implements Serializable {
private String uname;
private String upwd;
public User(String uname, String upwd) {
this.uname = uname;
this.upwd = upwd;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
@Override
public String toString() {
return "User{" +
"uname='" + uname + ''' +
", upwd='" + upwd + ''' +
'}';
}
}
Topic.java
package com.edu.zut.entity;
import com.edu.zut.utils.DateUtil;
import java.io.Serializable;
import java.util.Date;
public class Topic implements Serializable {
private Integer tid;
private String title;
private String tneirong;
private Date tdate;
private Integer renshu;
private String uname;
public Topic(Integer tid, String title, String tneirong, Date tdate, Integer renshu, String uname) {
this.tid = tid;
this.title = title;
this.tneirong = tneirong;
this.tdate = tdate;
this.renshu = renshu;
this.uname = uname;
}
public Integer getTid() {
return tid;
}
public void setTid(Integer tid) {
this.tid = tid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTneirong() {
return tneirong;
}
public void setTneirong(String tneirong) {
this.tneirong = tneirong;
}
public Date getTdate() {
return tdate;
}
public void setTdate(Date tdate) {
this.tdate = tdate;
}
public Integer getRenshu() {
return renshu;
}
public void setRenshu(Integer renshu) {
this.renshu = renshu;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
@Override
public String toString() {
return "Topic{" +
"tid=" + tid +
", title='" + title + ''' +
", tneirong='" + tneirong + ''' +
", tdate=" + tdate +
", renshu=" + renshu +
", uname='" + uname + ''' +
'}';
}
}
Liuyan.java
package com.edu.zut.entity;
import java.io.Serializable;
import java.util.Date;
public class Liuyan implements Serializable {
private Integer lid;
private Date ldate; //留言日期
private Integer support; //支持人数
private Integer oppose; //反对人数
private String lneirong; //留言内容
private Integer tid; //话题
private String uname; //用户名
public Liuyan(Integer lid, Date ldate, Integer support, Integer oppose, String lneirong, Integer tid, String uname) {
this.lid = lid;
this.ldate = ldate;
this.support = support;
this.oppose = oppose;
this.lneirong = lneirong;
this.tid = tid;
this.uname = uname;
}
public Integer getLid() {
return lid;
}
public void setLid(Integer lid) {
this.lid = lid;
}
public Date getLdate() {
return ldate;
}
public void setLdate(Date ldate) {
this.ldate = ldate;
}
public Integer getSupport() {
return support;
}
public void setSupport(Integer support) {
this.support = support;
}
public Integer getOppose() {
return oppose;
}
public void setOppose(Integer oppose) {
this.oppose = oppose;
}
public String getLneirong() {
return lneirong;
}
public void setLneirong(String lneirong) {
this.lneirong = lneirong;
}
public Integer getTid() {
return tid;
}
public void setTid(Integer tid) {
this.tid = tid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
@Override
public String toString() {
return "Liuyan{" +
"lid=" + lid +
", ldate=" + ldate +
", support=" + support +
", oppose=" + oppose +
", lneirong='" + lneirong + ''' +
", tid=" + tid +
", uname='" + uname + ''' +
'}';
}
}
Service层
删除话题
package com.edu.zut.service;
import com.edu.zut.dao.TopicDao;
import com.edu.zut.dao.UserDao;
import com.edu.zut.entity.Topic;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class DeleteTopic extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码,必须放在头部
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
Integer tid=Integer.parseInt(req.getParameter("tid"));
HttpSession session = req.getSession();
String uname= (String) session.getAttribute("uname");
int result= TopicDao.deleteTopic(tid,uname);
if(result==1){
resp.getWriter().print("");
resp.setHeader("refresh","2;/page2");
}else{
resp.getWriter().print("");
resp.setHeader("refresh","2;/page2");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
话题详情
package com.edu.zut.service;
import com.edu.zut.dao.LiuyanDao;
import com.edu.zut.dao.TopicDao;
import com.edu.zut.entity.Liuyan;
import com.edu.zut.entity.Topic;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
public class DetailTopic extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//添加之后跳转到详情页面,设置方式是setAttrbute()
Integer tid=(Integer)(req.getAttribute("detailid"));
//点击超链接里的查看,跳转后,获取方式是getParam
if(req.getParameter("option")!=null){
if(req.getParameter("option").equals("detail")){
tid=Integer.parseInt(req.getParameter("detailid"));
}
}
System.out.println("detail"+tid);
Topic topic=TopicDao.getTopic(tid);
List liuyanlist = LiuyanDao.getLiuyans(tid);
req.setAttribute("topic",topic);
req.setAttribute("liuyanlist",liuyanlist);
req.getRequestDispatcher("/jsp/detail.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
留言
package com.edu.zut.service;
import com.edu.zut.dao.LiuyanDao;
import com.edu.zut.utils.DateUtil;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
public class InsertLiuyan extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession httpSession= req.getSession();
//获取uname,lneirong,ldate
Date ldate= new Date();
String uname= (String) httpSession.getAttribute("uname");
Integer tid =Integer.parseInt(req.getParameter("tid"));
String lneirong=req.getParameter("lneirong");
System.out.println("liuyan:"+uname+tid+lneirong+DateUtil.getString(new Date()));
//添加
LiuyanDao.insertLiuYan(tid,uname,lneirong);
req.setAttribute("detailid",tid);
//转到详情页,重新检索数据库的留言信息
req.getRequestDispatcher("/detail").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
添加话题
package com.edu.zut.service;
import com.edu.zut.dao.TopicDao;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
//@RequestMapping("/insert")
public class InsertTopic extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码,必须放在头部
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
String title=req.getParameter("title");
String tneirong=req.getParameter("tneirong");
String uname= String.valueOf(req.getSession().getAttribute("uname"));
int result=TopicDao.insertTopic(title,tneirong,uname);
if(result==1){
resp.getWriter().print("");
//查询话题的id号
int tid=TopicDao.getTid(uname);
if(tid!=-1){
req.setAttribute("detailid",tid);
req.getRequestDispatcher("/detail").forward(req,resp);
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
登录
package com.edu.zut.service;
import com.edu.zut.dao.UserDao;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class login extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码,必须放在头部
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
String uname=req.getParameter("uname");
String upwd=req.getParameter("upwd");
if(UserDao.selectUser(uname,upwd)==1){
HttpSession session= req.getSession();
session.setAttribute("uname",uname);
session.setAttribute("upwd",upwd);
resp.sendRedirect("/page2");
}else {
resp.getWriter().print("");
resp.setHeader("refresh","2;/login.html");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
展示所有话题
package com.edu.zut.service;
import com.edu.zut.dao.TopicDao;
import com.edu.zut.entity.Topic;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class PageTwo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List list = TopicDao.getTopics();
req.setAttribute("list",list);
req.getRequestDispatcher("/jsp/topics.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
点赞或踩的修改
package com.edu.zut.service;
import com.edu.zut.dao.LiuyanDao;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Support extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if(req.getParameter("option")!=null){
Integer tid =Integer.parseInt(req.getParameter("tid"));
Integer lid =Integer.parseInt(req.getParameter("lid"));
if(req.getParameter("option").equals("1")){
LiuyanDao.updateSupport(tid,lid);
}else{
LiuyanDao.updateOppose(tid,lid);
}
req.setAttribute("detailid",tid);
//转到详情页,重新检索数据库的留言信息
req.getRequestDispatcher("/detail").forward(req,resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
Util层
日期处理
package com.edu.zut.utils;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtil {
public static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static String getString(Date date){
String string= sdf.format(date);
return string;
}
public static Date getDate(String string) {
Date date = null;
try {
date = sdf.parse(string);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public static Date getForDate(java.sql.Timestamp date){
//保存的是的是对应时分秒格式的date
Date date1= DateUtil.getDate(DateUtil.getString(new Date(date.getTime())));
return date1;
}
public static java.sql.Timestamp getForDate2(Date date1){
java.sql.Timestamp date =new java.sql.Timestamp(date1.getTime());
return date;
}
}
JDBC
package com.edu.zut.utils;
import java.sql.*;
public class JDBUtil {
private static String className,url,name,pwd;
static {
className="com.mysql.jdbc.Driver";
url="jdbc:mysql://localhost:3306/luntan?useUnicode=true&characterEncoding=utf-8&userSSL=false";
name="root";
pwd="123456";
}
static {
try {
Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getCon(){
Connection connection=null;
try {
connection = DriverManager.getConnection(url,name,pwd);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void close(Connection connection,ResultSet resultSet ,PreparedStatement preparedStatement){
try {
if(resultSet!=null){resultSet.close();}
if(preparedStatement!=null){ preparedStatement.close();}
if(connection!=null){connection.close();}
} catch (SQLException e) {
throw new RuntimeException();
}
}
}
Web页面
/jsp
<%@ page import="com.edu.zut.entity.Topic" %><%--
Created by IntelliJ IDEA.
User: hi
Date: 2022/4/20
Time: 10:17
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
话题详情页
<%@page import="com.edu.zut.utils.DateUtil" %>
话题${requestScope.topic.title}的详情页面
${requestScope.topic.title}
作者:${requestScope.topic.uname} |
时间:${DateUtil.getString(requestScope.topic.tdate)} |
关注:${requestScope.topic.renshu}
${requestScope.topic.tneirong}
${liuyan.uname}
${DateUtil.getString(liuyan.ldate)}
赞:${liuyan.support}
踩:${liuyan.oppose}
${liuyan.lneirong}
topics.jsp展示所有
<%--
Created by IntelliJ IDEA.
User: hi
Date: 2022/4/19
Time: 23:21
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="ftm" uri="http://java.sun.com/jsp/jstl/fmt" %>
所有话题的展示!
<%@page import="com.edu.zut.entity.Topic"%>
<%@ page import="java.util.*" %>
<%@page import="com.edu.zut.utils.DateUtil" %>
<%
List list = (List) request.getAttribute("list");
%>
添加话题
顺序
编号
标题
关注
时间
作者
操作
查看详情
${vs.index}
${topic.tid}
${topic.title}
${topic.renshu}
${DateUtil.getString(topic.tdate)}
${topic.uname}
删除
查看
insert.html
添加话题
login.html
欢迎来到社区系统!
数据库的操作
注意:时间是timstamp类型(精确到时分秒),主键一定要自增,不然添加的时候会出现麻烦,你能知道自己要添加的主键吗,不看数据库你能知道你主键添加到第几了吗,最主要是也不能写null来添加主键,因为主键不为null(我的测试不成功,所以写null也不行),总之必须自增!
ALTER DATABASE luntan CHARACTER SET utf8;
alter table user character set utf8;
alter table liuyan character set utf8;
alter table topic character set utf8;
-- 表关系:一个人可以关注多个话题,创建多个话题,因此一对多;一个话题可以有多个留言,每个人可以留言多次
CREATE TABLE user(
uname VARCHAr(12) PRIMARY KEY, -- 用户名
upwd VARCHAr(12) not null -- 密码
);
CREATE TABLE topic(
tid int PRIMARY KEY auto_increment,
title VARCHAr(12), -- 标题
tneirong VARCHAr(20), -- 话题内容
tdate Date not null, -- 话题创建日期
renshu INT, -- 关注人数
uname VARCHAr(12) NOT NULL, -- 创建者
FOREIGN key(uname) REFERENCES user(uname)
);
CREATE TABLE liuyan(
lid INT PRIMARY KEY,
ldate DATE not null, -- 留言的时间
support int, -- 支持的人数
oppose int, -- 反对的人数
lneirong VARCHAr(50), -- 留言的内容
tid INT,
uname VARCHAr(12) NOT NULL, -- 一对一,每个留言一定要有uname,因此不为空
FOREIGN key(uname) REFERENCES USER(uname),
FOREIGN KEY(tid) REFERENCES topic(tid)
);
alter table topic modify tdate timestamp
alter table liuyan modify ldate timestamp
需要的jar包
如果没有,可以从maven仓库里找,比如javax._或者standrad._下面找
最初的设计2022/4/21 10:36 获取自增键功能的修改
功能:添加话题
功能解释:每当添加一个话题,添加完毕,转到详情页
设计点:添加完毕之后,转到对应的详情页,但是此时不知道获取哪个话题的详情页(因为用户添加话题,不可能会让用户添加话题的id号,因此这里想要在添加后,将id传给下一个action是很困的事情,只能传个session里的用户名,话题内容,话题标题,另外也不可能知道添加话题的id号,除非看数据库)
具体实现:再写个检索方法,添加成功后立刻检索该session存储的用户的所有话题,在写sql语句的时候,进行了检索,排序,截取操作(类似的功能解决,比如排除添加重复话题,也可以用sql解决,方便快捷代码量少)
局限性:数据库中的用户表与话题表必须有关系,当用户是管理员的时候,与话题表没有关系,当多个管理员并发进行添加操作的时候,即使通过检索数据库截取操作等等 也无法获取当前管理员所添加的话题,因为是并发。
select tid from topic order by tid desc limit 1改进的设计
PreparedStatement里有个参数,叫autoGenereatedKey(大概张这个样子),如
@Test
public void test07() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection connection=DriverManager.getConnection("","","");
//设置获取1,不设置获取2
PreparedStatement preparedStatement = connection.prepareStatement("", Statement.RETURN_GENERATED_KEYS);
preparedStatement.executeUpdate();
//获取自增主键
ResultSet resultSet = preparedStatement.getGeneratedKeys();
}
Pic:
通过获取自增的主键(1),即省去了getTid方法,可以添加新话题的时候,返回自增主键的id,不必再检索该用户所有话题,倒序截取第一个话题的id了。



