关闭

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

标签: Java Web开发实战经典基础篇课后题第05章JSP基础语法_第01题课后题答案mysql
802人阅读 评论(0) 收藏 举报
分类:
以下为调试通过代码,需要更详细的可以私信我
要修改的地方:
1、<input type="hidden" name="cp" value="1">
<pre name="code" class="html">改为

 <input type="hidden" id="cp" value="1" name="cp">
2、数据库的连接:
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密码"; 
</pre><pre name="code" class="html">
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.sql.*" %>
代码如下:
</pre><pre name="code" class="html">
emp_list_false.jsp
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.sql.*" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<html>
  <head>
    <title>My JSP 'emp_list_true.jsp' starting page</title>
  </head>
  <script language="javascript">
		function changeColor(obj,color){
			obj.bgColor = color ;
		}
	</script>
  
  <body>
  <%! 
  public static final String URL = "emp_list_false.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;
    int lineSize = 5;
    int allRecorders = 0;
    int pageSize = 1;
    int lsData[]={1,3,5,7,9,10,15,20,25,30,35,100};
 %>
 <%
   try{
   currentPage = Integer.parseInt(request.getParameter("cp"));
  }catch(Exception e){}
  try{
  lineSize = Integer.parseInt(request.getParameter("ls"));
  }catch(Exception e){}
  %>
<%
   Class.forName(DBDRIVER);
   conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
   String sql = "SELECT COUNT(empno) FROM emp ";
   pstmt = conn.prepareStatement(sql);
   rs = pstmt.executeQuery();
   if(rs.next()){
     allRecorders = rs.getInt(1);
   }
%>
<center> 
	<h1>雇员列表</h1>
<script language="javascript">
   function go(num){
      document.getElementById("cp").value = num;
     
      document.spform.submit();
   }
</script>
<%
  pageSize = (allRecorders +lineSize -1)/lineSize ;
 %>
 
 <%
   sql = "SELECT empno,ename,job,hiredate,sal,comm FROM emp ORDER BY empno";
   pstmt = conn.prepareStatement(sql);
   rs = pstmt.executeQuery();
  %>
<form name="spform" action="<%=URL%>" method="post">
   <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 %>)"> 
   跳转到<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>
<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>
 <%
 for(int x=0;x<(currentPage-1)*lineSize;x++){
    rs.next();
 }
 for(int x=0;x<lineSize;x++){
    if(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>





























0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5353次
    • 积分:217
    • 等级:
    • 排名:千里之外
    • 原创:17篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    最新评论