网页封装代码

封装代码之前呢需要连接数据库,来一个DBHlelper

public class DBHelper {
	private static String user = "scott";
	private static String upwd = "tiger";
	private static String cname = "oracle.jdbc.driver.OracleDriver";
	private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
	
	//注册驱动类
	static {
		try {
			Class.forName(cname);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	//连接数据库
	/**
	 * 连接数据库
	 * @return
	 */
	public static Connection getCon() {
		Connection con = null;
		try {
			con = DriverManager.getConnection(url, user, upwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return con;
	}
	
	/**
	 * 关闭连接
	 * @param con
	 * @param ps
	 * @param rs
	 */
	public static void closeDb(Connection con,PreparedStatement ps,ResultSet rs) {
		try {
			if(con!=null) {
				con.close();
			}
			if(ps!=null) {
				ps.close();
			}
			if(rs!=null) {
				rs.close();
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 下一个获取下一个编号的方法
	 * @return 下一个编号
	 */
	public static int getNextId(String tableName,String col) {
		int id = 1;
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = DBHelper.getCon();
			ps = con.prepareStatement("select max("+col+") from "+tableName);
			rs = ps.executeQuery();
			if(rs.next()) {
				id = rs.getInt(1)+1;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.closeDb(con, ps, rs);
		}
		return id;
	}
	
	
	//关闭连接

}

然后是实体类,有两个

News实体类


public class News {
	private int nid;
	private int tid;
	private String ntitle;
	private String nzz;
	private String nnr;
	private String nzy;
	private Date ndate;
	private int nlook;
	private String nimage;
	public News() {
		super();
	}
	public News(int tid, String ntitle, String nzz, String nnr, String nzy, Date ndate, int nlook, String nimage) {
		super();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值