package com.lx.model;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import cn.mldn.lxh.dao.noteBean_dao;
public class notesBean_conf implements noteBean_dao {
private Connection con;
private PreparedStatement pstmt;
private ResultSet rs;
private boolean b;
private int pageCount=0;
private int pageSize=5;
private int pageNow=0;
private int rowCount=0;
//获得一共分多少页
public int getPageCount() { //计算页数
try {
String sql="select count(*) from notes";
con = new DBCon().getCon();
pstmt=con.prepareStatement(sql);
rs = pstmt.executeQuery();
if (rs.next()) {
rowCount = rs.getInt(1);
}
if (rowCount % pageSize == 0) {
pageCount = rowCount / pageSize;
} else {
pageCount = rowCount / pageSize + 1;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
this.close();
}
return pageCount;
}
// 显示每页的内容
public List queryAll(int pageNow) throws Exception {
List all = new ArrayList();
notes_Bean nb = null;
this.pageNow=pageNow;
try {
String sql = "select top " + pageSize
+ " * from notes where id not in (select top "
+ pageSize * (pageNow - 1) + " id from notes) ";
con = new DBCon().getCon();
pstmt = con.prepareStatement(sql);
rs=pstmt.executeQuery();
while (rs.next()) {
nb = new notes_Bean();
nb.setId(rs.getInt(1));
nb.setTitle(rs.getString(2));
nb.setAutor(rs.getString(3));
nb.setDatetime(rs.getString(4));
nb.setContent(rs.getString(5));
all.add(nb);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
this.close();
}
return all;
}
public boolean insert(notes_Bean nb) {
int i = 0;
b = false;
try {
String title = new String(nb.getTitle().getBytes("ISO-8859-1"),
("GB2312"));
String autor = new String(nb.getAutor().getBytes("ISO-8859-1"),
("GB2312"));
String datetime = nb.getDatetime();
String content = new String(nb.getContent().getBytes("ISO-8859-1"),
("GB2312"));
String sql = "insert into notes(title,autor,datetime,content) values(?,?,?,?)";
con = new DBCon().getCon();
pstmt = con.prepareStatement(sql);
pstmt.setString(1, title);
pstmt.setString(2, autor);
pstmt.setString(3, datetime);
pstmt.setString(4, content);
i = pstmt.executeUpdate();
if (i > 0) {
b = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
this.close();
}
return b;
}
public boolean update(notes_Bean nb, int id) {
int i = 0;
b = false;
try {
String title = new String(nb.getTitle().getBytes("ISO-8859-1"),
"GB2312");
String autor = new String(nb.getAutor().getBytes("ISO-8859-1"),
"GB2312");
String datetime = nb.getDatetime();
String content = new String(nb.getContent().getBytes("ISO-8859-1"),
"GB2312");
String sql = "update notes set title=? ,autor=?,datetime=?,content=? where id=?";
con = new DBCon().getCon();
pstmt = con.prepareStatement(sql);
pstmt.setString(1, title);
pstmt.setString(2, autor);
pstmt.setString(3, datetime);
pstmt.setString(4, content);
pstmt.setInt(5, id);
i = pstmt.executeUpdate();
if (i > 0) {
b = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
this.close();
}
return b;
}
public boolean delete(int id) {
int i = 0;
b = false;
try {
String sql = "delete from notes where id=?";
con = new DBCon().getCon();
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, id);
i = pstmt.executeUpdate();
if (i > 0) {
b = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
this.close();
}
return b;
}
public notes_Bean queryById(int id) throws Exception {
notes_Bean nb = null;
String sql = "SELECT * FROM notes WHERE id=?";
// 下面是针对数据库的具体操作
try {
// 连接数据库
con = new DBCon().getCon();
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, id);
// 进行数据库查询操作
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
// 查询出内容,之后将查询出的内容赋值给person对象
nb = new notes_Bean();
nb.setId(rs.getInt(1));
nb.setTitle(rs.getString(2));
nb.setAutor(rs.getString(3));
nb.setDatetime(rs.getString(4));
nb.setContent(rs.getString(5));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
this.close();
}
return nb;
}
public void close() { // 关闭各种打开的资源
try {
if (rs != null) {
rs.close();
rs = null;
}
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
if (con != null) {
con.close();
con = null;
}
} catch (Exception e) {
e.printStackTrace();// 打印异常,以便修改
}
}
}
评论0