图书管理系统项目
第一天:准备把整个框架顺一遍,第一次独立写这个算是我人生中第一个java小项目,不清楚多少天能完成,我会坚持把自己的心得写在这里,主要是给自己看(不是学文的,文笔很差见谅,争取把注释写详细点),如果能帮助其他初学者更好了 一起加油!
DriverManager.getConnection(String url,String user,String password) 我之前不太理解 特意查了下API
第一天搭建一个数据库实现基本功能 (因为前三步都需要连接数据库,定义一个数据库连接)
在类中创建一个构造函数初始化数据库的连接
(PS: 为什么要创建数据库,为了初始化数据库,在后面会经常用的到)
1.创建数据库 createDatabase()
2.创建表 createTable()
3.创建文件 addData(String filename)
我标记红色的请留意
-
package javastudy;
-
-
import java.io.BufferedReader;
-
import java.io.FileReader;
-
import java.io.IOException;
-
import java.sql.DriverManager;
-
import java.sql.PreparedStatement;
-
import java.sql.SQLException;
-
-
import com.mysql.jdbc.Connection;
-
import com.mysql.jdbc.Statement;
-
-
public class DBLib {
-
//因为三个方法都需要连接数据库
-
Connection conn; //定义一个变量
-
Statement st;
-
public DBLib() throws ClassNotFoundException//定义一个构造函数 初始化数据库连接
-
, SQLException
-
{
-
Class.forName("com.mysql.jdbc.Driver"); //加载驱动程序
-
//需要得到三个参数
-
String url="jdbc:mysql://127.0.0.1:3306
?useUnicode=true&characterEncoding=utf-8";//本机ip地址 本机端口
防止数据库乱码(若无乱码无须添加)
-
String user="root"; //数据库的名字
-
String pwd="521125"; //数据库密码
-
conn=(Connection) DriverManager.getConnection(url, user, pwd); //初始化数据库 拿到三个参数
-
st=(Statement) conn.createStatement(); //创建一个 Statement 对象来将 SQL 语句发送到数据库。
-
}
-
public void createDatabase() throws SQLException //创建一个数据库
-
{
-
String sql="drop database if exists Book;"; //判断数据库存在删除
-
st.executeUpdate(sql);//执行给定 SQL 语句
-
sql="create database Book;"; //创建表
-
st.executeUpdate(sql);//执行
-
-
-
}
-
public void createTable() throws SQLException //创建一个表
-
{
-
String sql="use Book;";//使用表
-
st.executeQuery(sql);//执行
-
sql="create table BOOKS";
-
sql+="(";
-
sql+=" ID int(4) not null primary key auto_increment,";
-
sql+=" Name varchar(100),";
-
sql+=" Author varchar(50),";
-
sql+=" Price decimal,";
-
sql+="Publisher varchar(100)";
-
sql+=")
ENGINE=InnoDB DEFAULT CHARSET=utf8;";//我额外加的 因为我安装数据库
未设置UTF-8 出现乱码
-
st.executeUpdate(sql);//执行语句
-
}
-
public void addData(String filename) throws SQLException//创建一个文档
-
, IOException
-
{
-
String sql="use Book;";//使用表
-
st.executeUpdate(sql);//执行
-
FileReader fr=new FileReader(filename); //读取
-
BufferedReader br=new BufferedReader(fr); //缓冲区(包装类)
-
String line;
-
sql=" INSERT INTO BOOKS (Name,Author,Price,Publisher) VALUES(?,?,?,?)"; //sql语句
-
PreparedStatement ps=conn.prepareStatement(sql);
-
-
while((line=br.readLine())!=null) //判断是否读完
-
{
-
String[] values=line.split(","); //以逗号拆分 用一个字符串数组接收
-
ps.setString(1, values[0]); //字段从1数组从0开始
-
ps.setString(2, values[1]);
-
ps.setDouble(3, Double.parseDouble(values[2]));//把字符串强制 转换Double
-
ps.setString(4, values[3]);
-
st.executeUpdate(sql);//执行
-
}
-
br.close(); //关闭资源
-
}
-
}
?useUnicode=true&characterEncoding=utf-8";//本机ip地址 本机端口 防止数据库乱码(若无乱码无须添加)
String user="root"; //数据库的名字
String pwd="521125"; //数据库密码
conn=(Connection) DriverManager.getConnection(url, user, pwd); //初始化数据库 拿到三个参数
st=(Statement) conn.createStatement(); //创建一个 Statement 对象来将 SQL 语句发送到数据库。
}
public void createDatabase() throws SQLException //创建一个数据库
{
String sql="drop database if exists Book;"; //判断数据库存在删除
st.executeUpdate(sql);//执行给定 SQL 语句
sql="create database Book;"; //创建表
st.executeUpdate(sql);//执行
}
public void createTable() throws SQLException //创建一个表
{
String sql="use Book;";//使用表
st.executeQuery(sql);//执行
sql="create table BOOKS";
sql+="(";
sql+=" ID int(4) not null primary key auto_increment,";
sql+=" Name varchar(100),";
sql+=" Author varchar(50),";
sql+=" Price decimal,";
sql+="Publisher varchar(100)";
sql+=") ENGINE=InnoDB DEFAULT CHARSET=utf8;";//我额外加的 因为我安装数据库未设置UTF-8 出现乱码
st.executeUpdate(sql);//执行语句
}
public void addData(String filename) throws SQLException//创建一个文档
, IOException
{
String sql="use Book;";//使用表
st.executeUpdate(sql);//执行
FileReader fr=new FileReader(filename); //读取
BufferedReader br=new BufferedReader(fr); //缓冲区(包装类)
String line;
sql=" INSERT INTO BOOKS (Name,Author,Price,Publisher) VALUES(?,?,?,?)"; //sql语句
PreparedStatement ps=conn.prepareStatement(sql);
while((line=br.readLine())!=null) //判断是否读完
{
String[] values=line.split(","); //以逗号拆分 用一个字符串数组接收
ps.setString(1, values[0]); //字段从1数组从0开始
ps.setString(2, values[1]);
ps.setDouble(3, Double.parseDouble(values[2]));//把字符串强制 转换Double
ps.setString(4, values[3]);
st.executeUpdate(sql);//执行
}
br.close(); //关闭资源
}
}
2.创建一个init 初始化测试创建数据库是否成功
第一个问题:
遇到一个错误 创建数据实现,无法向里面加入数据,显示代码错误
解决方法:
由于粗心,和对代码的不熟悉浪费了十分钟去查错误,特意去看了API把
1.executeUpdate() 2.executeQuery();
上面错误代码我没有修改!复制时需要注意
第二个问题 数据库在创建时 生成的不是utf-8
解决方法1:
第3个问题 导入文字出现乱码
解决方法1:去根目录找文件把属性改成utf-8 C:\Users\Administrator\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\图书管理项目 (未成功)
解决方案2:去官网找了一篇大佬的资料
jsp用来显示结果,用来做页面呈现
逻辑处理用Servlet来做。
jsp:用来显示结果
-
<%@ page language="java" contentType="text/html; charset=UTF-8"
-
pageEncoding=
"UTF-8"
import=
"javastudy.*"%>
-
<!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>
-
<script type="text/javascript" src="pytjon/jquery-1.11.1.js">
</script>
-
<script type="text/javascript">
-
$(
function(){
-
$(
"input[type='button']").click(
function(data){
//当按钮被点击
-
$.post(
"initDB.jsp",
function(data){
//进入initDB。jsp
-
$(
"#info").html(data);
-
});
-
});
-
});
-
</script>
-
-
</head>
-
<body>
-
<input type="button" value="初始化数据">
-
<div id="info">
</div>
-
</body>
servlet:处理逻辑,处理业务,
-
package javastudy;
-
-
import java.io.IOException;
-
import java.io.PrintWriter;
-
import java.sql.SQLException;
-
-
import javax.servlet.ServletException;
-
import javax.servlet.annotation.WebServlet;
-
import javax.servlet.http.HttpServlet;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
-
/**
-
* Servlet implementation class InitIt
-
*/
-
@WebServlet("/InitIt")
-
public class InitIt extends HttpServlet {
-
private static final long serialVersionUID = 1L;
-
-
/**
-
* @see HttpServlet#HttpServlet()
-
*/
-
public InitIt() {
-
super();
-
// TODO Auto-generated constructor stub
-
}
-
-
/**
-
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
-
*/
-
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
// TODO Auto-generated method stub
-
//response.getWriter().append("Served at: ").append(request.getContextPath());
-
-
DBLib lib;
-
try {
-
lib = new DBLib();
-
lib.createDatabase();
-
lib.createTable();
-
lib.addData(this.getServletContext().getRealPath("") + "\\data\\data.txt");
-
response.setCharacterEncoding("utf-8");
-
PrintWriter out=response.getWriter();
-
out.print("初始化成功!");
-
} catch (ClassNotFoundException | SQLException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
-
/**
-
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
-
*/
-
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
// TODO Auto-generated method stub
-
doGet(request, response);
-
}
-
-
}
-
Servlet解决乱码的方法
-
-
response.setContentType("text/html;charset=utf-8");
-
response.setCharacterEncoding("utf-8");
-
-
注解配置Servlet
-
Annotation=注解
-
-
-
web.xml配置servlet的方法
-
-
<servlet>
-
<servlet-name>a
</servlet-name>
-
<servlet-class>javastudy.InitIt
</servlet-class>
-
</servlet>
-
<servlet-mapping>
-
<servlet-name>a
</servlet-name>
-
<url-pattern>/myservlet
</url-pattern>
-
</servlet-mapping>
前面的数据库创建测试通过,数据库成功搭建
把整个项目步骤写一下:
JSP网站开发模式二:JSP+JavaBeans+Servlet Model II MVC的思想(Model=模型,View=视图,Control=控制)
将控制业务逻辑的代码写到Servlet中,讲页面的展现(HTML)和业务逻辑的控制(Java)分离开来。
步骤:1.建立一个JavaBean,它是一个类Book
-
package javastudy;
-
-
public class Book {
-
int id;
-
String name;
-
String author;
-
float price;
-
String publisher;
-
public int getId() {
-
return id;
-
}
-
public void setId(int id) {
-
this.id = id;
-
}
-
public String getName() {
-
return name;
-
}
-
public void setName(String name) {
-
this.name = name;
-
}
-
public String getAuthor() {
-
return author;
-
}
-
public void setAuthor(String author) {
-
this.author = author;
-
}
-
public float getPrice() {
-
return price;
-
}
-
public void setPrice(float price) {
-
this.price = price;
-
}
-
public String getPublisher() {
-
return publisher;
-
}
-
public void setPublisher(String publisher) {
-
this.publisher = publisher;
-
}
-
}
2.建立一类叫做BookDAO ,这是数据访问层DAO=Data Access Object=数据访问接口连接数据库,实现以下方法。
2.1BookDAO()
-
Connection conn; //定义公共变量
-
Statement st;
-
String sql;
-
PreparedStatement ps;
-
public BookDAO() throws ClassNotFoundException,SQLException//定义一个构造函数 初始化数据库连接
-
{
-
Class.forName("com.mysql.jdbc.Driver"); //加载驱动程序
-
//需要得到三个参数
-
String url="jdbc:mysql://127.0.0.1:3306/book?useUnicode=true&characterEncoding=utf-8";//本机ip地址 本机端口 防止数据库乱码
-
String user="root"; //数据库的名字
-
String pwd="521125"; //数据库密码
-
conn=DriverManager.getConnection(url,user,pwd); //初始化数据库 拿到三个参数
-
st=conn.createStatement(); //创建一个 Statement 对象来将 SQL 语句发送到数据库。
-
-
}
2.2 addBook(Book book)
-
//增加
-
public int addBook(Book book) throws Exception
-
{
-
sql="insert into BOOKS(name,author,price,publisher) values(?,?,?,?)";
-
ps=conn.prepareStatement(sql);//创建一个 PreparedStatement 对象来将参数化的 SQL 语句发送到数据库。
-
ps.setString(1,book.getName());
-
ps.setString(2,book.getAuthor());
-
ps.setDouble(3,book.getPrice());
-
ps.setString(4,book.getPublisher());
-
-
return ps.executeUpdate();
-
}
2.3 deleteBook(int id)
-
//删除
-
public int deleteBook(int id) throws Exception //通过ID查找
-
{
-
sql="delete from books where id=?";
-
ps=conn.prepareStatement(sql);
-
ps.setInt(1, id);
-
return ps.executeUpdate();
-
// 在此 PreparedStatement 对象中执行 SQL 语句,该语句必须是一个 SQL 数据操作语言语句,或者是无返回内容的 SQL 语句
-
}
2.4 ArrayList<Book> getBooks()
-
//拿到所有 的图书
-
public ArrayList
<Book> getbooks() throws Exception {
-
sql="select * from books";
-
ps=conn.prepareStatement(sql);
-
ResultSet rs = ps.executeQuery();
-
ArrayList
<Book> list=new ArrayList
<Book>();
-
-
while(rs.next()) {
-
Book book=new Book();
-
book.setId(rs.getInt(1));
-
book.setName(rs.getString(2));
-
book.setAuthor(rs.getString(3));
-
book.setPrice(rs.getFloat(4));
-
book.setPublisher(rs.getString(5));
-
list.add(book);
-
}
-
return list;
-
}
2.5 modifyBook(int id,Book newbook)
-
//修改一本书
-
public int modifyBook(int id, Book newbook) throws Exception {
-
// TODO Auto-generated method stub
-
sql="update books set name=?,author=?,price=?,publisher=? where id=?";
-
ps=conn.prepareStatement(sql);
-
ps.setString(1, newbook.getName());
-
ps.setString(2, newbook.getAuthor());
-
ps.setDouble(3, newbook.getPrice());
-
ps.setString(4, newbook.getPublisher());
-
ps.setInt(5, id);
-
return ps.executeUpdate();
-
}
2.6getBookById(int id)
-
//通过一本书得到信息
-
public Book getBookById(int id) throws SQLException
-
{
-
String sql="select * from books where id=?";
-
ps=conn.prepareStatement(sql);
-
ps.setInt(1, id);
-
ResultSet rs=ps.executeQuery();
-
rs.next();
-
Book book=new Book();
-
book.setId(rs.getInt(1));
-
book.setName(rs.getString(2));
-
book.setAuthor(rs.getString(3));
-
book.setPrice(rs.getFloat(4));
-
book.setPublisher(rs.getString(5));
-
return book;
-
-
-
}
2.7getBooks(String bookName)
-
//搜索图书
-
public ArrayList
<Book> getBook(String books) throws Exception {
-
// SELECT * FROM users WHERE username LIKE '%三%'
-
//sql="select * from books where name=?";
-
//select * from table_name where id like '83%58193';
-
//sql:select id,name from tablename where contains( address, '上海' );
-
/* SELECT student_id,student_name
-
FROM students
-
WHERE CONTAINS( address, 'beijing' )*/
-
-
sql="SELECT * FROM books WHERE name LIKE '%?%'";
-
ps=conn.prepareStatement(sql);
-
ps.setString(1, books);
-
ResultSet rs = ps.executeQuery(sql);
-
ArrayList
<Book> list=new ArrayList
<Book>();
-
while(rs.next()) {
-
Book book=new Book();
-
book.setId(rs.getInt(1));
-
book.setName(rs.getString(2));
-
book.setAuthor(rs.getString(3));
-
book.setPrice(rs.getFloat(4));
-
book.setPublisher(rs.getString(5));
-
list.add(book);
-
}
-
return list;
-
}
//搜索图书
public ArrayList<Book> getBook(String books) throws Exception {
// SELECT * FROM users WHERE username LIKE '%三%'
//sql="select * from books where name=?";
//select * from table_name where id like '83%58193';
//sql:select id,name from tablename where contains( address, '上海' );
/* SELECT student_id,student_name
FROM students
WHERE CONTAINS( address, 'beijing' )*/
sql="SELECT * FROM books WHERE name LIKE '%?%'";
ps=conn.prepareStatement(sql);
ps.setString(1, books);
ResultSet rs = ps.executeQuery(sql);
ArrayList<Book> list=new ArrayList<Book>();
while(rs.next()) {
Book book=new Book();
book.setId(rs.getInt(1));
book.setName(rs.getString(2));
book.setAuthor(rs.getString(3));
book.setPrice(rs.getFloat(4));
book.setPublisher(rs.getString(5));
list.add(book);
}
return list;
}
搜索功能待更新
3.做一个jsp页面,bookList.jsp。
整体给大家看一下:
<input type="hidden" name="id"> 隐藏
booklist.jsp,页面加载时,列表显示所有的图书
3.1【样式表+Js代码】
-
<script type="text/javascript">
-
-
function altRows(id){
-
if(
document.getElementsByTagName){
-
-
var table =
document.getElementById(id);
-
var rows = table.getElementsByTagName(
"tr");
-
-
for(i =
0; i < rows.length; i++){
-
if(i %
2 ==
0){
-
rows[i].className =
"evenrowcolor";
-
}
else{
-
rows[i].className =
"oddrowcolor";
-
}
-
}
-
}
-
}
-
-
window.onload=
function(){
-
altRows(
'gridtable');
-
}
-
</script>
-
<style type="text/css">
-
-
-
table
.gridtable {
-
font-family: verdana,arial,sans-serif;
-
font-size:
11px;
-
color:
#333333;
-
border-width:
1px;
-
border-color:
#a9c6c9;
-
border-collapse: collapse;
-
}
-
table
.gridtable
th {
-
border-width:
1px;
-
padding:
8px;
-
border-style: solid;
-
border-color:
#a9c6c9;
-
}
-
table
.gridtable
td {
-
border-width:
1px;
-
padding:
8px;
-
border-style: solid;
-
border-color:
#a9c6c9;
-
}
-
.oddrowcolor{
-
background-color:
#d4e3e5;
-
}
-
.evenrowcolor{
-
background-color:
#c3dde0;
-
}
-
*{
margin:
0;
padding:
0;}
-
body{
overflow:hidden;
background:
url(
"images/2.jpg");}
-
</style>
font-family: verdana,arial,sans-serif;
font-size:11px;
color:#333333;
border-width: 1px;
border-color: #a9c6c9;
border-collapse: collapse;
}
table.gridtable th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #a9c6c9;
}
table.gridtable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #a9c6c9;
}
.oddrowcolor{
background-color:#d4e3e5;
}
.evenrowcolor{
background-color:#c3dde0;
}
*{margin:0;padding:0;}
body{overflow:hidden;background:url("images/2.jpg");}
</style>
3.2【增】页面有一个增加图书按钮,可以增加图书
-
$("#btnAdd").click(function(){
-
location.href="add.jsp";
-
});
-
<embed src="music/4.mp3" width="0.01" height="0.01">
</embed>
-
<form method="post" action="AddIt">
-
<table class="gridtable" id="gridtable">
-
<tr>
<td colspan="2">添加图书信息
</td>
</tr>
-
<tr>
<td>名称
</td>
<td>
<input type="text" name="name" >
</td>
</tr>
-
<tr>
<td>作者
</td>
<td>
<input type="text" name="author" >
</td>
</tr>
-
<tr>
<td>价格
</td>
<td>
<input type="text" name="price" >
</td>
</tr>
-
<tr>
<td>出版社
</td>
<td>
<input type="text" name="publisher">
</td>
</tr>
-
<tr>
<td colspan="2">
<input type="submit" value="添加图书信息">
<div id="info">
</div>
</td>
</tr>
-
</table>
-
</form>
-
</body>
-
response.setCharacterEncoding("utf-8");
-
request.setCharacterEncoding("utf-8");
-
//int id=Integer.parseInt(request.getParameter("id")); //得到ID
-
String name=request.getParameter("name");
-
String author=request.getParameter("author");
-
float price=Float.parseFloat(request.getParameter("price"));
-
String publisher=request.getParameter("publisher");
-
Book newbook=new Book();
-
newbook.name=name;
-
newbook.author=author;
-
newbook.price=price;
-
newbook.publisher=publisher;
-
BookDAO dao;
-
try {
-
dao=new BookDAO();
-
dao.addBook(newbook);
-
response.sendRedirect("BookList.jsp");
-
} catch (Exception e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
3.2【删】页面上有一个删除按钮,可以删除图书
-
$("#btnDelete").click(function(){
-
//将被选中的图书的id收集起来
-
var ids="";
-
var i=0;
-
$("input[type='checkbox'][name='selectit']").each(function(index, element) {
-
if(index>0)
-
{
-
if($(this).prop("checked"))
-
{
-
var id= $(this).parent().parent().children()
-
.eq(0).children().eq(0).val();
-
ids+=id;
-
i++;
-
if(!$(this).is(":last"))
-
{
-
ids+=",";
-
}
-
}
-
}
-
});
-
var answer=confirm("你确定要删除选定的这" + i+ "本图书吗?");
-
if(!answer)
-
{
-
return;
-
}
-
location.href="DeleteIt?ids=" + ids;
-
});
-
response.setCharacterEncoding("utf-8");
-
String ids=request.getParameter("ids");
-
String [] id=ids.split(",");
-
-
-
try {
-
BookDAO dao =new BookDAO();
-
for(int i=0;i
<id.length;i++)
-
{
-
dao.deleteBook(Integer.parseInt(id[i]));
-
}
-
} catch (Exception e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
response.sendRedirect("BookList.jsp");
-
-
-
-
//response.getWriter().append("Served at: ").append(request.getContextPath());
-
}
String ids=request.getParameter("ids");
String [] id=ids.split(",");
try {
BookDAO dao =new BookDAO();
for(int i=0;i<id.length;i++)
{
dao.deleteBook(Integer.parseInt(id[i]));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.sendRedirect("BookList.jsp");
//response.getWriter().append("Served at: ").append(request.getContextPath());
}
3.3【改】每本图书后面有一个修改按钮,可以修改图书
-
$("input[type='button'][name='modifyit']").click(function(){
-
//获取id
-
var id= $(this).parent().parent().children()
-
.eq(0).children().eq(0).val();
-
//页面跳转
-
location.href="modify.jsp?id="+id;
-
});
-
<body>
-
<embed src="music/3.mp3" width="0.01" height="0.01">
</embed>
-
<%
-
int
id=
Integer.parseInt(request.getParameter("
id")); //需要封装
-
BookDAO
dao=
new
BookDAO();
-
Book
book=
dao.getBookById(id); //得到
ID
-
-
-
%>
-
<form method="post" action="Modify">
-
<table class="gridtable" id="gridtable" >
-
<tr>
<td colspan="2">修改图书信息
</td>
</tr>
-
<tr>
<td>
<input type="hidden" name="id" value="<%=book.getId()%>">
</td>
</tr>
-
<tr>
<td>名称
</td>
<td>
<input type="text" name="name"value="<%=book.getName() %>">
</td>
</tr>
-
<tr>
<td>作者
</td>
<td>
<input type="text" name="author"value="<%=book.getAuthor() %>">
</td>
</tr>
-
<tr>
<td>价格
</td>
<td>
<input type="text" name="price"value="<%=book.getPrice() %>">
</td>
</tr>
-
<tr>
<td>出版社
</td>
<td>
<input type="text" name="publisher"value="<%=book.getPublisher() %>">
</td>
</tr>
-
<tr>
<td colspan="2">
<input type="submit" value="修改图书信息">
<div id="info">
</div>
</td>
</tr>
-
</table>
-
-
-
</form>
-
</body>
Book book=dao.getBookById(id); //得到ID
%>
<form method="post" action="Modify">
<table class="gridtable" id="gridtable" >
<tr><td colspan="2">修改图书信息</td></tr>
<tr><td><input type="hidden" name="id" value="<%=book.getId()%>"></td></tr>
<tr><td>名称</td><td><input type="text" name="name"value="<%=book.getName() %>"></td></tr>
<tr><td>作者</td><td><input type="text" name="author"value="<%=book.getAuthor() %>"></td></tr>
<tr><td>价格</td><td><input type="text" name="price"value="<%=book.getPrice() %>"></td></tr>
<tr><td>出版社</td><td><input type="text" name="publisher"value="<%=book.getPublisher() %>"></td></tr>
<tr><td colspan="2"><input type="submit" value="修改图书信息"><div id="info"></div></td></tr>
</table>
</form>
</body>
-
response.setCharacterEncoding("utf-8");
-
request.setCharacterEncoding("utf-8");
-
int id=Integer.parseInt(request.getParameter("id")); //得到ID
-
String name=request.getParameter("name");
-
String author=request.getParameter("author");
-
float price=Float.parseFloat(request.getParameter("price"));
-
String publisher=request.getParameter("publisher");
-
Book newbook=new Book();
-
newbook.name=name;
-
newbook.author=author;
-
newbook.price=price;
-
newbook.publisher=publisher;
-
BookDAO dao;
-
-
-
-
try {dao=new BookDAO();
-
dao.modifyBook(id,newbook);
-
} catch (Exception e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
response.sendRedirect("BookList.jsp");
-
-
//response.getWriter().append("Served at: ").append(request.getContextPath());
-
}
request.setCharacterEncoding("utf-8");
int id=Integer.parseInt(request.getParameter("id")); //得到ID
String name=request.getParameter("name");
String author=request.getParameter("author");
float price=Float.parseFloat(request.getParameter("price"));
String publisher=request.getParameter("publisher");
Book newbook=new Book();
newbook.name=name;
newbook.author=author;
newbook.price=price;
newbook.publisher=publisher;
BookDAO dao;
try {dao=new BookDAO();
dao.modifyBook(id,newbook);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.sendRedirect("BookList.jsp");
//response.getWriter().append("Served at: ").append(request.getContextPath());
}
3.4【查】页面上有一个选中按钮,可以根据图书的是否被选中查询显示图书
-
$("#query").click(function(){
-
//将被选中的图书的id收集起来
-
/* var ids="";
-
var i=0;
-
var
-
$("input[type='checkbox'][name='selectit']").each(function(index, element) {
-
if(index>0)
-
{
-
if($(this).prop("checked"))
-
{
-
var id= $(this).parent().parent().children()
-
.eq(0).children().eq(0).val();
-
ids+=id;
-
i++;
-
if(!$(this).is(":last"))
-
{
-
ids+=",";
-
}
-
}
-
}
-
});
-
*/
-
var arr=[];
-
$("input[type='checkbox']:checked").each(function(){
-
arr.push(this.value);
-
})
-
var json = JSON.stringify(arr);//数组转换成json,都在了,数组和json
-
alert(json + arr);
-
-
var answer=confirm("你确定要查询选定的这" + arr.length + "本图书吗?");
-
if(!answer)
-
{
-
return;
-
}
-
location.href="reveal.jsp?ids="+arr;
-
});
无须处理 只需要判断选中多少 遍历出来 没用到Servlet
3.5【搜】页面上有一个搜索按钮,可以根据图书的名称和作者,来搜索并显示图书
(待)修改
-
$("#search").click(function(){
-
location.href="search.jsp";
-
});
-
<body>
-
<center>
-
<marquee width="300" direction=left height="50">
<center>
<font color="pink" size="7" face="华文彩云">
<b>图书信息
</b>
</font>
</center>
</marquee>
-
<embed src="music/3.mp3" width="0.01" height="0.01">
</embed>
-
<table class="gridtable" id="gridtable" height="400" width="30%">
-
<%
-
request.setCharacterEncoding("UTF-8");
-
response.setCharacterEncoding("UTF-8");
-
String name=request.getParameter("name");
-
String condition=request.getParameter("condition");
-
out.print(name);
-
-
%>
-
<%
-
BookDAO
dao=
new
BookDAO();
-
ArrayList<
Book> list=dao.getbooks();
-
for(int i=0;i
<list.size();i++){
-
Book
book=
list.get(i);
-
%>
-
<tr>
-
<td>
<%=i+1 %>
<input type="hidden" name="id" value="<%=book.getId()%>">
</td>
-
<td>
<h3>
<%=book.getName() %>
</h3>
</td>
-
<td>
<h3>
<%=book.getAuthor() %>
</h3>
</td>
-
<td>
<h3>
<%=book.getPrice() %>
</h3>
</td>
-
<td>
<h3>
<%=book.getPublisher() %>
</h3>
</td>
-
</tr>
-
<%
-
}
-
%>
-
-
</table>
-
</center>
-
</body>
创建一个BookDAO的类实现数据库的基本功能 增删改查,新建个类aaa做测试
-
package javastudy;
-
-
import java.util.ArrayList;
-
-
public class aaa {
-
-
public static void main(String[] args) throws Exception {
-
// TODO Auto-generated method stub
-
BookDAO dao=new BookDAO();
-
ArrayList
<Book> list = dao.getbooks();
-
for (Book book : list) {
-
System.out.println(book);
-
}
-
dao.getbooks();
-
}
-
-
}
基本框架已经实现,开始写其功能 可能会遇到很多问题加油!
<strong><span style="color:#000000">目前是1.0 等今后有时间升级2.0 有兴趣的可以问我要代码 项目没完成就不在这发了 下周一在去完善 </span></strong>
<strong><span style="color:#000000">通宵把我之前写的东西重新捋一遍 好累 会的太少 完成2.0 就准备往下学 等以后在此基础上 升级 </span></strong>
<strong><span style="color:#000000">看我博客的同学 如果有兴趣的可以和我一起研究 今天通宵把其他功能全部实现,就差最后一步(搜索还有点小BUG) 加了很多图片音乐 加了个数字时钟 我打算以后做一个UI用户登录 今后学到新的知识再往上加 (本人菜鸟一只 希望大佬指点一二)</span></strong>
-
<body>
-
<center>
-
<marquee width=
"300" direction=left height=
"50"><center><font color=
"pink" size=
"7" face=
"华文彩云"><b>图书信息</b></font></center></marquee>
-
<embed src=
"music/3.mp3" width=
"0.01" height=
"0.01"></embed>
-
<table
class=
"gridtable" id=
"gridtable" height=
"400" width=
"30%">
-
<%
-
request.setCharacterEncoding(
"UTF-8");
-
response.setCharacterEncoding(
"UTF-8");
-
String name=request.getParameter(
"name");
-
//name+="%"+name+"%";
-
String condition=request.getParameter(
"condition");
-
// out.print(name);
-
BookDAO dao=
new BookDAO();
-
ArrayList<Book> list=dao.serachBook(name);
-
if(list==
null || list.size()==
0){
-
out.print(
"没用找到相应的图书");
-
}
-
for(
int i=
0;i<list.size();i++){
-
Book book=list.get(i);
-
%>
-
-
<tr>
-
<td><%=i+
1 %><input type=
"hidden" name=
"id" value=
"<%=book.getId()%>"></td>
-
<td><h3><%=book.getName() %></h3></td>
-
<td><h3><%=book.getAuthor() %></h3></td>
-
<td><h3><%=book.getPrice() %></h3></td>
-
<td><h3><%=book.getPublisher() %></h3></td>
-
</tr>
-
<%
-
}
-
%>
-
-
</table>
-
</center>
-
</body>
-
<body>
-
<embed src="music/3.mp3" width="0.01" height="0.01">
</embed>
-
<center>
-
<div class="divMain">
-
<marquee width="300" direction=left height="50">
<center>
<font color="#0000ff" size="7" face="华文彩云">
<b>跟小刘学java 从入门到精通!
</b>
</font>
</center>
</marquee>
-
-
<h1 align="center" class="STYLE1">请输入要查询的关键字
</h1>
-
<form action="seek.jsp" method="post">
-
<h2>
<span class="STYLE2"> 请选择查询条件
</span>
</h2>
-
<select name="condition">
-
<option value ="author">作者
</option>
-
<option value ="name" selected="selected">书名
</option>
-
</select>
-
<span class="STYLE3">请输入
</span>
-
<input type="text" name="name" />
<br />
-
<input type="submit" value="提交信息 " />
-
</form>
-
-
</div>
-
</center>
-
</body>
全部代码都放在我博客上了,可以去下载完整的项目
源码下载地址
https://download.csdn.net/download/liuy521/10222825
转载:https://blog.csdn.net/LiuY521/article/details/79008415