站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题jsp显示数据库中数据的问题,顶者有分!

jsp显示数据库中数据的问题,顶者有分!

摘要: jsp显示数据库中数据的问题,顶者有分!


我的网站是jsp+servlet+javabean的,主页上需要显示数据库中的一个表中的数据,已做了一个bean用来将数据封装到Arraylist,现在我在jsp中使用来显示数据,可是这样做,符合mvc吗?
如果不符合mvc,那怎么做最好?


沙发,帮你顶,学习


up


大家有没有碰到过这种情况呢?
因为我做的是一个购物车,有一个存放商品的表,现在需要一个页面来显示商品列表,就这么简单,我觉得奇怪了,好像谁都没遇到过这种需求?


晕,这么大的便宜不检....
我顶~~~

嘻嘻,你使用struts了么?如果没有,那你这个连c(controller)都没有,所以称不上了。。。。

如何做简单,使用当今struts,pring,hibernate之类的框架了......
这方面参考一下"天乙社区”吧,国人开源的东东....
给你顶


顶一下,
顺便提一下,个人不同意楼上的说法。
struts只是一个优秀MVC框架,不用struts不等于没用MVC。
另:建议楼主还是先理解什么是MVC,然后在看自己有没有用MVC。
我想楼主应该是初学者吧,如果可能的话,先将程序功能实现了,再考虑模式的问题。


顶一个,不断学习,我理解MVC就是在不断的看源码,写代码中明白了,其实大部分问题都是要悟的,


晕。俺.....
请使用类似于struts的framework吧......

刚开始没有必要看源代码,熟练运用之后,再看源代码,体会更深...

呵呵~~~




UP


在jsp中使用来显示数据
可以啊
要不view中怎么显示数据


up


UP


你的SERVLET实现了哪些功能?


你用SERVLET了没有,没有用到SERVLET就不能算MVC了
一些数据库的功能你可以放到SERVLET里调用,JSP页面里可以用FUNCTION来提交给SERVLET


因为我做的是一个购物车,有一个存放商品的表,现在需要一个页面来显示商品列表,就这么简单,我觉得奇怪了,好像谁都没遇到过这种需求?
--------------------------------------------------------
你说的这种情况可以用jsp:useBean 当然也可以直接用Bean bean=new Bean(); 只是显示的话就不用进SERVLET了


up


MVC三层架构就是理论,自己怎么用着舒服就怎么用,如果LZ是初学,建议只用struts,什么spring和hibernate之类的,先别考虑了,struts最能体现MVC模式了,提交到action上,调用javabean
还有就是我个人不太建议用ArrayList,用HashMap吧



顶啊,有分啊,顶完再顶


路过


用JSTL


路过


『顶顶』



同学,偶也不懂


可以用userbean 建议先掌握mvc 在学习struts 这样上手很快


up


只要知道MVC是什么意思,你就会明白了。


不懂,幫頂~~~學習!!


学习中。。。


看到MV ,没看到C
这样做,可以的。


帮顶



能实现目的就是好的
MVC不一定必须采用
实用是目的~


如果你要体现MVC那就要把商品建一个form类来封装商品啊


其实MVC,每个系统里几乎都可以提取出一个MVC框架,只要你那是有展示,有逻辑,有模型的
VIEW,BEAN是MODEL,CONTROL,主要是那种思想,规范编程,才会使系统易编写,易维护.


先要能跑起来得程序才是好程序,等你跑起来之后再慢慢改造,可以用struts试试。






up



其实MVC,每个系统里几乎都可以提取出一个MVC框架,只要你那是有展示,有逻辑,有模型的
VIEW,BEAN是MODEL,CONTROL,主要是那种思想,规范编程,才会使系统易编写,易维护.


表示贊同


学习中我也在学习struts


我也在学Strust,嘎嘎~!


数据封装Bean

package article.entity;

import java.util.Date;

public class ArticleBean {
private long articleId;

private String articleTitle;

private String articleContent;

private long catalogId;

private long userId;

private Date createTime;

public ArticleBean() {

}

public String getArticleContent() {
return articleContent;
}

public void setArticleContent(String articleContent) {
this.articleContent = articleContent;
}

public long getArticleId() {
return articleId;
}

public void setArticleId(long articleId) {
this.articleId = articleId;
}

public String getArticleTitle() {
return articleTitle;
}

public void setArticleTitle(String articleTitle) {
this.articleTitle = articleTitle;
}

public long getCatalogId() {
return catalogId;
}

public void setCatalogId(long catalogId) {
this.catalogId = catalogId;
}

public Date getCreateTime() {
return createTime;
}

public void setCreateTime(Date createTime) {
this.createTime = createTime;
}

public long getUserId() {
return userId;
}

public void setUserId(long userId) {
this.userId = userId;
}

}



操作封装Bean
package common.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionBean {
//数据库驱动类名
private static final String DRIVER_CLASS =
"sun.jdbc.odbc.JdbcOdbcDriver";
//数据库源名
private static final String URL = "jdbc:odbc:JDBCSQLDemo_JSPTest";
//数据库登录用户名
private static final String USER = "sa";
//用户名密码
private static final String PASSWORD = "";

public ConnectionBean() {

}
//加载驱动类,获得数据库链接
public static Connection getConnection() {
Connection con = null;
try {
Class.forName(DRIVER_CLASS);
con = DriverManager.getConnection(URL, USER, PASSWORD);
}catch(ClassNotFoundException cnfe){
System.out.println("加载数据库驱动失败");
}
catch(SQLException e) {
System.out.println("创建数据库连接失败!");
}
return con;
}
//关闭数据库链接
public static void closeConnection(Connection con) {
try {
if(con != null && !con.isClosed()) {
con.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
//关闭结果集
public static void closeResultSet(ResultSet res) {
try {
if (res != null) {
res.close();
res = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//关闭预编译statement
public static void closeStatement(PreparedStatement pStatement) {
try {
if (pStatement != null) {
pStatement.close();
pStatement = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//重载closeStatement,关闭statement
public static void closeStatement(Statement st) {
try {
if (st != null) {
st.close();
st = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}



操作封装Bean2

package article.dao;

import article.entity.ArticleBean;
import common.dao.ConnectionBean;
import common.entity.PageBean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.Date;

import java.text.SimpleDateFormat;

public class ArticleDBOperation {
public ArticleBean getOneArticle(long lngArticleId) {
Connection con = ConnectionBean.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;

String strsql = "select * from article_info where article_id = ?";
ArticleBean articleBean = new ArticleBean();

try {
pstmt = con.prepareStatement(strsql);
pstmt.setLong(1, lngArticleId);
rs = pstmt.executeQuery();
if (rs.next()) {
articleBean.setArticleId(rs.getLong("article_id"));
articleBean.setArticleTitle(rs.getString("article_title")
.trim());
articleBean.setArticleContent(rs.getString("article_content")
.trim());
articleBean.setCatalogId(rs.getLong("catalog_id"));
articleBean.setUserId(rs.getLong("user_id"));
articleBean.setCreateTime(rs.getDate("create_time"));
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
ConnectionBean.closeResultSet(rs);
ConnectionBean.closeStatement(pstmt);
ConnectionBean.closeConnection(con);
}

return articleBean;
}

public ArrayList getPageArticle(PageBean pageBean) {
ArrayList articleList = new ArrayList();
Connection con = null;
PreparedStatement pStatement = null;
ResultSet rs = null;

int rows = pageBean.getRowsInPage();
long currentPage = pageBean.getCurrentPage();

try {
con = ConnectionBean.getConnection();
String strSql;
strSql = "select top "
+ rows
+ " * from article_info where article_id not in(select top "
+ (rows * (currentPage - 1))
+ " article_id from article_info order by article_id) order by article_id";
pStatement = con.prepareStatement(strSql);
rs = pStatement.executeQuery();
while (rs.next()) {
ArticleBean articleBean = new ArticleBean();
articleBean.setArticleId(rs.getLong("article_id"));
articleBean.setArticleTitle(rs.getString("article_title"));
articleBean.setArticleContent(rs.getString("article_content"));
articleBean.setCatalogId(rs.getLong("catalog_id"));
articleBean.setUserId(rs.getLong("user_id"));
articleBean.setCreateTime(rs.getDate("create_time"));
articleList.add(articleBean);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
ConnectionBean.closeResultSet(rs);
ConnectionBean.closeStatement(pStatement);
ConnectionBean.closeConnection(con);
}
return articleList;
}

public ArrayList getAllArticle() {
ArrayList articleList = new ArrayList();

Connection con = ConnectionBean.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;

String strsql = "select * from article_info";

try {
pstmt = con.prepareStatement(strsql);
rs = pstmt.executeQuery();
while (rs.next()) {
ArticleBean articleBean = new ArticleBean();
articleBean.setArticleId(rs.getLong("article_id"));
articleBean.setArticleTitle(rs.getString("article_title")
.trim());
articleBean.setArticleContent(rs.getString("article_content")
.trim());
articleBean.setCatalogId(rs.getLong("catalog_id"));
articleBean.setUserId(rs.getLong("user_id"));
articleBean.setCreateTime(rs.getDate("create_time"));
articleList.add(articleBean);
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
ConnectionBean.closeResultSet(rs);
ConnectionBean.closeStatement(pstmt);
ConnectionBean.closeConnection(con);
}

// Iterator it = catalogList.iterator();
// while(it.hasNext()){
// CatalogBean catalogBean1 = (CatalogBean) it.next();
// System.out.println(catalogBean1.getCatalogTitle());
// }

return articleList;
}

public int insertOneArticle(ArticleBean articleBean) {
int result = 0;

Connection con = ConnectionBean.getConnection();
PreparedStatement pstmt = null;

String strsql = "insert into article_info values (?,?,?,?,?)";

SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss");

String strCreateTime = simpleDateFormat.format(new Date());

try {
pstmt = con.prepareStatement(strsql);
pstmt.setString(1, articleBean.getArticleTitle());
pstmt.setString(2, articleBean.getArticleContent());
pstmt.setLong(3, articleBean.getCatalogId());
pstmt.setLong(4, articleBean.getUserId());
pstmt.setString(5, strCreateTime);
result = pstmt.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
ConnectionBean.closeStatement(pstmt);
ConnectionBean.closeConnection(con);
}
return result;
}

public int updateOneArticle(ArticleBean articleBean) {
int result = 0;

Connection con = ConnectionBean.getConnection();
PreparedStatement pstmt = null;

String strsql = "update article_info set article_title = ? , article_content = ? , catalog_id = ? , user_id = ? , create_time = ? where article_id = ?";

SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss");

String strCreateTime = simpleDateFormat.format(new Date());

try {
pstmt = con.prepareStatement(strsql);
pstmt.setString(1, articleBean.getArticleTitle());
pstmt.setString(2, articleBean.getArticleContent());
pstmt.setLong(3, articleBean.getCatalogId());
pstmt.setLong(4, articleBean.getUserId());
pstmt.setString(5, strCreateTime);
pstmt.setLong(6, articleBean.getArticleId());
result = pstmt.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
ConnectionBean.closeStatement(pstmt);
ConnectionBean.closeConnection(con);
}
return result;
}
}



struts 还是 jsf 看准了再学啊


业务处理Servlet

package common.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import article.dao.ArticleDBOperation;

public class GetAllArticleServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doPost(request,response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();

ArrayList articleList = new ArticleDBOperation().getAllArticle();
String strJsp = new String(request.getParameter("strJsp").getBytes("ISO8859_1")).trim();

//request.setAttribute("articleList",articleList);
//RequestDispatcher rd = request.getRequestDispatcher("../../"+strJsp);
//rd.forward(request,response);

request.getSession().setAttribute("articleList",articleList);
response.sendRedirect("../../"+strJsp);

out.flush();
out.close();
}
}



显示页面Jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="javax.servlet.jsp.PageContext"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>

<%String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<head>


My JSP 'articlelist.jsp' starting page







<link> rel="stylesheet" type="text/css" href="news_css.css">


↑返回目录
前一篇: 用JB2006 怎么把 源代码添加到 工程里面?没有jpx文件
后一篇: 请问:JFREECHART里的柱状图怎么生成相应的MAP标签?