JDBC 增删改查

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;
	}
}



2.BaseDao 类:

 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();
}



4 NewsDaoOracleImpl 类:

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;
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值