关于jsp连表查询dao代码的信息

本文目录一览:

连接数据库进行查询,怎样把查询到的结果在jsp中显示,在servlet使用Dao中的方法是什么?

servlet配置到你的wed.xml中。

从数据库中获取到的数据放进响应中然后通过%=request.getAttribute(“bookId1”) %获取对应的值当然仅仅有servlet和jsp页面是不够的还须要的是servlet配置到你的wed.xml中。这样servlet才干够使用。

具体代码如下:

package bookConnUtil;

import java.io.IOException;

import java.sql.SQLException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class selectBook extends HttpServlet {

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

// TODO Auto-generated method stub

jsp查询数据库代码

这种方法并不是很常用,只是页面的相关操作,代码太多,所以中间省略了部分%

Object userUID=session.getAttribute(“USERID”);

String uid=””;

if(userUID!=null)

{

uid=(String)userUID;

}

else

{ response.sendRedirect(“../Public/loseSession.jsp”);}

try

{

String today=DateUtils.getInstance().getToday();

String userIP=request.getRemoteAddr();

String sql=””;

String getPage=request.getParameter(“toPage”);

sql=cu.exchange(request.getParameter(“sql”)); if (sql==null||sql.equals(“”)){

String getDepId=””;

String getPoliticalPosition=””;

String getTechnicalPosition=””;

String getPoliticalLevel=””;

String getTechnicalLevel=””;

String getDegreeCode=””;

String getGrade=””;

String getLongevity=””;String getAllowance=””;

long depId=0;

long degreeCode=0;

long politicalPosition=0;

long technicalPosition=0;

long politicalLevel=0;

long technicalLevel=0;

long grade=0;

long longevity=0;

long allowance=0;

String[] postID=null;

String post=””;

String userID=””;

String userName=””;

String address=””;

getDepId=request.getParameter(“depId”);

depId=Long.parseLong(getDepId.trim());getDegreeCode=request.getParameter(“degreeCode”);

if (getDegreeCode!=null) {

degreeCode=Long.parseLong(getDegreeCode.trim());

}

getPoliticalPosition=request.getParameter(“politicalPosition”);

if (getPoliticalPosition!=null) {

politicalPosition=Long.parseLong(getPoliticalPosition.trim());

}

post=request.getParameter(“post”);

if(post!=nullpost.trim().equals(“1”))

postID=request.getParameterValues(“dyourlocation”);

getTechnicalPosition=request.getParameter(“technicalPosition”);if (getTechnicalPosition!=null) {

technicalPosition=Long.parseLong(getTechnicalPosition.trim());

}

getPoliticalLevel=request.getParameter(“politicalLevel”);if (getPoliticalLevel!=null) {politicalLevel=Long.parseLong(getPoliticalLevel.trim());

}getTechnicalLevel=request.getParameter(“technicalLevel”);if (getTechnicalLevel!=null) {

technicalLevel=Long.parseLong(getTechnicalLevel.trim());

}getGrade=request.getParameter(“grade”);if (getGrade!=null) {

grade=Long.parseLong(getGrade.trim());

}getLongevity=request.getParameter(“longevity”);if (getLongevity!=null) {

longevity=Long.parseLong(getLongevity.trim());

}getAllowance=request.getParameter(“allowance”);

if (getAllowance!=null) {

allowance=Long.parseLong(getAllowance.trim());

}

userID=ParamUtils.getParameter(request,”userID”);//用户代码

userName=ParamUtils.getParameter(request,”userName”);

//用户名称

address=ParamUtils.getParameter(request,”address”);

//得到要转入的页面sql=”select org_user.* from org_user,org_detail where org_user.user_id=org_detail.user_id”;

if (depId!=0)

{

sql=sql+” and org_user.department_id=”+depId+””;

}if (userID!=nulluserID!=””){

sql=sql+” and org_user.user_id='”+userID+”‘”;

}

if (userName!=nulluserName!=””)

{

sql=sql+” and org_user.name like ‘%”+userName+”%'”;

}if (address!=nulladdress!=””)

{

sql=sql+” and org_user.address like ‘%”+address+”%'”;

}

if(post!=null!post.trim().equals(“0”))

{

sql=sql+” and org_detail.post in (“;

for(int i=0;ipostID.length;i++)

{

sql=sql+postID[i];

if(i!=(postID.length-1))

sql=sql+”,”;

}

sql=sql+”)”;

}

if (degreeCode!=0)

{

sql=sql+” and org_detail.degree=”+degreeCode+””;

}

if (politicalPosition!=0)

{

sql=sql+” and org_detail.politicalPosition=”+politicalPosition+””;

}

if (technicalPosition!=0)

{

sql=sql+” and org_detail.technicalPosition=”+technicalPosition+””;

}

if (technicalPosition!=0)

{

sql=sql+” and org_detail.politicalLevel=”+politicalLevel+””;

}

if (grade!=0)

{

sql=sql+” and org_detail.grade=”+grade+””;

}

if (longevity!=0)

{

sql=sql+” and org_detail.longevity=”+longevity+””;

}

if(getAllowance!=null)

{

if (allowance==0||allowance==1)

{

sql=sql+” and org_detail.allowance=”+allowance+””;

}

}

}

User[] userList =null;

userList=ur.complexSearch(sql);

log.addLog(1,1,1,uid,userIP,uid+”于(“+today+”)查询员工”);int cnt=userList.length;//总记录数

int pageSize=10;//每页显示记录数

int curPage=1;//当前页

int cntPage;//总页数

int m=1; if (cnt0)

{

if (cnt%pageSize==0)

cntPage=cnt/pageSize;

else

cntPage=cnt/pageSize+1;

}

else

cntPage=0; if (getPage==null)

{

getPage=”1″;

curPage=1;

}

else

curPage=Integer.parseInt(getPage.trim());

%form name=”thisform” action=”userSearchDel.jsp” method=”post”

table bgColor=”#FFFFFF” border=”1″ borderColorDark=”#ffffff” borderColorLight=”#c0c0c0″ cellSpacing=”0″ width=”95%” cellpadding=”4″

tr height=”25″ align=”center” bgcolor=”#959595″

td  /td

tdfont color=”#FFFFFF”姓名/font/td

tdfont color=”#FFFFFF”性别/font/td

td font color=”#FFFFFF”所在部门/font/td

td font color=”#FFFFFF”办公地址/font/td

tdfont color=”#FFFFFF”联系电话/font/td

/tr

%

int u=1;

for(int i=0;icnt;i++)

{

//显示记录的起始位置

int j=(curPage-1)*pageSize;

//显示记录的末位置

int k=curPage*pageSize;

//只显示page_size条数据

if(mjm=k)

{

String userId=userList[i].getUserID();

String gender=userList[i].getGender();

String name=userList[i].getName();

String tel=userList[i].getTel();

String useAddress=userList[i].getAddress();

long dep_id=userList[i].getDepID();

String dname=dep.getName(userList[i].getDepID());

if(tel==null||tel.equals(“null”))

tel=””;

if(useAddress==null||useAddress.equals(“null”))

useAddress=””;

//判断没一行该输出的颜色

if((u%2)==0)

{

out.println(“tr bgcolor=#D7D7D7 onMouseOver=this.style.backgroundColor=’#fcd4d7′; onMouseOut=if(selectedItem!=this.id){this.style.backgroundColor=’#D7D7D7′;}”);

}

else

{

out.println(“tr onMouseOver=this.style.backgroundColor=’#fcd4d7′; onMouseOut=if(selectedItem!=this.id){this.style.backgroundColor=’#FFFFFF’;}”);

}

u++;

%

td align=centerinput type=”checkbox” name=”checkbox” id=”CHK_+%=userId%” value=”%=userId%”/td

td align=center

%

long departmentID=ur.getDepID(uid);

if(up.haveRight(uid,”USER_COMPLEXQUERY”)==true)

{

%

a href=”userAnalyze.jsp?userId=%=userId%dep_id=%=dep_id%toPage=%=curPage%”font color=”#330099″

%

}

else if(up.haveRight(uid,”DEPARTMENT_PLATFORM”)==truedep_id==departmentID)

{

%

a href=”userAnalyze.jsp?userId=%=userId%dep_id=%=dep_id%toPage=%=curPage%”font color=”#330099″

%

}

%

%=name%/font/a/td

%

if(gender.trim().equals(“0”))

out.println(“td align=center男/td”);

else

out.println(“td align=center女/td”);

%

td align=centera href=”depintro.jsp?id=%=dep_id%toPage=%=curPage%”font color=”#330099″%=dname%/font/a/td

td align=center %=useAddress%/td

td align=center %=tel%/td

/tr

%

}//end if m++;

}//end for

%

input type=hidden name=”sql” value=”%=sql%”

input type=hidden name=”page” value=”%=curPage%”

/table

/form

form name=pageform action=”searchAction.jsp” method=”post”

table border=”0″ width=”95%”

tr

td align=”center”第%=curPage%页/共%=cntPage%页/td

td align=”left”

img name=”firstpage” src=”../Image/btn_firstpage.gif” style=”cursor:hand” onclick=”pageClick(1)” %=curPage1?” “:”disabled”%

img name=”prepage” src=”../Image/btn_prepage.gif” style=”cursor:hand” onclick=”pageClick(%=curPage-1%)” %=curPage1?” “:”disabled”%

img name=”nextpage” src=”../Image/btn_nextpage.gif” style=”cursor:hand” onclick=”pageClick(%=curPage+1%)” %=curPagecntPage?” “:”disabled”%

img name=”lastpage” src=”../Image/btn_lastpage.gif” style=”cursor:hand” onclick=”pageClick(%=cntPage%)” %=curPagecntPage?” “:”disabled”%

/td

td align=left width=55%

到第input type=”text” name=”goPage” size=”4″页

img border=”0″ src=”../Image/icon_search.gif” style=”cursor:hand” onclick=”aPage()”

/td

/trtr

td colspan=”3″ valign=”middle” width=”23%” align=”right”

%

if(up.haveRight(uid,”USER_MANAGE”)==true)

{

%

img border=”0″ src=”../Image/btn_delete.gif” style=”cursor:hand” onclick=”delUser()”

%}%

img border=”0″ src=”../Image/btn_return.gif” style=”cursor:hand” onclick=”back()”

/td

input type=hidden name=”sql” value=”%=sql%”

input type=hidden name=”toPage”

/td

/tr

/table

/form

br

br

/td

/tr

/table

/td

/tr

/table

%

}//end else

}//end try

catch(Exception e)

{

out.println(e.getMessage());

}

%

/td

/tr

/table

jsp页面调用dao方法问题

jsp没办法直接调用到DAO层的。

解释:

因为根据三层结构,jsp属于view层,之后是controler层,最好才是model层。或者是jsp为action,之后是server层,之后是dao层。不管怎么算,都没办法直接调用的。

备注:

上面的只是代码规范,但是提问非要这么干的话,也没问题,直接在java类中action方法下,调用Dao方法即可。

JSP连接数据库的代码

Class.forName(“com.mysql.jdbc.Driver”);

Connection conn=DriverManager.getConnection(“jdbc:mysql://”+host+”/”+dbname,username,password);

Statement Stmt=conn.createStatement();

ResultSet rs=Stmt.executeQuery(sql);

这是基本的数据库操作方法

大概流程就是加载驱动类,创建连接,执行数据库操作,关闭

写了一个Dao,连接数据库进行查询,怎样把查询到的结果在jsp中显示,在servlet在使用Dao中的方法,

jsp 直接写代码好了 记得把dao倒进来就行了

%@page import=”entity.*” %

%

UserDao dao =…..

ListUser users = dao.getUser…….

for( int i =0 ;iusers.size();i++){

User user = users.get(i);

%

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/183071.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-11-24 16:26
下一篇 2024-11-24 16:26

相关推荐

  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python字符串宽度不限制怎么打代码

    本文将为大家详细介绍Python字符串宽度不限制时如何打代码的几个方面。 一、保持代码风格的统一 在Python字符串宽度不限制的情况下,我们可以写出很长很长的一行代码。但是,为了…

    编程 2025-04-29
  • Python基础代码用法介绍

    本文将从多个方面对Python基础代码进行解析和详细阐述,力求让读者深刻理解Python基础代码。通过本文的学习,相信大家对Python的学习和应用会更加轻松和高效。 一、变量和数…

    编程 2025-04-29
  • 仓库管理系统代码设计Python

    这篇文章将详细探讨如何设计一个基于Python的仓库管理系统。 一、基本需求 在着手设计之前,我们首先需要确定仓库管理系统的基本需求。 我们可以将需求分为以下几个方面: 1、库存管…

    编程 2025-04-29
  • Python满天星代码:让编程变得更加简单

    本文将从多个方面详细阐述Python满天星代码,为大家介绍它的优点以及如何在编程中使用。无论是刚刚接触编程还是资深程序员,都能从中获得一定的收获。 一、简介 Python满天星代码…

    编程 2025-04-29
  • 写代码新手教程

    本文将从语言选择、学习方法、编码规范以及常见问题解答等多个方面,为编程新手提供实用、简明的教程。 一、语言选择 作为编程新手,选择一门编程语言是很关键的一步。以下是几个有代表性的编…

    编程 2025-04-29
  • Python实现简易心形代码

    在这个文章中,我们将会介绍如何用Python语言编写一个非常简单的代码来生成一个心形图案。我们将会从安装Python开始介绍,逐步深入了解如何实现这一任务。 一、安装Python …

    编程 2025-04-29
  • 怎么写不影响Python运行的长段代码

    在Python编程的过程中,我们不可避免地需要编写一些长段代码,包括函数、类、复杂的控制语句等等。在编写这些代码时,我们需要考虑代码可读性、易用性以及对Python运行性能的影响。…

    编程 2025-04-29
  • Python爱心代码动态

    本文将从多个方面详细阐述Python爱心代码动态,包括实现基本原理、应用场景、代码示例等。 一、实现基本原理 Python爱心代码动态使用turtle模块实现。在绘制一个心形的基础…

    编程 2025-04-29
  • 北化教务管理系统介绍及开发代码示例

    本文将从多个方面对北化教务管理系统进行介绍及开发代码示例,帮助开发者更好地理解和应用该系统。 一、项目介绍 北化教务管理系统是一款针对高校学生和教职工的综合信息管理系统。系统实现的…

    编程 2025-04-29

发表回复

登录后才能评论