當前位置:首頁 » 數據倉庫 » 查詢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