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

jsp頁面查詢資料庫

發布時間: 2022-11-14 13:37:19

A. 如何用jsp顯示資料庫中的數據

用jsp顯示資料庫中的數據的方法:
1、通過jdbc建立資料庫連接:
Connection connection = DriverManager.getConnection(
"jdbc:odbc:data", "Steve", "password");
2、創建查詢的statement:
Statement statement = connection.createStatement() ;

3、執行查詢:
ResultSet resultset =
statement.executeQuery("select * from tableName") ;

4、循環輸出獲取到的數據:
while(resultset.next()){
。。。。。。
}

5、綜合1-4的完整代碼如下:
<%@ page import="java.sql.*" %>
<% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); %>
<HTML>
<HEAD>
<TITLE>The tableName Database Table </TITLE>
</HEAD>

<BODY>
<H1>The tableName Database Table </H1>
<%
Connection connection = DriverManager.getConnection(
"jdbc:odbc:data", "Steve", "password");

Statement statement = connection.createStatement() ;
ResultSet resultset =
statement.executeQuery("select * from tableName") ;
%>

<TABLE BORDER="1">
<TR>
<TH>ID</TH>
<TH>Name</TH>
<TH>City</TH>
<TH>State</TH>
<TH>Country</TH>
</TR>
<% while(resultset.next()){ %>
<TR>
<TD> <%= resultset.getString(1) %></td>
<TD> <%= resultset.getString(2) %></TD>
<TD> <%= resultset.getString(3) %></TD>
<TD> <%= resultset.getString(4) %></TD>
<TD> <%= resultset.getString(5) %></TD>
</TR>
<% } %>
</TABLE>
</BODY>
</HTML>

B. jsp中查詢資料庫功能

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

C. 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;
}
這樣就可以實現任意屬於參數個數實現查詢了:)

D. 在JSP頁面查詢資料庫信息

你可以在頁面上設置一個隱藏域,當點擊『查看詳細信息』按鈕時,將選中對象的id值賦給隱藏域,然後跳轉顯示詳細信息的頁面,再根據id值來查處詳細信息即可。
彈窗口 用
window.showModalDialog("XXXX.html","XXXX","dialogWidth=800px;dialogHeight=600px");就可以

E. ssh2怎樣在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";
}
}

F. 如何在jsp上查詢並顯示資料庫mysql的數據表

在頁面中寫Java片段 比如:
<%
//驅動程序名
String driverName = "com.mysql.jdbc.Driver";
//資料庫用戶名
String userName = "自己的";
//密碼
String userPasswd = "自己的";
//資料庫名
String dbName = "自己的";
//表名
String tableName = "自己的";
//聯結字元串
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);
%>

G. 如何在JSP頁面顯示mysql資料庫內容

顯示資料庫數據的jsp代碼如下:

解釋及說明在代碼的注釋中即可查看


<spanstyle="font-size:12px;"><spanstyle="font-size:14px;"><%@pagelanguage="java"import="java.sql.*,java.io.*,java.util.*"%>
<%@pagecontentType="text/html;charset=utf-8"%>
<html>
<head>
<styletype="text/css">
table{
border:2px#CCCCCCsolid;
width:360px;
}

td,th{
height:30px;
border:#CCCCCC1pxsolid;
}
</style>
</head>
<body>
<%
//驅動程序名
StringdriverName="com.mysql.jdbc.Driver";
//資料庫用戶名
StringuserName="root";
//密碼
StringuserPasswd="szy";
//資料庫名
StringdbName="studentmanage";
//表名
StringtableName="student";
//連接字元串
Stringurl="jdbc:mysql://資料庫地址:埠號/"+dbName+"?user="
+userName+"&password="+userPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connectionconnection=DriverManager.getConnection(url);
Statementstatement=connection.createStatement();
Stringsql="SELECT*FROM"+tableName;
ResultSetrs=statement.executeQuery(sql);
%>
<br>
<br>
<tablealign="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>
<divalign="center">
<br><br><br>
<%
out.print("數據查詢成功,恭喜你");
%>
</div>
<%
rs.close();
statement.close();
connection.close();
%>
</body>
</html></span><spanstyle="font-size:24px;color:rgb(255,0,0);">
</span></span>

顯示結果如下所示:

H. JSP頁面如何實現初期化的時候就檢索一次資料庫

JSP頁面實現初期化的時候就檢索一次資料庫的方法是在init方法中調用層數據介面。
1.初始化時期 當一個伺服器裝載 servlet 時,它運行 servlet 的 init() 方法。
public void init(ServletConfig config) throws ServletException
{
super.init(); //一些初始化的操作,如資料庫的連接
queryData();//調用數據訪問介面查詢數據並返回jsp
}

需要記住的是一定要在 init()結束時調用 super.init()。init()方法不能反復調用,一旦調用就是重裝載 servlet。直到伺服器調用 destroy 方法卸載 servlet 後才能再調用。
2、Servlet 的執行時期 在伺服器裝載初始化 servlet 後,servlet 就能夠處理客戶端的請求在 Servlet 執行期間其最多的應用是處理客戶端的請求並產生一個網頁。其代碼如下:
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>"數據初始化</title></head>");
out.println("<body>");
out.println("querydata()"+data.getName);
out.println("</body></html>");
out.close();

I. jsp頁面在資料庫查詢

String mysql="select * from task where 1=1";
if(request.getParameter("taskdate")!=null&&!request.getParameter("taskdate").equals(""))
{
mysql=mysql+" and taskdate="+request.getParameter("taskdate");
}
if(request.getParameter("tasktype")!=null&&!request.getParameter("tasktype").equals(""))
{
mysql=mysql+" and tasktype="+request.getParameter("tasktype");
}
if(request.getParameter("taskstate")!=null&&!request.getParameter("taskstate").equals(""))
{
mysql=mysql+" and tasktype="+request.getParameter("taskstate");
}
if(request.getParameter("station")!=null&&!request.getParameter("station").equals(""))
{
mysql=mysql+" and station="+request.getParameter("station");
}
假設你的完成日期、任務類型、任務狀態和分站分別為taskdate、tasktype、taskstate和station.
用jsp的話,應該這些就可以了。mysql就是你要的SQL語句。

熱點內容
安卓網頁怎麼截取 發布:2024-05-18 20:53:56 瀏覽:970
在配置更新的時候沒電關機怎麼辦 發布:2024-05-18 20:36:10 瀏覽:927
win7訪問win2000 發布:2024-05-18 20:27:41 瀏覽:388
青島人社局密碼多少 發布:2024-05-18 20:19:10 瀏覽:734
無法存儲呼叫轉移 發布:2024-05-18 20:18:30 瀏覽:126
資料庫的調優 發布:2024-05-18 20:18:29 瀏覽:346
sqlserver注冊表清理 發布:2024-05-18 20:13:14 瀏覽:991
linux刪除連接 發布:2024-05-18 20:06:56 瀏覽:822
linux搭建雲伺服器平台 發布:2024-05-18 19:52:21 瀏覽:402
安卓怎麼關閉美易訂閱 發布:2024-05-18 19:29:16 瀏覽:644