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

jsp資料庫數據顯示

發布時間: 2023-08-10 20:59:41

java新手,JSP頁面如何在一開始載入的時候就顯示後台資料庫的數據

你要顯示資料庫中的數據,首先要建立連接,連到資料庫。至於在servlet還是struts類,還是jsp頁面是設計問題。最笨的方法就是在jsp裡面嵌入java代碼,連接資料庫,寫查詢語句,將返回結果保存在容器中,如vector,然後顯示。

❷ 在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>
<%
}
%>

❸ 在jsp頁面上顯示資料庫一個表中所有的的內容。

在jsp頁面上顯示資料庫一個表中所有的的內容的方法是迭代。
1、jsp頁面接收所有內容的bookslist:
<html>
<body>
<head>
<title>
View Books
</title>
</head>
<body>
<table border=2>
<tr>
<th>Book ID</th>
<th>Title</th>
<th>Author</th>
<th>No. of copies AVAILABLE</th>
<th>Number of favourites</th>
</tr>

<%
ArrayList<Book> dbooks=(ArrayList)request.getAttribute("bookslist");
Iterator it=dbooks.iterator();
while(it.hasNext())
{
Book b=(Book)it.next();
%>
<tr>
<td><%=b.bookID%></td>
<td><%=b.bookTitle%></td>
<td><%=b.bookAuthor%></td>
<td><%=b.bookCopies%></td>
<td><%=b.bookFavs%></td>
</tr>
<%
}
%>
</table>
</body>
</html>

2、java代碼獲取資料庫內容:
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3307/library", "root", "admin");
PreparedStatement ps=con.prepareStatement("select * from book");
ResultSet rs=ps.executeQuery();
ArrayList<Book> books=new ArrayList<Book>();
while(rs.next())
{
Book b= new Book();
b.bookID=rs.getInt(3);
b.bookTitle=rs.getString(1);
b.bookAuthor=rs.getString(2);
b.bookCopies=rs.getInt(4);
b.bookFavs=rs.getInt(5);
books.add(b);
}
req.setAttribute("bookslist",books);
con.close();

❹ JSP頁面上如何顯示資料庫內容

查詢資料庫應該知道吧?
調用查詢資料庫方法,從而得到一個數據集合,List類型,數組類型都可以。
假設查詢資料庫方法是
getData(),返回一個list集合。
<select>
<option
value=0>--請選擇--</option>
<%

d=new
();//這是那個資料庫訪問的類。
List
list=d.getData();
for(int
i=0;i<list.size();i++)
{
%>
<option
value=<%=i+1%>><%=list.get(i)%></option>
<%}%>
</select>
就這樣。

❺ 怎麼從資料庫中提取數據,在jsp頁面顯示

在資料庫提取部分數據,在JSP上顯示的做法如下:
思路:1、創建db連接 2、創建statement 3、執行查詢 4、遍歷結果並展示
完整代碼如下:
<span style="font-size:12px;"><span style="font-size:14px;"><%@ page language="java" import="java.sql.*,java.io.*,java.util.*"%>
<%@ page contentType="text/html;charset=utf-8"%>
<html>
<head>
<style type="text/css">
table {
border: 2px #CCCCCC solid;
width: 360px;
}

td,th {
height: 30px;
border: #CCCCCC 1px solid;
}
</style>
</head>
<body>
<%
//驅動程序名
String driverName = "com.mysql.jdbc.Driver";
//資料庫用戶名
String userName = "root";
//密碼
String userPasswd = "szy";
//資料庫名
String dbName = "studentmanage";
//表名
String tableName = "student";
//聯結字元串
String url = "jdbc:mysql://localhost:3306/" + dbName + "?user="
+ userName + "&password=" + userPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = DriverManager.getConnection(url);
Statement statement = connection.createStatement();
String sql = "SELECT * FROM " + tableName;
ResultSet rs = statement.executeQuery(sql);
%>
<br>
<br>
<table align="center">
<tr>
<th>
<%
out.print("學號");
%>
</th>
<th>
<%
out.print("姓名");
%>
</th>
<th>
<%
out.print("專業");
%>
</th>
<th>
<%
out.print("班級");
%>
</th>
</tr>

<%
while (rs.next()) {
%>
<tr>
<td>
<%
out.print(rs.getString(1));
%>
</td>
<td>
<%
out.print(rs.getString(2));
%>
</td>
<td>
<%
out.print(rs.getString(3));
%>
</td>
<td>
<%
out.print(rs.getString(4));
%>
</td>
</tr>
<%
}
%>
</table>
<div align="center">
<br> <br> <br>
<%
out.print("數據查詢成功,恭喜你");
%>
</div>
<%
rs.close();
statement.close();
connection.close();
%>
</body>
</html></span><span style="font-size:24px;color: rgb(255, 0, 0);">
</span></span>

熱點內容
手游編程培訓 發布:2025-03-11 09:43:38 瀏覽:510
php獲取瀏覽器 發布:2025-03-11 09:03:31 瀏覽:877
安卓常駐後台需要什麼許可權 發布:2025-03-11 08:58:26 瀏覽:181
綠源電動車威牛是什麼配置 發布:2025-03-11 08:47:34 瀏覽:10
wps加密文件密碼忘記 發布:2025-03-11 08:36:49 瀏覽:47
可編程渲染管線 發布:2025-03-11 08:35:23 瀏覽:455
一般人手機設置密碼會是什麼 發布:2025-03-11 08:27:19 瀏覽:416
緩存電視劇軟體 發布:2025-03-11 08:26:26 瀏覽:135
安卓怎麼下載ios14 發布:2025-03-11 08:25:50 瀏覽:567
軟體調試源碼 發布:2025-03-11 08:24:59 瀏覽:489