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 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决