數據庫jsp查詢語句怎麼寫(jsp實現數據庫查詢)

本文目錄一覽:

jsp數據庫查詢

sql = “select * from my_table where nian_fen”+kai_shi_nian_fen +”and nian_fen”+jie_shu_nian_fen;

你這樣試試,引號內是你要穿給sql的最終值,而顯然kai_shi_nian_fen 不是你要真正加到sql中的,你需要的是他的值,所以要放在引號外面,用加號鏈接。

jsp查詢數據庫裡面的信息怎麼寫代碼啊

jsp先加載你數據庫的驅動,連接數據庫(不同數據庫不一樣),然後再寫sql語句,再執行sql,再通過rs集,把rs結果集讀出顯示在jsp頁面

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頁面,具體怎麼寫代碼

sql 語句”SELECT COUNT(字段名) FORM 表名 WHERE 查詢條件;”

得到的數 int a = request.setAttribute(“num”,變量名);

然後 jsp上要顯示的位置 %=request.getAttribute(“num”,變量名); %

jsp頁面中,條件查詢要怎麼做

示例:

JSP 頁面中:input name = “username”

後台 :String sql = “select * from user where username =?”;//從用戶表中查詢指定用戶名的數據

取得文本框中的值:String name = requset.getParameter(“username”);

然後取得connection對象 conn

Preparestatement pstmt = conn.preparestatement();

pstmt.setString(1,name);//拼接sql語句成為:select * from user where username =(name的值);

最後執行sql語句

jsp頁面在數據庫查詢

String mysql=”select * from task where 1=1″;

if(request.getParameter(“taskdate”)!=null!request.getParameter(“taskdate”).equals(“”))

{

mysql=mysql+” and taskdate=”+request.getParameter(“taskdate”);

}

if(request.getParameter(“tasktype”)!=null!request.getParameter(“tasktype”).equals(“”))

{

mysql=mysql+” and tasktype=”+request.getParameter(“tasktype”);

}

if(request.getParameter(“taskstate”)!=null!request.getParameter(“taskstate”).equals(“”))

{

mysql=mysql+” and tasktype=”+request.getParameter(“taskstate”);

}

if(request.getParameter(“station”)!=null!request.getParameter(“station”).equals(“”))

{

mysql=mysql+” and station=”+request.getParameter(“station”);

}

假設你的完成日期、任務類型、任務狀態和分站分別為taskdate、tasktype、taskstate和station.

用jsp的話,應該這些就可以了。mysql就是你要的SQL語句。

原創文章,作者:HQNQV,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/317327.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
HQNQV的頭像HQNQV
上一篇 2025-01-11 16:27
下一篇 2025-01-11 16:27

相關推薦

  • Python3支持多行語句

    Python3是一種高級編程語言,開發人員可以輕鬆地使用該語言編寫簡單到複雜的代碼。其中Python3支持多行語句,方便開發人員編寫複雜的代碼,提高代碼的可讀性和可維護性。 一、使…

    編程 2025-04-29
  • Python 常用數據庫有哪些?

    在Python編程中,數據庫是不可或缺的一部分。隨着互聯網應用的不斷擴大,處理海量數據已成為一種趨勢。Python有許多成熟的數據庫管理系統,接下來我們將從多個方面介紹Python…

    編程 2025-04-29
  • openeuler安裝數據庫方案

    本文將介紹在openeuler操作系統中安裝數據庫的方案,並提供代碼示例。 一、安裝MariaDB 下面介紹如何在openeuler中安裝MariaDB。 1、更新軟件源 sudo…

    編程 2025-04-29
  • Python for循環語句打印九九乘法表

    本篇文章將詳細介紹如何使用Python的for循環語句打印九九乘法表。打印九九乘法表是我們初學Python時經常練習的一項基礎操作,也是編寫Python程序的基本能力之一。 1、基…

    編程 2025-04-29
  • Python中while語句和for語句的區別

    while語句和for語句是Python中兩種常見的循環語句,它們都可以用於重複執行一段代碼。然而,它們的語法和適用場景有所不同。本文將從多個方面詳細闡述Python中while語…

    編程 2025-04-29
  • Python中自定義函數必須有return語句

    自定義函數是Python中最常見、最基本也是最重要的語句之一。在Python中,自定義函數必須有明確的返回值,即必須要有return語句。本篇文章將從以下幾個方面對此進行詳細闡述。…

    編程 2025-04-29
  • 數據庫第三範式會有刪除插入異常

    如果沒有正確設計數據庫,第三範式可能導致刪除和插入異常。以下是詳細解釋: 一、什麼是第三範式和範式理論? 範式理論是關係數據庫中的一個規範化過程。第三範式是範式理論中的一種常見形式…

    編程 2025-04-29
  • Python中升序排列的if語句

    本文將為大家介紹Python中升序排列的if語句。首先,我們來看一下如何實現。 if a > b: a, b = b, a if b > c: b, c = c, b …

    編程 2025-04-29
  • leveldb和unqlite:兩個高性能的數據庫存儲引擎

    本文將介紹兩款高性能的數據庫存儲引擎:leveldb和unqlite,並從多個方面對它們進行詳細的闡述。 一、leveldb:輕量級的鍵值存儲引擎 1、leveldb概述: lev…

    編程 2025-04-28
  • Python怎麼導入數據庫

    Python是一種高級編程語言。它具有簡單、易讀的語法和廣泛的庫,讓它成為一個靈活和強大的工具。Python的數據庫連接類型可以多種多樣,其中包括MySQL、Oracle、Post…

    編程 2025-04-28

發表回復

登錄後才能評論