package Servlet;
import JavaBean.BookInf;
import JavaBean.DBbean;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class Book_Insert extends HttpServlet
{
public Book_Insert()
{
}
public void doGet(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
javax.servlet.http.HttpSession httpsession = httpservletrequest.getSession();
String s = "/Book_Manage.jsp";
BookInf bookinf = new BookInf();
httpservletrequest.setCharacterEncoding("gb2312");
String s1 = httpservletrequest.getParameter("id").trim();
String s2 = httpservletrequest.getParameter("name").trim();
String s3 = httpservletrequest.getParameter("author").trim();
String s4 = httpservletrequest.getParameter("pubhouse").trim();
String s5 = httpservletrequest.getParameter("type").trim();
String s6 = httpservletrequest.getParameter("price").trim();
String s7 = httpservletrequest.getParameter("ifNew").trim();
DBbean dbclass = new DBbean();
if(s1.equals(""))
{
bookinf.setId("*\u672C\u9879\u5FC5\u987B\u586B\u5199!");
s = "/Book_Insert.jsp";
} else
if(isExist(s1))
{
bookinf.setId("*\u4E0E\u5176\u5B83\u56FE\u4E66\u7684ID\u51B2\u7A81\uFF01");
s = "/Book_Insert.jsp";
}
if(s2.equals(""))
{
bookinf.setName("*\u672C\u9879\u5FC5\u987B\u586B\u5199!");
s = "/Book_Insert.jsp";
}
if(s3.equals(""))
{
bookinf.setAuthor("*\u672C\u9879\u5FC5\u987B\u586B\u5199!");
s = "/Book_Insert.jsp";
}
if(s4.equals(""))
{
bookinf.setPubhouse("*\u672C\u9879\u5FC5\u987B\u586B\u5199!");
s = "/Book_Insert.jsp";
}
if(s5.equals("0"))
{
bookinf.setType("*\u672C\u9879\u5FC5\u987B\u586B\u5199!");
s = "/Book_Insert.jsp";
}
if(s6.equals(""))
{
bookinf.setPrice("*\u672C\u9879\u5FC5\u987B\u586B\u5199!");
s = "/Book_Insert.jsp";
}
if(s.equals("/Book_Manage.jsp"))
{
String s8 = (new StringBuilder()).append("insert into book values('").append(s1).append("'").append(",").append("'").append(s2).append("'").append(",").append("'").append(s3).append("'").append(",").append("'").append(s4).append("'").append(",").append("'").append(s5).append("'").append(",").append("'").append(s7).append("'").append(",").append("'").append(s6).append("'").append(")").toString();
dbclass.connect();
dbclass.executeUpdate(s8);
dbclass.closeConnection();
}
httpservletrequest.setAttribute("errInf", bookinf);
RequestDispatcher requestdispatcher = httpservletrequest.getRequestDispatcher(s);
requestdispatcher.forward(httpservletrequest, httpservletresponse);
}
public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
doGet(httpservletrequest, httpservletresponse);
}
public boolean isExist(String s)
{
boolean flag = false;
String s1 = (new StringBuilder()).append("select * from book where b_id='").append(s).append("'").toString();
DBbean dbclass = new DBbean();
dbclass.connect();
ResultSet resultset = dbclass.executeQuery(s1);
try
{
if(resultset.next())
flag = true;
}
catch(SQLException sqlexception)
{
System.err.println((new StringBuilder()).append("Erro with connection:").append(sqlexception).toString());
}
dbclass.closeConnection();
return flag;
}
}