封装代码之前呢需要连接数据库,来一个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();