當前位置:首頁 » 操作系統 » jsp頁面獲取資料庫數據

jsp頁面獲取資料庫數據

發布時間: 2022-08-03 03:06:30

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

爭議當然是通過java後台連接資料庫,獲取數據,然後傳到jsp上面

⑵ 如何在jsp頁面獲取資料庫中的數據

  1. 建立資料庫連接

  2. 調用方法,比如list<User> userlist = DB.findAll(), req.setAttribute("list",userlist)

  3. jsp部分:<c:forEach items="list" var="user">

    <td>${user.id}</td> //顯示User對象的id屬性

    </c:forEach>

    用到forEach,要引入jstl.jar

⑶ JSP頁面怎麼得到資料庫中的數據

1、jsp頁面寫小腳本可以得到
2、可以是使用ajax技術 非同步進行訪問
3、可以配置web.xml 裡面配置好servlet
4、使用框架的話,就直接用框架技術來得到.......
總之,方法很多,看你是要採用哪種方法來做

⑷ jsp怎麼獲取資料庫數據 spring

比如頁面有name和age兩個內容。
在spring MVC框架下
在action中如下寫
@RequestMapping(value="/login",method=RequestMethod.POST)
public String login(User user){
String userName = user.getName();
String userage = user.getAge();
System.out.println(userName + "," + userage);
if(userName=="zhangsan"&&age=="123"){
return "login";
}else{
return "false";
}
}

具體return的login和false是要跳到哪個頁面,在spirng的配置文件servlet.xml中如下配置
<!-- 視圖解析類-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/" /> <!-- 前綴 -->
<property name="suffix" value=".jsp" /> <!-- 後綴 -->
</bean>

這個配置的意思就是,return的是XXXX,就跳到/web-INF/page/xxxx.jsp 這個頁面

⑸ 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>

⑹ jsp獲取資料庫中的數據

<%
//JSP頁面直接訪問資料庫
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName("JDBC驅動");
conn = DriverManager.getConnection("url", "username", "password");
stmt = conn.createStatement();
rs = stmt.executeQuery("select factor, ratio from 表名 where id=1");
while(rs.next()){
String factor = rs.getString("factor");
String ratio = rs.getString("ratio");
%>
factor :<%=factor %>
ratio :<%=ratio %>
<%
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
}catch(Exception e1){
e1.printStackTrace();
}
}
%>
修改 驅動、url、username、password、表名、欄位名成你應用的相應數據,然後將這些代碼加入到你的jsp頁面,就可以在jsp頁面直接讀取到資料庫中的對應表指定欄位的數據了,祝你好運!

⑺ jsp頁面前台用jdbc如何獲取sql資料庫里的數據。

1、在後台寫一個JDBC方法,從資料庫中查詢出數據來,封裝到一個集合中,比如List或Map,然後放到request中,在前台直接從request獲取即可。
2、直接在前台寫一個JDBC操作的腳本方法,從資料庫中查詢出數據,封裝到集合中,然後根據需要迭代顯示。

⑻ 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>

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

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

⑽ 如何在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>

熱點內容
壓縮比英文 發布:2024-05-06 01:56:35 瀏覽:171
數字php 發布:2024-05-06 01:53:10 瀏覽:742
編程中怎麼 發布:2024-05-06 01:43:32 瀏覽:629
如何訪問遠程資料庫 發布:2024-05-06 01:39:20 瀏覽:447
刷演算法的網站 發布:2024-05-06 01:30:39 瀏覽:270
少兒編程徐州 發布:2024-05-06 01:20:42 瀏覽:462
sqlserver連接驅動 發布:2024-05-06 00:33:34 瀏覽:646
存儲開銷 發布:2024-05-06 00:13:38 瀏覽:954
伺服器怎麼盈利 發布:2024-05-05 23:56:16 瀏覽:942
java網站培訓學校 發布:2024-05-05 23:43:11 瀏覽:41