老师,代码报错500,不知道哪错了
//Message
package com.imooc.jdbc.bean;
import java.sql.Timestamp;
import java.util.Date;
/**
* Message Bean
*/
public class Message {
private long id;
private long userId;
private String username;
private String title;
private String content;
private Date createTime;
public Message(long aLong, long id, String string, String title, String username, Timestamp create_time) {
this.id = id;
this.userId = userId;
this.username = this.username;
this.title = this.title;
this.content = content;
this.createTime = createTime;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
//connectionUtil
package com.imooc.jdbc.bean;
import java.sql.Timestamp;
import java.util.Date;
/**
* Message Bean
*/
public class Message {
private long id;
private long userId;
private String username;
private String title;
private String content;
private Date createTime;
public Message(long aLong, long id, String string, String title, String username, Timestamp create_time) {
this.id = id;
this.userId = userId;
this.username = this.username;
this.title = this.title;
this.content = content;
this.createTime = createTime;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
//MessageDAO
package com.imooc.jdbc.dao;
import com.imooc.jdbc.bean.Message;
import com.imooc.jdbc.common.ConnectionUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* 消息DAO
*/
public class MessageDAO {
/**
* 分页查询全部留言
* @param page 当前页码
* @param pageSize 每页记录数
* @return
*/
public List<Message> getMessages(int page, int pageSize){
Connection conn= ConnectionUtil.getConnection();
String sql="select * from message order by create_time desc limit ?,?";//limit m,n 从第m条开始,取出共计n条记录
PreparedStatement stmt=null;
ResultSet rs=null;
List<Message> messages=new ArrayList<>();
try {
stmt=conn.prepareStatement(sql);
stmt.setInt(1,(page-1)*pageSize);
stmt.setInt(2,pageSize);
rs=stmt.executeQuery();
while(rs.next()){
messages.add(new Message(rs.getLong("id"),rs.getLong("user_id"),rs.getString("username"),rs.getString("title"),rs.getString("content"),rs.getTimestamp("create_time")));
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
ConnectionUtil.release(rs,stmt,conn);
}
return messages;
}
/**
* 计算所有留言数量
* @return
*/
public int countMessages(){
Connection conn= ConnectionUtil.getConnection();
String sql="select count(*) total from message ";//limit m,n 从第m条开始,取出共计n条记录
PreparedStatement stmt=null;
ResultSet rs=null;
try {
stmt=conn.prepareStatement(sql);
rs=stmt.executeQuery();
while(rs.next()){
return rs.getInt("total");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
ConnectionUtil.release(rs,stmt,conn);
}
return 0;
}
}
//MessageService
package com.imooc.jdbc.service;
import com.imooc.jdbc.bean.Message;
import com.imooc.jdbc.common.ConnectionUtil;
import com.imooc.jdbc.dao.MessageDAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
/**
* 消息service
*/
public class MessageService {
private MessageDAO messageDAO;
public MessageService(){
messageDAO =new MessageDAO();
}
/**
* 分页查询全部留言
* @param page 当前页码
* @param pageSize 每页记录数
* @return
*/
public List<Message> getMessages(int page,int pageSize){
return messageDAO.getMessages(page,pageSize);
}
/**
* 计算所有留言数量
*/
public int countMessages(){
return messageDAO.countMessages();
}
}
//MessageListServlet
package com.imooc.jdbc.servlet;
import com.imooc.jdbc.bean.Message;
import com.imooc.jdbc.service.MessageService;
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;
/**
* 消息列表Servlet
*/
public class MessageListServlet extends HttpServlet {
private MessageService messageService;
@Override
public void init() throws ServletException {
super.init();
messageService=new MessageService();
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String pageStr=request.getParameter("page");
int page=1;//页码默认值是1
if(null!=pageStr && (!("".equals(pageStr)))){
try {
page = Integer.parseInt(pageStr);
}catch (NumberFormatException e){
e.printStackTrace();
}
}
List<Message> messages= messageService.getMessages(page,5);//分页查询全部留言
int count=messageService.countMessages();
int last=count%5==0?(count/5):((count/5)+1);
request.setAttribute("last",last);
request.setAttribute("messages",messages);
request.setAttribute("page",page);
request.getRequestDispatcher("/WEB-INF/views/biz/message_list.jsp").forward(request,response);
}
@Override
public void destroy() {
super.destroy();
messageService=null;
}
}
//index.jsp
<%@ page contentType="text/html;charset=UTF-8"%>
<html>
<head>
<title>MessageBoard</title>
<meta http-equiv="refresh" content="0;url=<%=request.getContextPath()%>/message/list.do">
</head>
</html>
//web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>MessageListServlet</servlet-name>
<servlet-class>com.imooc.jdbc.servlet.MessageListServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MessageListServlet</servlet-name>
<url-pattern>/message/list.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/views/error/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/views/error/500.jsp</location>
</error-page>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
//数据库
CREATE DATABASE IF NOT EXISTS message_board ;
use message_board
CREATE TABLE message(
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) NOT NULL,
`username` varchar(32) NOT NULL,
`title` varchar(32) NOT NULL,
`content` varchar(4096) NOT NULL,
`create_time` datetime NOT NULL,
PRIMARY KEY(`id`)
) ENGINE=InnoDB AUTO_INCREMENT=24;
CREATE TABLE user(
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`username` varchar(32) NOT NULL,
`password` varchar(32) NOT NULL,
`real_name` varchar(32) DEFAULT NULL,
`birthday` datetime DEFAULT NULL,
`phone` varchar(16) DEFAULT NULL,
`address` varchar(128) DEFAULT NULL,
PRIMARY KEY(`id`),
UNIQUE KEY `user_username`(`username`)
) ENGINE=InnoDB AUTO_INCREMENT=9 ;
正在回答 回答被采纳积分+1
同学你好,我在这个问答下回答了你创建数据库错误的原因及修改方式,同学按此创建好数据库之后,再进行运行测试哦~问答链接如下:
http://class.imooc.com/course/qadetail/124602
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星