關於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/zh-tw/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

發表回復

登錄後才能評論