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条)

报告相同问题?

悬赏问题

  • ¥20 树莓派5做人脸情感识别与反馈系统
  • ¥15 selenium 控制 chrome-for-testing 在 Linux 环境下报错 SessionNotCreatedException
  • ¥15 使用pyodbc操作SQL数据库
  • ¥15 MATLAB实现下列
  • ¥30 mininet可视化打不开.mn文件
  • ¥50 C# 全屏打开Edge浏览器
  • ¥80 WEBPACK性能优化
  • ¥30 python拟合回归分析
  • ¥500 metaswitch 6010
  • ¥15 关于#分类#的问题:不用人工智能的算法