⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 changebookinf.java

📁 书店实现用struts实现的一个系统 采用mysql数据库
💻 JAVA
字号:
/**
 * 该Servlet接受来自changBookInf.jsp的请求
 * 用于修改图书信息
 */
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import bean.BookInf;
import bean.DBClass;

public class ChangeBookInf extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		HttpSession session=request.getSession();
		String toJsp="/bookManage.jsp";	
		BookInf errInf=new BookInf();
		request.setCharacterEncoding("gb2312");
		String id=request.getParameter("id");
		String name=request.getParameter("name").trim();
		String author=request.getParameter("author").trim();
		String publisher=request.getParameter("publisher").trim();
		String type=request.getParameter("type");
		String price=request.getParameter("price").trim();
		String ifNew=request.getParameter("ifNew").trim();
        DBClass db=new DBClass();
		
		/* 检查表单信息的合法性 */
		if(name.equals("")){
			errInf.setName("*本项必须填写!");
			toJsp="/changeBookInf.jsp?bookId="+id;
		} 
		

		if(author.equals("")){
			errInf.setAuthor("*本项必须填写!");
			toJsp="/changeBookInf.jsp?bookId="+id;
		}
		

		if(publisher.equals("")){
			errInf.setPublisher("*本项必须填写!");
			toJsp="/changeBookInf.jsp?bookId="+id;
		}

		if(type.equals("0")){
			errInf.setType("*本项必须填写!");
			toJsp="/changeBookInf.jsp?bookId="+id;
		}

		if(price.equals("")){
			errInf.setPrice("*本项必须填写!");
			toJsp="/changeBookInf.jsp?bookId="+id;
		}
		
		/* 更新数据库 */
		if(toJsp.equals("/bookManage.jsp")){
			String command="UPDATE book SET " 
			              +"name="+ "\'"+ name + "\'"+ ","
			              +"author="+ "\'"+ author + "\'"+ ","
			              +"publisher="+ "\'"+ publisher + "\'"+ ","
			              +"type="+ "\'"+ type + "\'"+ ","
			              +"price="+ "\'"+ price + "\'"+ ","
			              +"ifNew="+ "\'"+ ifNew + "\'"
			              +"WHERE id=" +"\'"+ id + "\'";
	        db.connect();
	        db.executeUpdate(command);
			db.closeConnection();
		}  
		request.setAttribute("errInf", errInf);
		RequestDispatcher dispatcher = 
		                  request.getRequestDispatcher(toJsp);		            
		dispatcher.forward(request, response);
		
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
	throws ServletException, IOException {
		doGet(request, response);
	}
	
	/* 这个类用于判断帐号与密码是否匹配 */
	static public boolean isMatch(DBClass db,String id,String pwd){
		boolean bool =false;
		String query="SELECT * FROM customer WHERE id="+"\'"+ id + "\'"
						+" AND password="+"\'"+ pwd + "\'";
			db.connect();
			ResultSet resultset=db.executeQuery(query);
			try{
				if(resultset.next()){
					bool=true;
				} 
			}catch(SQLException sqle){
			    System.err.println("Erro with connection:"+sqle);
			}
			db.closeConnection();
			return bool;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -