当前位置:首页 » 数据仓库 » 查询jsp数据库代码
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

查询jsp数据库代码

发布时间: 2022-11-27 10:13:10

A. jsp页面查询数据库

你的意思是一个submit最多能一次性提交4个文本框内的数据是吧?把4个文本框放进一个表单,提交表单就能4个一起提交,获取的时候用:request.getParameter("文本框名");
至于查询参数要在sql上写if判断了,如下写法: String cond="";
Long di_id=0l;
if(request.getParameter("dept")!=null&&!"0".equals(request.getParameter("dept"))){
di_id=Long.valueOf((request.getParameter("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;
}
这样就可以实现任意属于参数个数实现查询了:)

B. jsp查询数据库里面的信息怎么写代码啊

jsp先加载你数据库的驱动,连接数据库(不同数据库不一样),然后再写sql语句,再执行sql,再通过rs集,把rs结果集读出显示在jsp页面

C. 高分 jsp 数据库查询

大概样子就是这样,应该能连上,这还是很久以前写的代码了,你自己修改一下应该能用。。

<%
String strDriver="com.mysql.jdbc.Driver";//驱动
String strConn="jdbc:mysql://localhost:3306/test";// url,test为数据库名

String user="root";
String psw="root";

Connection conn=null;
ResultSet rs=null;
Statement stmt=null;

try {
Class.forName(strDriver);//加载驱动
} catch (ClassNotFoundException e) {
System.out.println("加载类失败!");
}

try {
conn=DriverManager.getConnection(strConn,user,psw);//连接数据库
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库联接失败!");
}

String NO=request.getParameter("NO");
String sql="select * from table where NO="+NO;//查询语句

try {
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("查询出问题了");
}

out.print("<table>");
out.print("<tr><td>NO</td><td>A</td><td>B</td><td>C</td></tr>");
rs.next();
out.print("<tr><td>"+rs.getString(1)+"</td><td>"+rs.getString(2)+"</td><td>"+rs.getString(3)+"</td><td>"+rs.getString(4)+"</td></tr>");
out.print("</table>");

conn.close();
rs.close();
%>

D. Jsp实现数据库数据修改的代码

可以使用jstl中的sql标签1. <sql:setDataSource dataSource=”” url=”” driver=”” user=”” password=”” var=”” scope=””> 这个标签用于设置数据源,可以通过scope指定数据源的范围,这里有两种类型的数据源,一种是直接使用在web应用中配置的数据源,只要指定它的JNDI名字就可以,另一种是指定所有的数据库连接属性。其中dataSource如果是String类型,那么表示JNDI名称空间中的DataSource的名字;如果是DataSource,那么是一个可以获得的数据源对象;url表示访问驱动程序的URL;driver表示指定驱动程序的实现类;user访问数据库的用户名;password表示访问数据库的密码;var表示标识这个DataSource的变量;scope表示指定这个DataSource的范围。例如:
<sql:setDataSource driver="com.microsoft.sqlserver.jdbc.SQLServerDriver"
user="liky"
password="redhat"
url="jdbc:sqlserver://localhost:1433;DatabaseName=csu"
var="db" />
2. <sql:query dataSource=”” sql=”” maxRows=”” startRow=”” var=”” scope=””> 这个标签用于执行数据库查询操作。其中dataSource表示在setDataSource中的dataSource数据源;sql表示要执行的SQL语句;var表示保存了查询的结果集;scope表示变量的有效范围;maxRows表示返回的最大行数;startRow表示从第几行开始查询,这两个属性在无脚本实现分页时非常有用。例如:
<sql:query dataSource="${db}"var="ursers"
sql="select * from bbs_user where id > ? "
scope="page" startRow="10" maxRows="10">
<sql:param>12</sql:param>
< /sql:query>
3. <sql:update dataSource=”” sql=”” var=”” scope=””> 这个标签用于对数据库进行更新操作,这里所谓的更新包括insert、update和delete数据库操作语句。此外还包括一些没有返回结果集的SQL操作语句,例如创建表,更改表的结构等等。其中dataSource表示在setDataSource中的dataSource数据源;sql表示要执行的SQL语句;var表示保存了语句执行结果的变量;scope表示变量的有效范围。例如:如果是更新、删除、插入,那么表示更新的数据行数。例如:
<sql:query
dataSource="${db}"var="ursers"
sql="select * from bbs_user"
scope="page" startRow="10" maxRows="10">
< /sql:query>

E. 编写一个查询access数据库的jsp页面。

编写查询access数据库的jsp页面方法:
1、建立一个ACCESS数据库testmdb,其中建立一个数据库表username,包含两个段uid和pwd,均为文本型字段,(数据库和表的名称可以自行生成,但要注意在下面代码中的相应位置予以调整)。
2、将testmdb存放在TOMCAT安装目录下的data文件夹中,例如C:ProgramFiles.0webappsROOTdata,(数据库的位置根据TOMCAT安装位置的不同适当调整)。
3、建立一个JSP文件JDBCAccessjs胡于显示数据访问效果。

F. search.jsp页面查询数据库并将查询结果显示到search.jsp的代码

<% UserDao userDao = new UserDao();
User user = userDao.findById("123");
%>
userName: <input type='text' value='<%=user.username%>'/>
这里简单做了个查询,显示,就不做异常判断了。其他判断你自己加。记得导包

G. JSP前台页面连接数据库查询并返回值的代码,求注解。

1、在后台写一个JDBC方法,从数据库中查询出数据来,封装到一个集合中,比如List或Map,然后放到request中,在前台直接从request获取即可。

2、直接在前台写一个JDBC操作的脚本方法,从数据库中查询出数据,封装到集合中,然后根据需要迭代显示。

H. 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>

I. 怎样用JSP语言查询数据库中的数据,并可以修改

查询什么数据库?

testsqlserver.jsp如下
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
//pubs为你的数据库的
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>

testoracle.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl为你的数据库的SID
String user="scott";
String password="tiger";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>

testdb2.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
String url="jdbc:db2://localhost:5000/sample";
//sample为你的数据库名
String user="admin";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>

testmysql.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url="jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
//testDB为你的数据库名
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>

有了 这些代码,不代表你就能连接数据库了,你还得有相应java连接各个数据库的.jar包加入到你的工程当中!

J. jsp获取数据库内容代码

用SELECT查询数据库就可以了
下面的语句从表中选取
LastName
列的数据:
SELECT
LastName
FROM
Persons