當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JSP_通过表格显示数据库的信息
生活随笔
收集整理的這篇文章主要介紹了
JSP_通过表格显示数据库的信息
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在本篇文章中,小編將介紹在jsp頁面中通過表格顯示
數(shù)據(jù)庫
的實(shí)現(xiàn):下面我們以“新聞發(fā)布系統(tǒng)”中顯示一級(jí)標(biāo)題的信息為例進(jìn)行講述,在新聞發(fā)布系統(tǒng)中存在一二級(jí)標(biāo)題,在后臺(tái)可以對(duì)標(biāo)題進(jìn)行管理,可查詢標(biāo)題等信息
【step one】
1-1 建立數(shù)據(jù)庫
在jsp中,我們使用的是mysql數(shù)據(jù)庫,對(duì)于此數(shù)據(jù)的優(yōu)缺點(diǎn)本篇不予以講述,首先建立news數(shù)據(jù)庫,其數(shù)據(jù)庫中表的信息為:
eg:< id :1 ; name:娛樂; creator:ibbon;createTime:2014-11-21 22:17:43>
【step two】 ?建立web項(xiàng)目(news)
2-1:首先,我們?cè)趙eb項(xiàng)目的src文件夾寫入鏈接數(shù)據(jù)庫的相關(guān)的類,他們分別是:鏈接數(shù)據(jù)庫的類,數(shù)據(jù)的實(shí)體類
-----------------------------------------------------------------------------------------------------------------------------------------------------
2-1-1 : 數(shù)據(jù)庫實(shí)體類,在此類中主要含有數(shù)據(jù)庫的字段,<包:cn.edu.bzu.entity ;實(shí)體類:Title.java;>?
復(fù)制代碼
1 package cn.news.jsp.entity;
2?
3 import java.sql.Date;
4?
5 public class Title {
/**
*下面這四個(gè)屬性為數(shù)據(jù)庫表中的四個(gè)屬性
*/
6 ? ? private int id; ??
7 ? ? private String name;
8 ? ? private String creator;
9 ? ? private Date createTime;
10 ? ??
11 ? ? public int getId() {
12 ? ? ? ? return id;
13 ? ? }
14 ? ? public void setId(int id) {
15 ? ? ? ? this.id = id;
16 ? ? }
17 ? ? public String getName() {
18 ? ? ? ? return name;
19 ? ? }
20 ? ? public void setName(String name) {
21 ? ? ? ? this.name = name;
22 ? ? }
23 ? ? public String getCreator() {
24 ? ? ? ? return creator;
25 ? ? }
26 ? ? public void setCreator(String creator) {
27 ? ? ? ? this.creator = creator;
28 ? ? }
29 ? ? public Date getCreateTime() {
30 ? ? ? ? return createTime;
31 ? ? }
32 ? ? public void setCreateTime(Date createTime) {
33 ? ? ? ? this.createTime = createTime;
34 ? ? }
35 ? ? public Title(int id, String name, String creator, Date createTime) {
36 ? ? ? ? super();
37 ? ? ? ? this.id = id;
38 ? ? ? ? this.name = name;
39 ? ? ? ? this.creator = creator;
40 ? ? ? ? this.createTime = createTime;
41 ? ? }
42 ? ??
43 }
復(fù)制代碼
ps:<實(shí)體類的作用>
/**
*實(shí)體類的作用?
*在JAVAWeb中,實(shí)體類里面的某一個(gè)類,相當(dāng)于是數(shù)據(jù)庫里的一張表,一個(gè)類里面的某個(gè)字段相當(dāng)于表的列名
*在實(shí)體里有g(shù)etter和setter方法,getter是只讀,setter是寫入
*/
-----------------------------------------------------------------------------------------------------------------------------------------------------
2-2-2 數(shù)據(jù)庫操作類 :下面以對(duì)數(shù)據(jù)庫的查詢功能書寫代碼,<包:cn.edu.bzu.dao;類:TitleDAO.java>
復(fù)制代碼
1 package cn.edu.bzu.dao;
2 import java.sql.*;
3 import java.util.ArrayList;
4 import java.util.List;
5?
6 import cn.edu.bzu.entity.Title;
7?
8 public class TitleDAO {
9 ? ? public List readFirstTitle(){
10 ? ? ? ? List<Title> list =new ArrayList<Title>();
11 ? ? ? ? Connection con=null;
12 ? ? ? ? PreparedStatement psmt=null;
13 ? ? ? ? ResultSet rs=null;
14 ? ? ? ? try {
15 ? ? ? ? ? ? Class.forName("com.mysql.jdbc.Driver");
16 ? ? ? ? } catch (ClassNotFoundException e) {
17 ? ? ? ? ? ? e.printStackTrace();
18 ? ? ? ? }
19 ? ? ? ??
20 ? ? ? ? try {
21 ? ? ? ? ? ? con=DriverManager.getConnection("jdbc:mysql://localhost:3306/news","root","baby123");
22 ? ? ? ? ? ? String sql="select * from title";
23 ? ? ? ? ? ? psmt=con.prepareStatement(sql);
24 ? ? ? ? ? ? rs=psmt.executeQuery();
25 ? ? ? ? ? ??
26 ? ? ? ? ? ? while(rs.next())
27 ? ? ? ? ? ? {
28 ? ? ? ? ? ? ? ? int id=rs.getInt("id");
29 ? ? ? ? ? ? ? ? String name=rs.getString("name");
30 ? ? ? ? ? ? ? ? String creator=rs.getString("creator");
31 ? ? ? ? ? ? ? ? Date createTime=rs.getDate("createTime");
32 ? ? ? ? ? ? ? ? Title tl=new Title(id, name, creator, createTime);
33 ? ? ? ? ? ? ? ? list.add(tl);
34 ? ? ? ? ? ? }
35 ? ? ? ? ? ??
36 ? ? ? ? } catch (SQLException e) {
37 ? ? ? ? ? ? e.printStackTrace();
38 ? ? ? ? }finally
39 ? ? ? ? {
40 ? ? ? ? ? ? try {
41 ? ? ? ? ? ? ? ? if(rs!=null)
42 ? ? ? ? ? ? ? ? {
43 ? ? ? ? ? ? ? ? ? ? rs.close();
44 ? ? ? ? ? ? ? ? }
45 ? ? ? ? ? ? ? ? if(psmt!=null)
46 ? ? ? ? ? ? ? ? {
47 ? ? ? ? ? ? ? ? ? ? psmt.close();
48 ? ? ? ? ? ? ? ? }
49 ? ? ? ? ? ? ? ? if(con!=null)
50 ? ? ? ? ? ? ? ? {
51 ? ? ? ? ? ? ? ? ? ? con.close();
52 ? ? ? ? ? ? ? ? }
53 ? ? ? ? ? ? } catch (SQLException e) {
54 ? ? ? ? ? ? ? ? e.printStackTrace();
55 ? ? ? ? ? ? }
56 ? ? ? ? }
57 ? ? ? ? return list;
58 ? ? }
59 ? ??
60 }
復(fù)制代碼
ps:現(xiàn)在分析一下上述代碼,
one:創(chuàng)建方法(public List readFirstTitle()),之所以選擇返回值是List是因?yàn)橐粋€(gè)標(biāo)題含有id,name,creator,cteateTime信息,這些信息存在集合中,便于管理,操作
two:創(chuàng)建變量,鏈接數(shù)據(jù)庫,在這個(gè)web project使用的是JDBC技術(shù)鏈接的數(shù)據(jù)庫,這技術(shù)需要一類三接口<DriverManager類,Connection接口,Statement接口,ResultSet接口>
ps:與數(shù)據(jù)庫建立鏈接的步驟:1.注冊(cè)數(shù)據(jù)庫驅(qū)動(dòng);2.獲取數(shù)據(jù)庫連接;3.獲取statement對(duì)象;4.關(guān)閉資源;
具體的實(shí)現(xiàn)步驟,請(qǐng)看上述代碼
three:在數(shù)據(jù)庫中讀取的信息需要使用List接口,以實(shí)現(xiàn)對(duì)數(shù)據(jù)庫的一列信息進(jìn)行封裝起來進(jìn)行來進(jìn)行后續(xù)的使用,就好比:在桌子上有很多的糖,裝糖的瓶子,裝瓶子的箱子-------->>>>>就好比之前的屬性為糖,好幾個(gè)屬性為一個(gè)瓶子,好幾個(gè)瓶子放在箱子中(List)
Four:下面進(jìn)行細(xì)節(jié)講解:
public List readFirstTitle()方法對(duì)每一行的數(shù)據(jù)進(jìn)行封裝, 通過為實(shí)體類建立對(duì)象(entity),調(diào)用他的構(gòu)造方法,通過構(gòu)造方法的方式進(jìn)行復(fù)制,然后把通過賦值完成的數(shù)據(jù)加入list集合中,這樣一行的數(shù)據(jù)就進(jìn)行封裝完成,通過while()循環(huán),從而實(shí)現(xiàn)對(duì)數(shù)據(jù)的遍歷,進(jìn)而實(shí)現(xiàn)對(duì)多行數(shù)據(jù)的存儲(chǔ),但是在此之前需要?jiǎng)?chuàng)建list對(duì)象,并且導(dǎo)入list對(duì)象的相關(guān)類-------------List<Title> list =new ArrayList<Title>();
----------------------------------------------------------------------------------------------------------------------------------------------------------
2-2-3 ? 在web頁面中顯示數(shù)據(jù)庫信息<在webroot下建立titleList.jsp,使用TitleDao.java中的查詢方法得到所有的記錄,然后用表格在頁面顯示>其具體的代碼:
復(fù)制代碼
1 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
2 <%@page import="cn.edu.bzu.dao.TitleDAO,cn.edu.bzu.entity.Title"%>
3 <%
4 String path = request.getContextPath();
5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
6 %>
7?
8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
9 <html>
10 ? <head>
11 ? ? <title>Test-newsTitle</title>
12 ? </head>
13 ??
14 ? <body>
15 ? <table border="1">
16 ? ? ? <tr>
17 ? ? ? ? ? <td>id</td>
18 ? ? ? ? ? <td>name</td>
19 ? ? ? ? ? <td>creator</td>
20 ? ? ? ? ? <td>createTime</td>
21 ? ? ? </tr>
22 ? ? ? ? <%
23 ? ? ? ? ? ?TitleDAO dao=new TitleDAO();
24 ? ? ? ? ? ?List<Title> list =dao.readFirstTitle(); ? ?
25 ? ? ? ? ? ?for(Title tl:list)
26 ? ? ? ? ? ?{%>
27 ? ? ? ? ? <tr>
28 ? ? ? ? ? ? ? <td><%=tl.getId() %></td>
29 ? ? ? ? ? ? ? <td><%=tl.getName() %></td>
30 ? ? ? ? ? ? ? <td><%=tl.getCreator() %>></td>
31 ? ? ? ? ? ? ? <td><%=tl.getCreateTime() %></td>
32 ? ? ? ? ? </tr>
33 ? ? ? ? ? ? <%}
34 ? ? ? ?%>
35 ? </table>
36 ? </body>
37 </html>
總結(jié)
以上是生活随笔為你收集整理的JSP_通过表格显示数据库的信息的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 人民大学云计算编程的网上评估平台--解题
- 下一篇: 在JSP页面中显示List集合·