com.jdbcdemo.entity实体类
package com.jdbcdemo.entity;
public class Notice {
private int n_id;
private String title;
private String details;
private String n_time;
//构造方法
public int getN_id() {
return n_id;
}
public Notice( String title, String details) {
super();
this.title = title;
this.details = details;
}
public Notice(String title, String details, String n_time) {
super();
this.title = title;
this.details = details;
this.n_time = n_time;
}
public Notice() {
super();
}
public void setN_id(int n_id) {
this.n_id = n_id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
public String getN_time() {
return n_time;
}
public void setN_time(String n_time) {
this.n_time = n_time;
}
}
2.与数据库建立连接com.chuanzhiboke.jdbc
package com.chuanzhiboke.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtils {
static String driver = “com.mysql.jdbc.Driver”;
static String url = “jdbc:mysql://localhost:3306/itcaststore?useUnicode=true&characterEncoding=utf8”;
static String user = “root”;
static String password = “root”;
// 导入的是sql包import java.sql.DriverManager;
// import java.sql.PreparedStatement;
public static Connection getConnection() {
Connection conn = null;
try {
// 访问数据库的步骤
// 1.加载驱动
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn, Statement stmt) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn = null;
}
}
public static void close(ResultSet rSet, Connection conn, Statement stmt) {
if (rSet != null) {
try {
rSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rSet = null;
}
// 释放资源
close(conn, stmt);
}
}
3.实现页面的添加删,查询com.chuanzhiboke.dao
package com.chuanzhiboke.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.chuanzhiboke.jdbc.DBUtils;
import com.jdbcdemo.entity.Notice;
public class NoticeDao {
Connection conn = null;//
PreparedStatement pStatement = null; // 执行SQL语句对象
ResultSet rs = null;
// 查询所有notice
public static List<Notice> queryNoticeAll() {
System.out.print("NoticeDao queryNoticeAll() ");
List<Notice> nList = new ArrayList<Notice>();
Connection conn = null;//
PreparedStatement pStatement = null; // 执行SQL语句对象
ResultSet rs = null;
try {
conn = DBUtils.getConnection();
// 获取Statement的对象
pStatement = conn.prepareStatement("SELECT * FROM `notice`");
rs = pStatement.executeQuery();
while (rs.next()) {// 循环读取每一行结果
Notice notice = new Notice();
notice.setN_id(rs.getInt("n_id"));//传的id
notice.setTitle(rs.getString("title"));
// notice.setDetails(rs.getString("details"));
notice.setN_time(rs.getString("n_time"));
//============ System.out.print(rs.getString("details"));
//========== System.out.print(rs.getString("n_time"));
nList.add(notice);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtils.close(rs, conn, pStatement);
}
return nList;
}
// 根据指定的id查询用户
public Notice queryNoticeById(int n_id) {
Notice notice = new Notice();
String sql = "SELECT * FROM `notice` WHERE n_id=?";
System.out.println(sql);
try {
conn = DBUtils.getConnection();
// 获取Statement的对象
pStatement = conn.prepareStatement(sql);
pStatement.setInt(1, n_id);
rs = pStatement.executeQuery();
while (rs.next()) {// 循环读取每一行结果
notice.setN_id(rs.getInt(1));
notice.setTitle(rs.getString(2));
notice.setDetails(rs.getString(3));
notice.setN_time(rs.getString(4));
}
System.out.println(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtils.close(conn, pStatement);
}
return notice;
}
//添加用户数据
public Boolean insert(Notice notice) {
int num=0;
String sql = "INSERT INTO `notice`(`title`,`details`,`n_time`) VALUES(?,?,?)";//占位符符合
System.out.println(sql);
try {
conn = DBUtils.getConnection();
// 获取Statement的对象
pStatement = conn.prepareStatement(sql);
pStatement.setString(1, notice.getTitle());
pStatement.setString(2, notice.getDetails());
pStatement.setString(3, notice.getN_time());
num=pStatement.executeUpdate();
System.out.println("NoticeDao---insert():num="+num);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtils.close(conn, pStatement);
}
return num>0;
}
// 根据指定的id删除
public boolean deleteNotice(int n_id) {
int n=0;
String sql = "DELETE FROM notice WHERE n_id=?";//占位符符合
System.out.println(sql);
try {
conn = DBUtils.getConnection();
// 获取Statement的对象
pStatement = conn.prepareStatement(sql);
pStatement.setInt(1,n_id);
n=pStatement.executeUpdate();
System.out.println("NoticeDao---DELETE():n="+n_id);
} catch(Exception e) {
e.printStackTrace();
} finally {
DBUtils.close(conn, pStatement);
}
return n>0;
}
}
4.建立servlet,进行数据处理com.chuanzhiboke.servlet
1.NoticeServlett.java
package com.chuanzhiboke.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.chuanzhiboke.dao.NoticeDao;
import com.jdbcdemo.entity.Notice;
/**
- Servlet implementation class NooticeDeleteServlet
*/
public class NooticeDeleteServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public NooticeDeleteServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int n_id=Integer.parseInt(request.getParameter("id"));
NoticeDao noticeDao=new NoticeDao();
if (noticeDao.deleteNotice(n_id)){
request.getRequestDispatcher("client/noticelist.jsp").forward(request, response);
}
}
}
2.NooticeDeleteServlet.java
package com.chuanzhiboke.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.chuanzhiboke.dao.NoticeDao;
import com.jdbcdemo.entity.Notice;
/**
- Servlet implementation class NooticeDeleteServlet
*/
public class NooticeDeleteServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public NooticeDeleteServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int n_id=Integer.parseInt(request.getParameter("id"));
NoticeDao noticeDao=new NoticeDao();
if (noticeDao.deleteNotice(n_id)){
request.getRequestDispatcher("client/noticelist.jsp").forward(request, response);
}
}
}
5.建立web.xml,建立servlet,具体的情况根据自己的代码的情况
<?xml version="1.0" encoding="UTF-8"?> <servlet>
<servlet-name>NoticeServlett</servlet-name>
<servlet-class>com.chuanzhiboke.servlet.NoticeServlett</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NoticeServlett</servlet-name>
<url-pattern>/NoticeServlett</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>NooticeDeleteServlet</servlet-name>
<servlet-class>com.chuanzhiboke.servlet.NooticeDeleteServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NooticeDeleteServlet</servlet-name>
<url-pattern>/NooticeDeleteServlet</url-pattern>
</servlet-mapping>
6.建立页面进行jsp传值
noticeadd.jsp noticeDetils.jsp noticelist.jsp publicFooter.jsp publicHeader.jsp
noticelist.jsp
<%@page import=“com.chuanzhiboke.dao.NoticeDao”%>
<%@page import=“java.util.ArrayList”%>
<%@page import=“com.jdbcdemo.entity.Notice”%>
<%@page import=“java.util.List”%>
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
.notice span {
display: inline-block;
float: right;
}
<span>
<a href="${pageContext.request.contextPath}/NooticeDeleteServlet?id=<%=no.getN_id()%>">删除</a>
<a href="">编辑</a>
<%=no.getN_time()%>
</span>
</tr>
<%} %>
</div>
<p>
<a href="noticeadd.jsp"><input type="button" value="添加公告"></a>
</p>
<%@include file="publicFooter.jsp"%>
noticeDetils.jsp
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
<%@page import=“com.jdbcdemo.entity.Notice”%>
<%@page import=“com.chuanzhiboke.dao.NoticeDao”%>
<%=notice.getTitle()%>
<%=notice.getDetails()%>
<%@page import=“com.chuanzhiboke.dao.NoticeDao”%>
<%@page import=“java.util.ArrayList”%>
<%@page import=“com.jdbcdemo.entity.Notice”%>
<%@page import=“java.util.List”%>
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
.notice span {
display: inline-block;
float: right;
}
<form action="${pageContext.request.contextPath}/NoticeServlett?flag=add" method="post">
<p>
标题<input type="text" name="title">
</p>
<p>
公告显示
<textarea rows="10" cols="24" name="details"></textarea>
</p>
<p>
<input type="submit" value="添加公告">
</p>
<p>
<%
if (request.getAttribute("errorMsg") == null) {
out.println();
} else {
out.println("<font color='red'>添加失败</font>");
}
%>
</p>
</form>
备注时间处理
//时间的调用import java.util.Date;
Date date=new Date();
String time = new SimpleDateFormat(“yyyyMMddHHmmss”).format(date);