wlq530 2011-09-06 10:05
浏览 324
已采纳

初学者玩struts2 ,jsp页面显示不出来数据库内容,求解

DB_Connect.java
[code="java"]package com.util;

import java.sql.*;

public class DB_Connect {
// 数据库地址
private final String url = "jdbc:oracle:thin:@localhost:1521:orcl" ;
private final String username = "metrology";
private final String password = "password";
//数据库驱动地址
private final static String driver = "oracle.jdbc.OracleDriver";
private static Connection conn = null;
//加载数据库驱动
static {
try {
Class.forName(driver);
System.out.println("驱动加载成功!");
}
catch(Exception e) {
System.out.println(e);
}
}
//创建数据库连接
public boolean createConnection(){
try{
conn = DriverManager.getConnection("url","username","password");
conn.setAutoCommit(true);
}
catch (SQLException e){
System.out.println(e.getMessage());
}
return true;
}
public Connection getConnection(){
return this.conn;
}
public void close() throws SQLException{
this.conn.close();
}
//查询数据库
public ResultSet query(String sql){
ResultSet rs = null;
try {
if (conn == null){
createConnection();
}
Statement stmt = conn.createStatement();
try{
rs = stmt.executeQuery(sql);
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
}
catch(SQLException e){
System.out.println(e.getMessage());
}
return rs;
}
}
[/code]
newsVO.java
[code="java"]package com.vos;

public class newsVO {
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
private String content;
}
[/code]
newsDAO.java
[code="java"]package com.daos;

import java.sql.*;
import java.util.ArrayList;
import com.util.DB_Connect;
import com.vos.newsVO;

public class newsDAO {
private DB_Connect connection;
private newsVO vo;
public ArrayList queryNews(){
ArrayList list = new ArrayList();
String sql = "select * from news";
ResultSet rs = connection.query(sql);
try{
while(rs.next()){
vo.setTitle(rs.getString(1));
vo.setContent(rs.getString("content"));

        }
    }catch(SQLException e){
        e.printStackTrace();
    }
    return list;
}

}
[/code]
newsService.java
[code="java"]package services;

import java.util.ArrayList;

import com.daos.newsDAO;
import com.vos.newsVO;

public class NewsService {
private newsDAO dao;
public ArrayListqueryNews(){
return dao.queryNews();
}

}
[/code]
NewsAction.java
[code="java"]package com.actions;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import services.NewsService;

import com.opensymphony.xwork2.ActionSupport;
import com.vos.newsVO;

public class NewsAction extends ActionSupport {
private NewsService service;
private static final long serialVersionUID = 1L;
private String title;
private String content;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String execute(){
try{
HttpServletRequest request = ServletActionContext.getRequest();
ArrayList list = service.queryNews();
request.setAttribute("news", list);
return SUCCESS;
}catch(Exception e)
{
e.printStackTrace();
}
return SUCCESS;
}
}
[/code]

JSP页面:
[code="java"]

/s:property
/s:push
/s:iterator[/code]

  • 写回答

6条回答 默认 最新

  • ascendlin 2011-09-06 11:24
    关注

    dao和service貌似没注入吧

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 maccms影视模板 制作影视网站失败 求
  • ¥15 stm32按键设置闹钟数进退位不正常
  • ¥15 三电平逆变器中点电位平衡问题
  • ¥20 这怎么写啊 java课设
  • ¥15 用C语言完成一个复杂的游戏
  • ¥15 如何批量更改很多个文件夹里的文件名中包含文件夹名?
  • ¥50 MTK手机模拟HID鼠标出现卡顿
  • ¥20 求下下面这个数据结构代码
  • ¥20 前端 二进制文件流图片转化异常
  • ¥15 github上的这个C语言项目如何跑起来