📄 book.java
字号:
/*
* 创建日期 2005-3-7
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package store;
import java.sql.ResultSet;
import java.util.Date;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
/**
* @author wwx
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class Book {
private long id;
private String bookname;
private String bookclass;
private String author;
private String publish;
private String ISDN;
private String content;
private String regtime;
private float prince;
private int amount;
private int leavnum;
public Book()
{
}
public void SetId(long l)
{
id = l;
}
public void SetBookname(String s)
{
bookname = s;
}
public void SetBookclass(String s)
{
bookclass = s;
}
public void SetAuthor(String s)
{
author = s;
}
public void SetPublish(String s)
{
publish = s;
}
public void SetISDN(String s)
{
ISDN = s;
}
public void SetContent(String s)
{
content = s;
}
public void SetRegtime(String s)
{
regtime = s;
}
public void SetPrince(float f)
{
prince = f;
}
public void SetAmount(int i)
{
amount = i;
}
public void SetLeavnum(int i)
{
leavnum = i;
}
public String GetId()
{
return String.valueOf(id);
}
public String GetBookName()
{
return bookname;
}
public String GetBookclass()
{
return bookclass;
}
public String GetAuthor()
{
return author;
}
public String GetPublish()
{
return publish;
}
public String GetISDN()
{
return ISDN;
}
public String GetContent()
{
return content;
}
public String GetRegtime()
{
return regtime;
}
public float GetPrince()
{
return prince;
}
public int GetAmount()
{
return amount;
}
public int GetLeavnum()
{
return leavnum;
}
public boolean AddBook(HttpServletRequest httpservletrequest)throws Exception
{
String s = null;
boolean flag = false;
String s1 = ParamUtils.getString(httpservletrequest, "bookname", "");
String s2 = ParamUtils.getString(httpservletrequest, "bookclass", "");
String s3 = ParamUtils.getString(httpservletrequest, "author", "");
String s4 = ParamUtils.getString(httpservletrequest, "publish", "");
String s5 = ParamUtils.getString(httpservletrequest, "ISDN", "");
String s6 = ParamUtils.getString(httpservletrequest, "content", "");
String s7 = ParamUtils.getString(httpservletrequest, "prince", "");
String s8 = ParamUtils.getString(httpservletrequest, "amount", "");
if(s1.equals("") || s1 == null)
{
s = s + "You have not input the book's title!<br>";
flag = true;
}
if(s2.equals("") || s2 == null)
{
s = s + "You have not select the class of the book!<br>";
flag = true;
}
if(s3.equals("") || s3 == null)
{
s = s + "You have not input the author's name!<br>";
flag = true;
}
if(s4.equals("") || s4 == null)
{
s = s + "You have not input the publisher's name!<br>";
flag = true;
}
if(s5.equals("") || s5 == null)
{
s = s + "You have not input the ISDN of the book!<br>";
flag = true;
}
if(s6.equals("") || s6 == null)
{
s = s + "You have not input the content!<br>";
flag = true;
}
if(s7.equals("") || s7 == null)
{
s = s + "You have not input the prince of the book!<br>";
flag = true;
}
if(s8.equals("") || s8 == null)
{
s = s + "You have not input the amount of the book!<br>";
flag = true;
}
if(!flag)
{
//添加新书资料到库中
Date udate = new Date();
java.sql.Date sdate = new java.sql.Date(udate.getTime());
DBConnect dbconnect = new DBConnect();
dbconnect.prepareStatement("insert into my_book (bookname,bookclass,author,publish,bookno,content,prince,amount,leav_number,regtime) values (?,?,?,?,?,?,?,?,?,?)");
dbconnect.setString(1, s1);
dbconnect.setInt(2, Integer.parseInt(s2));
dbconnect.setString(3, s3);
dbconnect.setString(4, s4);
dbconnect.setString(5, s5);
dbconnect.setString(6, s6);
dbconnect.setFloat(7, Float.parseFloat(s7));
dbconnect.setFloat(8, Float.parseFloat(s8));
dbconnect.setFloat(9, Float.parseFloat(s8));
dbconnect.setDate(10, sdate);
dbconnect.executeUpdate();
dbconnect.close();
}
if(flag)
throw new Exception(s);
else
return true;
}
public boolean EditBook(HttpServletRequest httpservletrequest) throws Exception
{
String s = null;
boolean flag = false;
String s0 = ParamUtils.getString(httpservletrequest, "id", "");
String s1 = ParamUtils.getString(httpservletrequest, "bookname", "");
String s2 = ParamUtils.getString(httpservletrequest, "bookclass", "");
String s3 = ParamUtils.getString(httpservletrequest, "author", "");
String s4 = ParamUtils.getString(httpservletrequest, "publish", "");
String s5 = ParamUtils.getString(httpservletrequest, "ISDN", "");
String s6 = ParamUtils.getString(httpservletrequest, "content", "");
String s7 = ParamUtils.getString(httpservletrequest, "prince", "");
String s8 = ParamUtils.getString(httpservletrequest, "amount", "");
String s9 = ParamUtils.getString(httpservletrequest, "leavnum", "");
if(s0.equals("") || s0 == null)
{
s = s + "You have not input the book's id!<br>";
flag = true;
}
if(s1.equals("") || s1 == null)
{
s = s + "You have not input the book's title!<br>";
flag = true;
}
if(s2.equals("") || s2 == null)
{
s = s + "You have not select the class of the book!<br>";
flag = true;
}
if(s3.equals("") || s3 == null)
{
s = s + "You have not input the author's name!<br>";
flag = true;
}
if(s4.equals("") || s4 == null)
{
s = s + "You have not input the publisher's name!<br>";
flag = true;
}
if(s5.equals("") || s5 == null)
{
s = s + "You have not input the ISDN of the book!<br>";
flag = true;
}
if(s6.equals("") || s6 == null)
{
s = s + "You have not input the content!<br>";
flag = true;
}
if(s7.equals("") || s7 == null)
{
s = s + "You have not input the prince of the book!<br>";
flag = true;
}
if(s8.equals("") || s8 == null)
{
s = s + "You have not input the amount of the book!<br>";
flag = true;
}
if(s9.equals("") || s9 == null)
{
s = s + "You have not input the number of the books left!<br>";
flag = true;
}
if(!flag)
{
//更新书的信息
DBConnect dbconnect = new DBConnect();
dbconnect.prepareStatement("update my_book set bookname=?,bookclass=?,author=?,publish=?,bookno=?,content=?,prince=?,amount=?,leav_number=? where id=?");
dbconnect.setString(1, s1);
dbconnect.setInt(2, Integer.parseInt(s2));
dbconnect.setString(3, s3);
dbconnect.setString(4, s4);
dbconnect.setString(5, s5);
dbconnect.setString(6, s6);
dbconnect.setFloat(7, Float.parseFloat(s7));
dbconnect.setInt(8, Integer.parseInt(s8));
dbconnect.setInt(9, Integer.parseInt(s9));
dbconnect.setLong(10, Long.parseLong(s0));
dbconnect.executeUpdate();
dbconnect.close();
}
if(flag)
throw new Exception(s);
else
return true;
}
public static void DelBook(String delid) throws Exception
{
String s = "";
boolean flag = false;
if (delid.equals("") || delid==null)
{
s = "You must input the id of the book which you want to delete!<br>";
flag = true;
}
if (!flag)
{
DBConnect dbconnect = new DBConnect();
dbconnect.prepareStatement("delete from My_book where id = ?");
dbconnect.setLong(1, Long.parseLong(delid));
dbconnect.executeUpdate();
}
else
{
throw new Exception(s);
}
}
public static void DelBook(HttpServletRequest httpservletrequest) throws Exception
{
String s = "";
boolean flag = false;
String s1 = ParamUtils.getString(httpservletrequest, "field", "");
String s2 = ParamUtils.getString(httpservletrequest, "keyword", "");
if(s1.equals("") || s1==null)
{
s1 = (String)httpservletrequest.getAttribute("field");
}
if(s2.equals("") || s2==null)
{
s2 = (String)httpservletrequest.getAttribute("keyword");
}
if(s1.equals("") || s1==null)
{
s = s + "You have not input the field!<br>";
flag = true;
}
if(s2.equals("") || s2==null)
{
s = s + "You have not input the keyword!<br>";
flag = true;
}
if(!flag)
{
DBConnect dbconnect = new DBConnect();
dbconnect.prepareStatement("delete from My_book where " + s1 + "=?");
if (s1.equals("id") || s1.equals("bookclass"))
{
dbconnect.setLong(1, Long.parseLong(s2));
}
else
{
dbconnect.setBytes(1, s2.getBytes("GBK"));
}
dbconnect.executeUpdate();
dbconnect.close();
}
if(flag)
{
throw new Exception(s);
}
}
public Vector LoadBook() throws Exception //读取所书的信息
{
Vector vector = new Vector();
DBConnect dbconnect = new DBConnect();
dbconnect.prepareStatement("select * from My_book ORDER BY id desc");
Book book;
for(ResultSet resultset = dbconnect.executeQuery(); resultset.next(); vector.add(book))
{
book = new Book();
book.SetId(resultset.getLong("id"));
book.SetBookname(resultset.getString("bookname"));
book.SetBookclass(resultset.getString("bookclass"));
book.SetAuthor(resultset.getString("author"));
book.SetPublish(resultset.getString("publish"));
book.SetISDN(resultset.getString("bookno"));
book.SetContent(resultset.getString("content"));
book.SetPrince(resultset.getInt("prince"));
book.SetAmount(resultset.getInt("amount"));
book.SetLeavnum(resultset.getInt("leav_number"));
book.SetRegtime(resultset.getString("regtime"));
}
dbconnect.close();
return vector;
}
public static Book LoadBook(String field, String keyword, boolean near) throws Exception
//读取指定书的信息,在field列中查询keyword关键词
//near为true时用like进行模糊匹配,near为false时用=进行精确查询
//程序中只实现了对字符串类型字段的查询以及对id的查询
{
String s = "";
boolean flag = false;
Book book = new Book();
if(!flag)
{
DBConnect dbconnect = new DBConnect();
if(near)
{
keyword = "%"+keyword;
keyword = keyword+"%";
dbconnect.prepareStatement("select * from My_book where "+field+" like ? ORDER BY id desc");
}
else
{
dbconnect.prepareStatement("select * from My_book where "+field+" = ? ORDER BY id desc");
}
if (field.equals("id"))
{
dbconnect.setLong(1, Long.parseLong(keyword));
}
else
{
dbconnect.setString(1, keyword);
}
ResultSet resultset = dbconnect.executeQuery();
if(resultset.next())
{
book = new Book();
book.SetId(resultset.getLong("id"));
book.SetBookname(resultset.getString("bookname"));
book.SetBookclass(resultset.getString("bookclass"));
book.SetAuthor(resultset.getString("author"));
book.SetPublish(resultset.getString("publish"));
book.SetISDN(resultset.getString("bookno"));
book.SetContent(resultset.getString("content"));
book.SetPrince(resultset.getInt("prince"));
book.SetAmount(resultset.getInt("amount"));
book.SetLeavnum(resultset.getInt("leav_number"));
book.SetRegtime(resultset.getString("regtime"));
}
else
{
s = s + "Can not find the book!<br>"+field+"|"+keyword;
flag = true;
}
dbconnect.close();
}
if(flag)
{
throw new Exception(s);
}
else
return book;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -