jsp页面查询数据库

jsp页面查询数据库,第1张

你的意思是一个submit最多能一次性提交4个文本框内的数据是吧?把4个文本框放进一个表单,提交表单就能4个一起提交,获取的时候用:requestgetParameter("文本框名");

至于查询参数要在sql上写if判断了,如下写法: String cond="";

Long di_id=0l;

if(requestgetParameter("dept")!=null&&!"0"equals(requestgetParameter("dept"))){

di_id=LongvalueOf((requestgetParameter("dept")));

cond+=" and pcr_exdept='"+di_id+"'";

}

判断前台读入的数据中是否有dept这个参数,如果有,就把条件cond赋值为and pcr_exdept='"+di_id+"',,判断完成后把cond传入写sql的函数里,而sql哪儿也要判断下cond 是否为空:

if(!""equals(condition)&&condition!=null){

sql = "select from user_info where 1=1"+condition;

}

这样就可以实现任意属于参数个数实现查询了:)

完整的JSP连接数据库的查询并显示

<%@page contentType="text/html;charset=gb2312"%>

<%@page import="javasql"%>

<HTML><BODY>

<hr align="center" width="55%" color="#991111" size="5"/>

<%

Connection conn = null;

PreparedStatement stmt = null;

ResultSet rs = null;

requestsetCharacterEncoding("GBK");

try{

ClassforName("oraclejdbcdriverOracleDriver");

}

catch(ClassNotFoundException ce){

outprintln(cegetMessage());

}

try{

 String url="jdbc:oracle:thin:@localhost:1521:orcl";

 conn=DriverManagergetConnection(url,"scott","tiger");

 stmt=connprepareStatement("select from emp");

rs=stmtexecuteQuery();

outprint("<TABLE BORDER='1' width='55%' cellpadding='3' align=center>");

outprint("<TR><TD>"+"<b>Empno</b>"+"</TD>");

outprint("<TD>"+"<b>Ename</b>"+"</TD>");

outprint("<TD>"+"<b>Job</b>"+"</TD>");

outprint("<TD>"+"<b>Mgr</b>"+"</TD>");

outprint("<TD>"+"<b>hiredate</b>"+"</TD>");

outprint("<TD align=center>"+"<b>Sal</b>"+"</TD>");

outprint("<TD>"+"<b>Comm</b>"+"</TD>");

outprint("<TD>"+"<b>Deptno</b>"+"</TD></TR>");

while(rsnext()){

outprint("<TR><TD>"+rsgetString(1)+"</TD>");

outprint("<TD>"+rsgetString(2)+"</TD>");

outprint("<TD> "+rsgetString(3)+"</TD>");

outprint("<TD>"+rsgetString(4)+"</TD>");

outprint("<TD>"+rsgetString(5)+"</TD>");

outprint("<TD>"+rsgetString(6)+"</TD>");

outprint("<TD>"+rsgetString(7)+"</TD>");

outprint("<TD>"+rsgetString(8)+"</TD></TR>");

}

outprint("</TABLE>");

rsclose();

stmtclose();

connclose();

}

catch(Exception e){

Systemoutprintln(egetMessage());

}

%>

</BODY></HTML>

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

Object userUID=sessiongetAttribute("USERID");

String uid="";

if(userUID!=null)

{

uid=(String)userUID;

}

else

{ responsesendRedirect("/Public/loseSessionjsp");}

try

{

String today=DateUtilsgetInstance()getToday();

String userIP=requestgetRemoteAddr();

String sql="";

String getPage=requestgetParameter("toPage");

sql=cuexchange(requestgetParameter("sql")); if (sql==null||sqlequals("")){

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=requestgetParameter("depId");

depId=LongparseLong(getDepIdtrim());getDegreeCode=requestgetParameter("degreeCode");

if (getDegreeCode!=null) {

degreeCode=LongparseLong(getDegreeCodetrim());

}

getPoliticalPosition=requestgetParameter("politicalPosition");

if (getPoliticalPosition!=null) {

politicalPosition=LongparseLong(getPoliticalPositiontrim());

}

post=requestgetParameter("post");

if(post!=null&&posttrim()equals("1"))

postID=requestgetParameterValues("dyourlocation");

getTechnicalPosition=requestgetParameter("technicalPosition");if (getTechnicalPosition!=null) {

technicalPosition=LongparseLong(getTechnicalPositiontrim());

}

getPoliticalLevel=requestgetParameter("politicalLevel");if (getPoliticalLevel!=null) {politicalLevel=LongparseLong(getPoliticalLeveltrim());

}getTechnicalLevel=requestgetParameter("technicalLevel");if (getTechnicalLevel!=null) {

technicalLevel=LongparseLong(getTechnicalLeveltrim());

}getGrade=requestgetParameter("grade");if (getGrade!=null) {

grade=LongparseLong(getGradetrim());

}getLongevity=requestgetParameter("longevity");if (getLongevity!=null) {

longevity=LongparseLong(getLongevitytrim());

}getAllowance=requestgetParameter("allowance");

if (getAllowance!=null) {

allowance=LongparseLong(getAllowancetrim());

}

userID=ParamUtilsgetParameter(request,"userID");//用户代码

userName=ParamUtilsgetParameter(request,"userName");

//用户名称

address=ParamUtilsgetParameter(request,"address");

//得到要转入的页面sql="select org_user from org_user,org_detail where org_useruser_id=org_detailuser_id";

if (depId!=0)

{

sql=sql+" and org_userdepartment_id="+depId+"";

}if (userID!=null&&userID!=""){

sql=sql+" and org_useruser_id='"+userID+"'";

}

if (userName!=null&&userName!="")

{

sql=sql+" and org_username like '%"+userName+"%'";

}if (address!=null&&address!="")

{

sql=sql+" and org_useraddress like '%"+address+"%'";

}

if(post!=null&&!posttrim()equals("0"))

{

sql=sql+" and org_detailpost in (";

for(int i=0;i<postIDlength;i++)

{

sql=sql+postID[i];

if(i!=(postIDlength-1))

sql=sql+",";

}

sql=sql+")";

}

if (degreeCode!=0)

{

sql=sql+" and org_detaildegree="+degreeCode+"";

}

if (politicalPosition!=0)

{

sql=sql+" and org_detailpoliticalPosition="+politicalPosition+"";

}

if (technicalPosition!=0)

{

sql=sql+" and org_detailtechnicalPosition="+technicalPosition+"";

}

if (technicalPosition!=0)

{

sql=sql+" and org_detailpoliticalLevel="+politicalLevel+"";

}

if (grade!=0)

{

sql=sql+" and org_detailgrade="+grade+"";

}

if (longevity!=0)

{

sql=sql+" and org_detaillongevity="+longevity+"";

}

if(getAllowance!=null)

{

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

{

sql=sql+" and org_detailallowance="+allowance+"";

}

}

}

User[] userList =null;

userList=urcomplexSearch(sql);

logaddLog(1,1,1,uid,userIP,uid+"于("+today+")查询员工");int cnt=userListlength;//总记录数

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

int curPage=1;//当前页

int cntPage;//总页数

int m=1; if (cnt>0)

{

if (cnt%pageSize==0)

cntPage=cnt/pageSize;

else

cntPage=cnt/pageSize+1;

}

else

cntPage=0; if (getPage==null)

{

getPage="1";

curPage=1;

}

else

curPage=IntegerparseInt(getPagetrim());

%><form name="thisform" action="userSearchDeljsp" 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>

<td><font color="#FFFFFF">姓名</font></td>

<td><font color="#FFFFFF">性别</font></td>

<td> <font color="#FFFFFF">所在部门</font></td>

<td> <font color="#FFFFFF">办公地址</font></td>

<td><font color="#FFFFFF">联系电话</font></td>

</tr>

<%

int u=1;

for(int i=0;i<cnt;i++)

{

//显示记录的起始位置

int j=(curPage-1)pageSize;

//显示记录的末位置

int k=curPagepageSize;

//只显示page_size条数据

if(m>j&&m<=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=depgetName(userList[i]getDepID());

if(tel==null||telequals("null"))

tel="";

if(useAddress==null||useAddressequals("null"))

useAddress="";

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

if((u%2)==0)

{

outprintln("<tr bgcolor=#D7D7D7 onMouseOver=thisstylebackgroundColor='#fcd4d7'; onMouseOut=if(selectedItem!=thisid){thisstylebackgroundColor='#D7D7D7';}>");

}

else

{

outprintln("<tr onMouseOver=thisstylebackgroundColor='#fcd4d7'; onMouseOut=if(selectedItem!=thisid){thisstylebackgroundColor='#FFFFFF';}>");

}

u++;

%>

<td align=center><input type="checkbox" name="checkbox" id="CHK_+<%=userId%>" value="<%=userId%>"></td>

<td align=center>

<%

long departmentID=urgetDepID(uid);

if(uphaveRight(uid,"USER_COMPLEXQUERY")==true)

{

%>

<a href="userAnalyzejspuserId=<%=userId%>&dep_id=<%=dep_id%>&toPage=<%=curPage%>"><font color="#330099">

<%

}

else if(uphaveRight(uid,"DEPARTMENT_PLATFORM")==true&&dep_id==departmentID)

{

%>

<a href="userAnalyzejspuserId=<%=userId%>&dep_id=<%=dep_id%>&toPage=<%=curPage%>"><font color="#330099">

<%

}

%>

<%=name%></font></a></td>

<%

if(gendertrim()equals("0"))

outprintln("<td align=center>男</td>");

else

outprintln("<td align=center>女</td>");

%>

<td align=center><a href="depintrojspid=<%=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="searchActionjsp" method="post">

<table border="0" width="95%">

<tr>

<td align="center">第<%=curPage%>页/共<%=cntPage%>页</td>

<td align="left">

<img name="firstpage" src="/Image/btn_firstpagegif" style="cursor:hand" onclick="pageClick(1)" <%=curPage>1" ":"disabled"%>>

<img name="prepage" src="/Image/btn_prepagegif" style="cursor:hand" onclick="pageClick(<%=curPage-1%>)" <%=curPage>1" ":"disabled"%>>

<img name="nextpage" src="/Image/btn_nextpagegif" style="cursor:hand" onclick="pageClick(<%=curPage+1%>)" <%=curPage<cntPage" ":"disabled"%>>

<img name="lastpage" src="/Image/btn_lastpagegif" style="cursor:hand" onclick="pageClick(<%=cntPage%>)" <%=curPage<cntPage" ":"disabled"%>>

</td>

<td align=left width=55%>

到第<input type="text" name="goPage" size="4">页

<img border="0" src="/Image/icon_searchgif" style="cursor:hand" onclick="aPage()">

</td>

</tr><tr>

<td colspan="3" valign="middle" width="23%" align="right">

<%

if(uphaveRight(uid,"USER_MANAGE")==true)

{

%>

<img border="0" src="/Image/btn_deletegif" style="cursor:hand" onclick="delUser()">

<%}%>

<img border="0" src="/Image/btn_returngif" 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)

{

outprintln(egetMessage());

}

%>

</td>

</tr>

</table>

查询数据库应该知道吧?

调用查询数据库方法,从而得到一个数据集合,List类型,数组类型都可以。

假设查询数据库方法是 getData(),返回一个list集合。

<select>

<option value=0>--请选择--</option>

<%

dao d=new dao();//这是那个数据库访问的类。

List list=dgetData();

for(int i=0;i<listsize();i++)

{

%>

<option value=<%=i+1%>><%=listget(i)%></option>

<%}%>

</select>

就这样。

点击按钮提交数据,应该是提交给某类的一个方法处理,在这个方法中调用service的某个方法,当要执行这个方法的时候,进入这个方法,在service的这个方法里面有再调用dao的方法,然后进入dao的方法,dao中的方法内,连接数据库处理得到值或集合,然后返回一个值,这个值service得到,同时service的这个方法也有返回值,然后在处理的那个类也就可以得到了,然后给显示出来,

就是每层调用的方法都有返回值,这样一层一层传

在页面中写Java片段 比如:

<%

//驱动程序名

String driverName = "commysqljdbcDriver";

//数据库用户名

String userName = "自己的";

//密码

String userPasswd = "自己的";

//数据库名

String dbName = "自己的";

//表名

String tableName = "自己的";

//联结字符串

String url = "jdbc:mysql://localhost:3306/" + dbName + "user="

+ userName + "&password=" + userPasswd;

ClassforName("commysqljdbcDriver")newInstance();

Connection connection = DriverManagergetConnection(url);

Statement statement = connectioncreateStatement();

String sql = "SELECT FROM " + tableName;

ResultSet rs = statementexecuteQuery(sql);

%>

jsp页面上提交用户查询数据的请求到action或者servlet, 在java代码中连接数据库查询出数据,返回到jsp页面显示。

这些例子网上很多的, 建议你去网上搜索一下JSP连接数据库的帖子, 有很多代码的。

1、在后台定义一个Servlet或者Action,接收jsp的参数去数据库查询数据,返回List

2、将从数据库查询的数据放在request中,如requestsetAttribute("studentList",studentList),输出到对应的jsp页面。

3、在jsp页面引入jstl标签,定义好一个html表格头

4、用jstl获取后台查询的数据,利用<c:foreach>标签循环输出到表格的<tr>中。

示例:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<html>

<body>

    <table class="table table-bordered" >

<tr>

<th style="text-align:center;width:2%">序号</th>

<th style="text-align:center;width:5%">姓名</th>

<th style="text-align:center;width:6%">年龄</th>

</tr>

<c:forEach items="${studentList}" var="student" varStatus="status">

<tr>

    <td style="text-align:center;">${status}</td>

    <td style="text-align:center;">${studentusername}</td>

    <td style="text-align:center;">${studentage}</td>

</tr>

</c:forEach>

</table>

 </body>

</html>

以上就是关于jsp页面查询数据库全部的内容,包括:jsp页面查询数据库、jsp 查询Oracle数据库、jsp查询数据库代码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10155434.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存