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]