當前位置:首頁 » 操作系統 » jsp從資料庫中讀取數據

jsp從資料庫中讀取數據

發布時間: 2022-03-05 07:29:16

Ⅰ JSP中<li>標簽怎樣從資料庫中動態獲取數據

如果會jstl技術的話,很簡單,不會的話,就用jsp腳本語言<% %>寫java代碼,在資料庫查到數據後,然後循環數據就行了,例如:
<%
for(int i = 0; i < 10; i++){
%>
<li><%=i%></li>
<%
}
%>

Ⅱ 用JSP從資料庫中讀取數據

從表user中查詢okok欄位=1的ID最大的10個數據(也就是最近插入的十條數據),數據按RND排序,如果RND相同就按ID排序.

Ⅲ 怎麼從資料庫中提取數據,在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>

Ⅳ jsp從資料庫中循環讀取一張表的數據,然後顯示在jsp頁面的一個表格中。求給一個完整的列子!!!

JSP頁面中用c標簽遍歷list,要顯示數據的對象列表放到list中。

java代碼:

request.setAttribute("list",yourList);

jsp代碼:

<c:forEachvar="user"items="${list}">
UserName:${user.username}<br/>
Age:${user.age}
</c:forEach>

注意在JSP中引入jstl的core標簽,如果實在不清楚,繼續追問

Ⅳ 在JSP中,如何從資料庫中獲取值在列表框中得到

你是要從資料庫獲取數據是吧?
你可以說清楚你要的是什麼嗎,下面不知道是不是你需要的
<%//連接需要的對象
Connection conn = null;
Statement stmt = null;
String sql = null;
ResultSet rs = null;
int ret;
try { Class.forName("com.mysql.jdbc.Driver"); //載入JDBC驅動程序
String strCon="jdbc:mysql://localhost:3306/JspSamples"; //連接字
conn = DriverManager.getConnection(strCon, "root", "root"); //連接資料庫
stmt = conn.createStatement(); //初始化查詢
sql = "select * from customers ";//查詢數據
rs = stmt.executeQuery(sql); //執行查詢數據,返回結果集

while (rs.next()) { //遍歷結果集
int id = rs.getInt("Id");//獲取指定列的值
String name = rs.getString("Name");
String tel = rs.getString("Tel");
String email = rs.getString("Email");
Timestamp addtime = rs.getTimestamp("addTime", Calendar.getInstance());
out.println("<tr>");//顯示結果
out.println("<td>" + id + "</td>");
out.println("<td>" + name + "</td>");
out.println("<td>" + tel + "</td>");
out.println("<td>" + email + "</td>");
out.println("<td>" + addtime + "</td>");
out.println("</tr>");
}
rs.close();//關閉結果集
stmt.close(); //關閉查詢
conn.close();//關閉連接

} catch (ClassNotFoundException e) {//意外處理,驅動程序無法找到
e.printStackTrace();
out.println("<h1>無法找到資料庫驅動</h1>");
} catch (SQLException e1) {//意外處理,資料庫操作失敗
e1.printStackTrace();
out.println("<h1>資料庫操作失敗</h1>");
}

%>

Ⅵ java從資料庫中讀取的數據怎樣顯示在jsp的網頁當中

java從資料庫中讀取的數據顯示在jsp的網頁當中的方法是迭代table。
1、迭代數據的jsp頁面代碼:

<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
<th>Phone No</th>
</tr>
<s:iterator value="users">
<tr>
<td><s:property value="name"/></td>
<td><s:property value="email"/></td>
<td><s:property value="address"/></td>
<td><s:property value="phno"/></td>
</tr>
</s:iterator>
</table>
2。後台java查詢數據
public class RegisterAction extends ActionSupport{
String name,pwd,email,address;
int phno;

public RegisterAction() {}

List<User> users = new ArrayList<User>();
UserDao u = new UserDao();

//Getters and setters.

public String execute() throws Exception {
User u=new User();
u.setName(name);
u.setEmail(email);
u.setAddress(address);
u.setPhno(phno);
u.setPwd(pwd);
u.addUser(u);
return "success";
}

public String listAllUsers(){
users = u.getUsers();
System.out.println("In Action, "+users);
return "success";
}
}

Ⅶ jsp中如何獲取從資料庫中取了多少條數據

用循環,給你個sql server的例子
==================================================
<%@ 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";
String user="sa";
String password="sa";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement();
String sql="select * from jobs";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
<%=rs.getString(1)%> //第一個欄位內容,也可以使用欄位名
<%=rs.getString(2)%> //第二個欄位內容
<%}%>
<%rs.close();
stmt.close();
conn.close();
%>

</body>
</html>

Ⅷ 在jsp中怎樣從資料庫中獲取信息到頁面的相應位置

整個過程基本實現是這樣的,這個過程跨域任何框架,與底層應用框架無關。
1 jsp頁碼中用戶發出一個請求(可以是能夠發出請求的任何方式)到一個能夠處理請求的組件,我們稱之為控制器(servlet 或 action 等)
2 控制器調用業務層組件方法,業務層組件方法中調用(數據訪問層)方法(此方法中實現從資料庫中讀取目標數據,通常封裝為一個javabean對象,我們稱之為實體bean)
3 控制器獲得封裝資料庫表的數據後,將其存儲在作用域中,定位到要顯示數據的jsp頁面
4 目標jsp頁碼中通常使用自定義標簽實現在jsp的特定位置讀取數據並顯示

Ⅸ Jsp怎樣在資料庫中讀取數據

你般情況,只要你的tr td之類的東西沒有放在for循環裡面還是不會亂,就算放到裡面了,邏輯正確也是不會亂的

Ⅹ 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-09-23 18:33:22 瀏覽:228
phpjs注入 發布:2024-09-23 18:31:51 瀏覽:595
高性能php應用開發 發布:2024-09-23 18:23:56 瀏覽:208
廣東雲存儲空間開發 發布:2024-09-23 18:21:47 瀏覽:383
易語言怎麼架伺服器 發布:2024-09-23 18:21:46 瀏覽:789
hibernate緩存清除緩存 發布:2024-09-23 18:11:01 瀏覽:364
安卓導航模式在哪裡 發布:2024-09-23 18:05:22 瀏覽:55
吉利博瑞ge配置有哪些不同 發布:2024-09-23 18:05:21 瀏覽:114
紅米手機刷新密碼是多少 發布:2024-09-23 17:59:26 瀏覽:699
codeblocks帶編譯器下載 發布:2024-09-23 17:58:03 瀏覽:925