當前位置:首頁 » 操作系統 » jsp查詢資料庫數據

jsp查詢資料庫數據

發布時間: 2022-08-04 11:59:27

1. 在JSp頁面查詢出資料庫的數據並顯示在表格上,我要操作資料庫

<script
type="text/javascript">
function
modify(id){
//直接鏈接提交
var
num
=
document.getElementById(id).value;
var
url
=
"/要提交的servlet地址?bookid="+id+"&num="+num;
location
=
url;
}
//添加到購物車
function
add(id){
var
url
=
"/要提交的servlet地址?bookid="+id;
location
=
url;
//通過id在後台得到實體對象,然後放到購物車即可
}
</script>
不知道你會EL和JSTL表達式,直接用java代碼了
每一條記錄肯定有個唯一標示的id,假設這個對象是Book,集合是list
<%
for(Book
book
:
list){
%>
<tr>
<td><%=book.id%></td>
<!--id-->
<td><%=book.name%></td>
<!--書名-->
<td><input
type="text"
name="num"
id="<%=book.id%>"
value="<%=book.num%"></td>
<!--數量->
<td><input
type="button"
value="修改"
onclick="modify(<%=book.id%>)"></td>
<td><input
type="button"
value="添加到購物車"
onclick="add(<%=book.id%>)"></td>
<tr>
<%
}
%>

2. jsp怎麼查詢資料庫內容

你用的什麼開發工具啊!
<%@ page
language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
%>
<%@page import="java.sql.*"%>
<center>
<H1> <font color="blue" size="12">管理中心</font></H1>
<HR />
<table width="80%" border="1">

<tr>
<th>ID</th>
<th>書名</th>
<th>作者</th>
<th>價格</th>
<th>刪除</th>

</tr>
<%

// 資料庫的名字
String dbName = "zap";
// 登錄資料庫的用戶名
String username = "sa";
// 登錄資料庫的密碼
String password = "123";
// 資料庫的IP地址,本機可以用 localhost 或者 127.0.0.1
String host = "127.0.0.1";
// 資料庫的埠,一般不會修改,默認為1433
int port = 1433;
String connectionUrl = "jdbc:sqlserver://" + host + ":" + port + ";databaseName=" + dbName + ";user=" + username
+ ";password=" + password;
//
//聲明需要使用的資源
// 資料庫連接,記得用完了一定要關閉
Connection con = null;
// Statement 記得用完了一定要關閉
Statement stmt = null;
// 結果集,記得用完了一定要關閉
ResultSet rs = null;
try {
// 注冊驅動
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// 獲得一個資料庫連接
con = DriverManager.getConnection(connectionUrl);

String SQL = "SELECT * from note";
// 創建查詢
stmt = con.createStatement();
// 執行查詢,拿到結果集
rs = stmt.executeQuery(SQL);

while (rs.next()) {
%>
<tr>
<td>

<%=rs.getInt(1)%>
</td>
<td>
<a href="prepareupdate?ID=<%=rs.getInt("ID")%>" target="_blank"><%=rs.getString(2)%></a>
</td>
<td>
<%=rs.getString(3)%>
</td>
<td>
<%=rs.getString(4)%>
</td>
<td>
<a href="delete?ID=<%=rs.getInt("ID")%>" target="_blank">刪除</a>

</td>

</tr>

<%

}

} catch (Exception e) {
// 捕獲並顯示異常
e.printStackTrace();
} finally {
// 關閉我們使用過的資源
if (rs != null)
try {
rs.close();
} catch (Exception e) {}
if (stmt != null)
try {
stmt.close();
} catch (Exception e) {}
if (con != null)
try {
con.close();
} catch (Exception e) {}
}
%>
</table>
<a href="insert.jsp">添加新紀錄</a>
</center>

我其中的一個資料庫查詢界面。

3. jsp中查詢資料庫功能

代碼並沒有結束呀,你只是寫到獲取參數了,至於 獲取到參數,是用來幹嘛的,你並沒有貼出來,所以不確定這幾個參數的作用。

4. 在JSP檢索頁面中查詢資料庫數據怎麼做

將你搜索到的數據方法域裡面,讓後再JSP 頁面通過 EL表達式獲取。

5. JSP中的資料庫查詢

rs = sql1.executeQuery("select * from table1 where 編號 ="+變數名);
這樣寫就可以了
如果編號是字元型那就得這樣寫
rs = sql1.executeQuery("select * from table1
where 編號 ='"+變數名+"'");

6. 怎樣用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包加入到你的工程當中!

7. JSP獲取資料庫信息

<table
width="100%"
border="0"
cellpadding="0"
cellspacing="1"
bgcolor="#a8c7ce">
<tr
align="center"
height="25"
bgcolor="d3eaef">
<td
width="5%">
編號
</td>
<td
width="10%">
標題
</td>
<td
width="23%">
內容
</td>
<td
width="10%">
發表日期
</td>
<td
width="16%">
基本操作
</td>
</tr>
<%
//獲取新聞信息集合,newList是從後台返回來的集合變數
List
nList
=
(List)
session.getAttribute("newList");
NewsEntity
new
=
null;
if
(nList.size()
<=
0)
{
%>
<tr
height="22"
bgcolor="#FFFFFF"
align="center">
<td
colspan="9"
align="center">
暫無新聞信息
</td>
</tr>
<%
}
else
{
for
(int
i
=
0;
i
<
nList.size();
i++)
{
new
=
(NewsEntity)
mList.get(i);
%>
<tr
height="22"
bgcolor="#FFFFFF"
align="center">
<td>
<%=new.getId()
%>
</td>
<td>
<%=new.getTitle()
%>
</td>
<td>
<%=new.getContent()
%>
</td>
<td>
<%=new.time()
%>
</td>
<td>
<a
href="MusicServlet?forward=getNewsDetailById&ID=<%=new.getId()%>"
>
<span
class="STYLE2">編輯</span>
</a>

<a
href="MusicServlet?forward=doDelNewsById&ID=<%=new.getId()%>"
onclick="return
confirm('您確定要刪除該條信息嗎?');"><span
class="STYLE2">刪除</span>
</a>
</td>
</tr>
<%
}
}
%>
</table>

8. 如何在jsp頁面獲取資料庫數據

把數據封裝在List中,把list放入request作用域鍾,在前台用foreach循環你的list就好了

9. jsp中如何獲得資料庫的值

最簡單的JSP頁面中的資料庫操作方法:
<%@ page
language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
%>
<%@page import="java.sql.*"%>
<center>
<H1> <font color="blue" size="12">管理中心</font></H1>
<HR />
<table width="80%" border="1">
<tr>
<th>ID</th>
<th>書名</th>
<th>作者</th>
<th>價格</th>
<th>刪除</th>
</tr>
<%
// 資料庫的名字
String dbName = "zap";
// 登錄資料庫的用戶名
String username = "sa";
// 登錄資料庫的密碼
String password = "123";
// 資料庫的IP地址,本機可以用 localhost 或者 127.0.0.1
String host = "127.0.0.1";
// 資料庫的埠,一般不會修改,默認為1433
int port = 1433;
String connectionUrl = "jdbc:sqlserver://" + host + ":" + port + ";databaseName=" + dbName + ";user=" + username
+ ";password=" + password;
//
//聲明需要使用的資源
// 資料庫連接,記得用完了一定要關閉
Connection con = null;
// Statement 記得用完了一定要關閉
Statement stmt = null;
// 結果集,記得用完了一定要關閉
ResultSet rs = null;
try {
// 注冊驅動
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// 獲得一個資料庫連接
con = DriverManager.getConnection(connectionUrl);
String SQL = "SELECT * from note";
// 創建查詢
stmt = con.createStatement();
// 執行查詢,拿到結果集
rs = stmt.executeQuery(SQL);
while (rs.next()) {
%>
<tr>
<td>
<%=rs.getInt(1)%>
</td>
<td>
<a href="prepareupdate?ID=<%=rs.getInt("ID")%>" target="_blank"><%=rs.getString(2)%></a>
</td>
<td>
<%=rs.getString(3)%>
</td>
<td>
<%=rs.getString(4)%>
</td>
<td>
<a href="delete?ID=<%=rs.getInt("ID")%>" target="_blank">刪除</a>
</td>
</tr>
<%
}
} catch (Exception e) {
// 捕獲並顯示異常
e.printStackTrace();
} finally {
// 關閉我們使用過的資源
if (rs != null)
try {
rs.close();
} catch (Exception e) {}
if (stmt != null)
try {
stmt.close();
} catch (Exception e) {}
if (con != null)
try {
con.close();
} catch (Exception e) {}
}
%>
</table>
<a href="insert.jsp">添加新紀錄</a>
</center>

10. 在jsp中怎麼獲取顯示資料庫的信息

方法有幾種fj現在開發的話都用框架51不知道樓主學到哪了254不同階段方法不同

熱點內容
如何創作抖音模板腳本 發布:2024-05-02 14:42:36 瀏覽:854
ftp被動模式下載 發布:2024-05-02 14:33:25 瀏覽:313
教堂電影院ftp 發布:2024-05-02 14:32:35 瀏覽:481
程序編譯機 發布:2024-05-02 14:32:29 瀏覽:796
cf伺服器雲南一區雲空間 發布:2024-05-02 14:18:24 瀏覽:752
如何破解excel2007密碼 發布:2024-05-02 14:18:23 瀏覽:993
ios數據上傳伺服器 發布:2024-05-02 13:39:27 瀏覽:351
Php面向對象模式 發布:2024-05-02 13:33:32 瀏覽:80
安卓手機軟體如何快速打開 發布:2024-05-02 13:25:16 瀏覽:963
安卓網頁圖片不顯示怎麼辦 發布:2024-05-02 13:16:00 瀏覽:673