Oh,shit ,只有查!
1.bean 类
package entity;
import java.util.*;
/***
* The News class.
* @author REAL
*
*/
public class News {
private String title; // 标题
private String author; // 作者
private Date createTime;// 日期
private String content; // 内容
public News() {
super();
// TODO Auto-generated constructor stub
}
public News(String title, String author, Date createTime, String content) {
super();
this.title = title;
this.author = author;
this.createTime = createTime;
this.content = content;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
package dao;
import java.sql.*;
public class BaseDao {
// Get the connection.
public Connection getConn() {
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "system", "orcl");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
// Close the connection.
public void closeAll(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (stmt != null) {
stmt.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
3 NewsDao 类:
package dao;
import java.util.*;
import entity.News;
/**
* The news interface.
* @author REAL
*
*/
public interface NewsDao {
/*
* Get all news.
*/
public List<News> allNews();
}
package dao;
import java.sql.*;
import java.util.*;
import entity.News;
/***
* News实现类
* @author REAL
*
*/
public class NewsDaoOracleImpl extends BaseDao implements NewsDao {
@Override
public List<News> allNews() {
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
List<News> list=new ArrayList<News>();
String sql="select * from News";
try {
conn=getConn();
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next()){
News ne=new News();
ne.setTitle(rs.getString("title"));
ne.setAuthor(rs.getString("author"));
ne.setCreateTime(rs.getDate("createTime"));
ne.setContent(rs.getString("content"));
list.add(ne);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
closeAll(conn, stmt, rs);
}
return list;
}
}