导航:首页 > 数据处理 > jsp数据库查询代码怎么写

jsp数据库查询代码怎么写

发布时间:2023-01-14 15:38:08

A. jsp怎么写增删改查代码

下面的代码即可实现(对数据库的操作):

<%@page
language="java"
contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"
%>
<%@pageimport="java.sql.*"%>
<center>
<H1><fontcolor="blue"size="12">管理中心</font></H1>
<HR/>
<tablewidth="80%"border="1">
<tr>
<th>ID</th>
<th>书名</th>
<th>作者</th>
<th>价格</th>
<th>删除</th>
</tr>
<%
//数据库的名字
StringdbName="zap";
//登录数据库的用户名
Stringusername="sa";
//登录数据库的密码
Stringpassword="123";
//数据库的IP地址,本机可以用localhost或者127.0.0.1
Stringhost="127.0.0.1";
//数据库的端口,一般不会修改,默认为1433
intport=1433;
StringconnectionUrl="jdbc:sqlserver://"+host+":"+port+";databaseName="+dbName+";user="+username
+";password="+password;
//
//声明需要使用的资源
//数据库连接,记得用完了一定要关闭
Connectioncon=null;
//Statement记得用完了一定要关闭
Statementstmt=null;
//结果集,记得用完了一定要关闭
ResultSetrs=null;
try{
//注册驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//获得一个数据库连接
con=DriverManager.getConnection(connectionUrl);

StringSQL="SELECT*fromnote";
//创建查询
stmt=con.createStatement();
//执行查询,拿到结果集
rs=stmt.executeQuery(SQL);
while(rs.next()){
%>
<tr>
<td>
<%=rs.getInt(1)%>
</td>
<td>
<ahref="prepareupdate?ID=<%=rs.getInt("ID")%>"target="_blank"><%=rs.getString(2)%></a>
</td>
<td>
<%=rs.getString(3)%>
</td>
<td>
<%=rs.getString(4)%>
</td>
<td>
<ahref="delete?ID=<%=rs.getInt("ID")%>"target="_blank">删除</a>
</td>
</tr>
<%
}
}catch(Exceptione){
//捕获并显示异常
e.printStackTrace();
}finally{
//关闭我们使用过的资源
if(rs!=null)
try{
rs.close();
}catch(Exceptione){}
if(stmt!=null)
try{
stmt.close();
}catch(Exceptione){}
if(con!=null)
try{
con.close();
}catch(Exceptione){}
}
%>
</table>
<ahref="insert.jsp">添加新纪录</a>
</center>

B. jsp中查询mysql数据库中的表,请问如果按时间查询代码该怎么写

jsp代码编写,其实就是把你上面的布局还原到页面上,这个只要会html就不难。
连接池就是为了访问数据库,一般设置两个xxx.xml文件就可,当然这也需要驱动,在后台要有一个类似DBHelp.java用来加载就可。
在jsp中不会使用连接池的,jsp中发送一个请求道后台,通过DBHelp来获取数据库中的数据。
大体流程就是这样。

C. 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!=null&&post.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!=null&&userID!=""){
sql=sql+" and org_user.user_id='"+userID+"'";
}
if (userName!=null&&userName!="")
{
sql=sql+" and org_user.name like '%"+userName+"%'";
}if (address!=null&&address!="")
{
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;i<postID.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 (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=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>
<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=curPage*pageSize;
//只显示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=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=center><input 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")==true&&dep_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=center><a 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)" <%=curPage>1?" ":"disabled"%>>
<img name="prepage" src="../Image/btn_prepage.gif" style="cursor:hand" onclick="pageClick(<%=curPage-1%>)" <%=curPage>1?" ":"disabled"%>>
<img name="nextpage" src="../Image/btn_nextpage.gif" style="cursor:hand" onclick="pageClick(<%=curPage+1%>)" <%=curPage<cntPage?" ":"disabled"%>>
<img name="lastpage" src="../Image/btn_lastpage.gif" 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_search.gif" style="cursor:hand" onclick="aPage()">
</td>
</tr><tr>
<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数据库查询代码怎么写相关的资料

热点内容
奥比岛怎么修改程序 浏览:49
无中介交易怎么避免卖家二次抵押 浏览:758
nfc技术怎么激活 浏览:913
为什么大飞机技术不好 浏览:435
交易员考什么课程 浏览:866
aac上架多少交易所 浏览:473
哪里有马岗鹅批发市场 浏览:722
撤案需要什么程序 浏览:499
会泽县小学信息技术多少分进面 浏览:631
实现数据压缩与什么层密切相关 浏览:504
怎么成为网点代理人 浏览:441
扫码查答案的程序有什么 浏览:792
个人信息泄露被判刑的有哪些 浏览:179
义乌狗市场狗多少一只 浏览:650
如何解除移动数据限流的方法 浏览:174
郴州市活禽交易市场什么时候休市 浏览:456
四川空间信息产业发展怎么样 浏览:284
宏基笔记本怎么样关闭程序 浏览:523
邯郸有哪些铁板市场 浏览:850
问道如何查询账号信息 浏览:324