用mysql编译:Java Web开发实战经典(基础篇)课后题答案 060502_〖第05章:JSP基础语法〗_第02题

以下为调试通过代码,需要更详细的可以私信问我
要修改的地方:
1、mysql和oracle之间的转换:
String sql = 	"SELECT COUNT(empno) FROM emp " + 
					" WHERE empno LIKE ? OR ename LIKE ? OR job LIKE ? OR TO_CHAR(hiredate,'yyyy-mm-dd') LIKE ? " + 
					" OR sal LIKE ? OR comm LIKE ? " ;
改为
String sql = 	"SELECT COUNT(empno) FROM emp " + 
					" WHERE empno LIKE ? OR ename LIKE ? OR job LIKE ? OR DATE_FORMAT(hiredate,'yyyy-mm-dd') LIKE ? " + 
					" OR sal LIKE ? OR comm LIKE ? " ;
2、mysql和oracle之间的转换:
	
sql = 	"SELECT * FROM (	" + 
			" SELECT empno,ename,job,hiredate,sal,comm,ROWNUM rn " +
			" FROM emp WHERE (empno LIKE ? OR ename LIKE ? OR job LIKE ? OR TO_CHAR(hiredate,'yyyy-mm-dd') LIKE ? OR sal LIKE ? OR comm LIKE ?) AND " + 
			" ROWNUM<=? ORDER BY empno) temp " + 
			" WHERE temp.rn>? " ;
改为:
sql = " SELECT * FROM emp WHERE empno LIKE ? OR ename LIKE ? OR job LIKE ? OR DATE_FORMAT(hiredate,'yyyy-mm-dd') LIKE ? "+
      "OR sal LIKE ? OR comm LIKE ?  ORDER BY empno LIMIT ? , ? ";
3、
改为
 
  
 
4、数据库的连接:
public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
	public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:MLDN" ;
	public static final String DBUSER = "scott" ;
	public static final String DBPASSWORD = "tiger" ;
改为:
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver"; 
	public static final String DBURL = "jdbc:mysql://localhost:3306/mldn"; 
	public static final String DBUSER = "用户名"; 
	public static final String DBPASSWORD = "t密码"; 
代码如下:
emp_list.jsp
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.sql.*" %>

www.mldnjava.cn,MLDN高端Java培训


	

<%!
	public static final String URL = "emp_list.jsp" ;
%>
<%!
	public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
	public static final String DBURL = "jdbc:mysql://localhost:3306/mldn";
	public static final String DBUSER = "root";
	public static final String DBPASSWORD = "toor";
%>
<%
	Connection conn = null ;
	PreparedStatement pstmt = null ;
	ResultSet rs = null ;
%>
<%
	int currentPage = 1 ;	// 为当前所在的页,默认在第1页
	int lineSize = 3 ;		// 每次显示的记录数
	int allRecorders = 0 ;	// 表示全部的记录数
	String keyWord = request.getParameter("kw"); //接收查询关键字
%>
<%
	try{
		currentPage = Integer.parseInt(request.getParameter("cp")) ;
	} catch(Exception e) {}
	try{
		lineSize = Integer.parseInt(request.getParameter("ls")) ;
	} catch(Exception e) {}
	if(keyWord == null){
	   keyWord = "";
	}
%>
<%
	Class.forName(DBDRIVER) ;
	conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
		String sql = 	"SELECT COUNT(empno) FROM emp " + 
					" WHERE empno LIKE ? OR ename LIKE ? OR job LIKE ? OR DATE_FORMAT(hiredate,'yyyy-mm-dd') LIKE ? " + 
					" OR sal LIKE ? OR comm LIKE ? " ;
	pstmt = conn.prepareStatement(sql) ;
	pstmt.setString(1,"%"+keyWord+"%") ;
	pstmt.setString(2,"%"+keyWord+"%") ;
	pstmt.setString(3,"%"+keyWord+"%") ;
	pstmt.setString(4,"%"+keyWord+"%") ;
	pstmt.setString(5,"%"+keyWord+"%") ;
	pstmt.setString(6,"%"+keyWord+"%") ;
	rs = pstmt.executeQuery() ;
	if(rs.next()){	// 取得全部的记录数
		allRecorders = rs.getInt(1) ;
	}
%>

雇员列表

<% sql = " SELECT * FROM emp WHERE empno LIKE ? OR ename LIKE ? OR job LIKE ? OR DATE_FORMAT(hiredate,'yyyy-mm-dd') LIKE ? "+ "OR sal LIKE ? OR comm LIKE ? ORDER BY empno LIMIT ? , ? "; pstmt = conn.prepareStatement(sql) ; pstmt.setString(1,"%"+keyWord+"%"); pstmt.setString(2,"%"+keyWord+"%"); pstmt.setString(3,"%"+keyWord+"%"); pstmt.setString(4,"%"+keyWord+"%"); pstmt.setString(5,"%"+keyWord+"%"); pstmt.setString(6,"%"+keyWord+"%"); pstmt.setInt(7,(currentPage -1)* lineSize) ; pstmt.setInt(8,lineSize) ; rs = pstmt.executeQuery() ; %> <% while(rs.next()){ int empno = rs.getInt(1) ; String ename = rs.getString(2) ; String job = rs.getString(3) ; Date hiredate = rs.getDate(4) ; double sal = rs.getDouble(5) ; double comm = rs.getDouble(6) ; %> <% } %>
编号 姓名 职位 雇佣日期 工资 奖金
<%=empno%> <%=ename%> <%=job%> <%=hiredate%> <%=sal%> <%=comm%>
<% conn.close() ; %>
split_page_plugin.jsp
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%
	int currentPage = 1 ;	// 为当前所在的页,默认在第1页
	int lineSize = 3 ;		// 每次显示的记录数
	long allRecorders = 0 ;	// 表示全部的记录数
	long pageSize = 1 ;		// 表示全部的页数(尾页)
	int lsData[] = {1,3,5,7,9,10,15,20,25,30,50,100} ;
	String keyWord = request.getParameter("kw"); //接收查询关键字
	String url = request.getParameter("url");
%>
<%
	try{
		currentPage = Integer.parseInt(request.getParameter("cp")) ;
	} catch(Exception e) {}
	try{
		lineSize = Integer.parseInt(request.getParameter("ls")) ;
	} catch(Exception e) {}
	try{
		allRecorders = Long.parseLong(request.getParameter("allRecorders")) ;
	} catch(Exception e) {}
	if(keyWord == null){
	   keyWord = "";
	}
%>

<%
	pageSize = (allRecorders + lineSize -1) / lineSize ;
	if(pageSize == 0){
	   pageSize = 1;
	}
%>

输入查询关键字:
> > > > 跳转到第页 每页显示



 
  
 
 

你可能感兴趣的:(Java)