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

原创 2015年07月08日 20:36:55
以下为调试通过代码,需要更详细的可以私信问我
要修改的地方:
1、mysql和oracle之间的转换:
String sql = <span style="white-space:pre">	</span>"SELECT COUNT(empno) FROM emp " + 
<span style="white-space:pre">					</span>" WHERE empno LIKE ? OR ename LIKE ? OR job LIKE ? OR TO_CHAR(hiredate,'yyyy-mm-dd') LIKE ? " + 
<span style="white-space:pre">					</span>" OR sal LIKE ? OR comm LIKE ? " ;
改为
String sql = <span style="white-space:pre">	</span>"SELECT COUNT(empno) FROM emp " + 
<span style="white-space:pre">					</span>" WHERE empno LIKE ? OR ename LIKE ? OR job LIKE ? OR DATE_FORMAT(hiredate,'yyyy-mm-dd') LIKE ? " + 
<span style="white-space:pre">					</span>" OR sal LIKE ? OR comm LIKE ? " ;
2、<span style="font-family: Arial, Helvetica, sans-serif;">mysql和oracle之间的转换:</span>
<span style="white-space:pre">	</span>
sql = <span style="white-space:pre">	</span>"SELECT * FROM (<span style="white-space:pre">	</span>" + 
<span style="white-space:pre">			</span>" SELECT empno,ename,job,hiredate,sal,comm,ROWNUM rn " +
<span style="white-space:pre">			</span>" 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 " + 
<span style="white-space:pre">			</span>" ROWNUM<=? ORDER BY empno) temp " + 
<span style="white-space:pre">			</span>" 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 ? , ? ";
</pre><pre name="code" class="html">3、<input type="hidden" name="cp" value="1">
<pre name="code" class="html">改为

 <input type="hidden" id="cp" value="1" name="cp">
4、数据库的连接:
public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
<span style="white-space:pre">	</span>public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:MLDN" ;
<span style="white-space:pre">	</span>public static final String DBUSER = "scott" ;
<span style="white-space:pre">	</span>public static final String DBPASSWORD = "tiger" ;
改为:
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver"; 
<span style="white-space:pre">	</span>public static final String DBURL = "jdbc:mysql://localhost:3306/mldn"; 
<span style="white-space:pre">	</span>public static final String DBUSER = "用户名"; 
<span style="white-space:pre">	</span>public static final String DBPASSWORD = "t密码"; 
代码如下:
emp_list.jsp
<pre name="code" class="html"><%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.sql.*" %>
<html>
<head><title>www.mldnjava.cn,MLDN高端Java培训</title>
<style type="text/css">
<!--
.STYLE6 {font-size: 12px}
.STYLE10 {font-size: 14px; font-weight: bold; }
-->
</style>
</head>
	<script language="javascript">
		function changeColor(obj,color){
			obj.bgColor = color ;
		}
	</script>
<body>
<%!
	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) ;
	}
%>
<center>
	<h1>雇员列表</h1>
<jsp:include page="split_page_plugin.jsp">
    <jsp:param name="allRecorders" value="<%=allRecorders%>"/>
    <jsp:param name="url" value="<%=URL%>"/>
</jsp:include>
<TABLE BORDER="1" cellpadding="5" cellspacing="0" bgcolor="F2F2F2" width="100%">
	<TR onMouseOver="changeColor(this,'white')" onMouseOut="changeColor(this,'F2F2F2')">
		<td align="center" valign="middle"><span class="STYLE10">编号</span></td>
		<td align="center" valign="middle"><span class="STYLE10">姓名</span></td>
		<td align="center" valign="middle"><span class="STYLE10">职位</span></td>
		<td align="center" valign="middle"><span class="STYLE10">雇佣日期</span></td>
		<td align="center" valign="middle"><span class="STYLE10">工资</span></td>
		<td align="center" valign="middle"><span class="STYLE10">奖金</span></td>
	</TR>
<%
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) ;
%>
	<TR onMouseOver="changeColor(this,'white')" onMouseOut="changeColor(this,'F2F2F2')">
		<td align="center" valign="middle"><span class="STYLE6"><%=empno%></span></td>
		<td align="center" valign="middle"><span class="STYLE6"><%=ename%></span></td>
		<td align="center" valign="middle"><span class="STYLE6"><%=job%></span></td>
		<td align="center" valign="middle"><span class="STYLE6"><%=hiredate%></span></td>
		<td align="center" valign="middle"><span class="STYLE6"><%=sal%></span></td>
		<td align="center" valign="middle"><span class="STYLE6"><%=comm%></span></td>
	</TR>
<%
	}
%>
</table>
<%
	conn.close() ;
%>
</center>
</body>
</html>
</pre><pre name="code" class="html">split_page_plugin.jsp
</pre><pre name="code" class="html"><%@ 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;
	}
%>
<script language="javascript">
	function go(num){
		document.getElementById("cp").value = num ;
		document.spform.submit() ;	// 表单提交
	}
</script>
<form name="spform" action="<%=url%>" method="post">
    输入查询关键字:<input type="text" name="kw" value="<%=keyWord%>"><input type="submit" value="查询"><br>
	<input type="button" value="首页" onclick="go(1)" <%=currentPage==1?"DISABLED":""%>>
	<input type="button" value="上一页" onclick="go(<%=currentPage-1%>)" <%=currentPage==1?"DISABLED":""%>>
	<input type="button" value="下一页" onclick="go(<%=currentPage+1%>)" <%=currentPage==pageSize?"DISABLED":""%>>
	<input type="button" value="尾页" onclick="go(<%=pageSize%>)" <%=currentPage==pageSize?"DISABLED":""%>>
	跳转到第<select name="selcp" onchange="go(this.value)">
		<%
			for(int x=1;x<=pageSize;x++){
		%>
				<option value="<%=x%>" <%=x==currentPage?"SELECTED":""%>><%=x%></option>
		<%
			}
		%>
	</select>页
	每页显示
		<select name="ls" onchange="go(1)">
		<%
			for(int x=0;x<lsData.length;x++){
		%>
			<option value="<%=lsData[x]%>" <%=lsData[x]==lineSize?"SELECTED":""%>><%=lsData[x]%></option>
		<%
			}
		%>
		</select>
	条
	
	<input type="hidden" id="cp" value="1" name="cp">
</form>





                

梁勇2011JAVA语言程序设计基础篇第16章编程题16.8

鼠标事件
  • vivi_and_qiao
  • vivi_and_qiao
  • 2016年12月07日 17:31
  • 480

Java语言程序设计基础篇原书第十版第六章编程练习题答案

第六章 方法的部分代码,会尽快补全
  • Tanganling
  • Tanganling
  • 2016年11月27日 20:25
  • 3775

java语言程序设计基础篇第十二章编程练习题

1package yongheng; import java.util.Scanner;public class Calculator { public static void main(Str...
  • gyhguoge01234
  • gyhguoge01234
  • 2016年09月08日 22:21
  • 2684

Java语言程序设计(基础篇)原书第十版 课后习题 第三章

// 3.11 Scanner input = new Scanner(System.in); System.out.print("Enter a month and a year:");...
  • zjjoebloggs
  • zjjoebloggs
  • 2015年10月06日 21:58
  • 7670

java语言程序设计基础篇第十一章编程练习题

1package yongheng; import java.util.Scanner;public class Main { public static void main(String a...
  • gyhguoge01234
  • gyhguoge01234
  • 2016年09月08日 22:19
  • 2343

java语言程序设计基础篇第六章编程练习题

1 import java.util.Scanner; public class Main{ public static void main(String[] args){ final int...
  • gyhguoge01234
  • gyhguoge01234
  • 2016年07月05日 22:26
  • 4087

Java语言程序设计基础篇第十版第一章编程练习题答案

Java语言程序设计基础篇第十版第一章编程练习题答案详解
  • Tanganling
  • Tanganling
  • 2016年10月20日 15:43
  • 2379

Java语言程序设计基础篇原书第十版第五章综合题答案

循环的综合题。需要其它讲解的话,评论中留下题号。
  • Tanganling
  • Tanganling
  • 2016年11月13日 13:01
  • 3812

Java语言程序设计(基础篇)原书第十版 课后习题 第五章

import java.util.Scanner; public class Code_Practice_5 { public static void main(String[] args) {...
  • zjjoebloggs
  • zjjoebloggs
  • 2015年10月21日 20:27
  • 10111

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

以下为调试通过代码,需要更详细的可以私信我要修改的地方:1、改为  2、数据库的连接:public static final String DBDRIVER = "oracle.jdbc.drive...
  • xiaomi817
  • xiaomi817
  • 2015年07月08日 20:27
  • 911
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:用mysql编译:Java Web开发实战经典(基础篇)课后题答案 060502_〖第05章:JSP基础语法〗_第02题
举报原因:
原因补充:

(最多只允许输入30个字)