/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lw.dao;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import lw.idao.ILwContentDao;
import lw.model.LwContent;
/**
*
* @author student1
*/
public class LwContentDao implements ILwContentDao {
@Override
public List<LwContent> findByPage(Integer pageSize, Integer pageNo) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<LwContent> list = new ArrayList<LwContent>();
try {
con = ConnectDB.connect();
String sql = "select * from lw_content limit " + (pageNo - 1) * pageSize + "," + pageSize;
pstmt=con.prepareStatement(sql);
rs = pstmt.executeQuery(sql);
LwContent lwContent = null;
while (rs.next()) {
lwContent = new LwContent();
lwContent.setContId(rs.getInt("cont_id"));
lwContent.setContTitle(rs.getString("cont_title"));
lwContent.setContContent(rs.getString("cont_content"));
lwContent.setContDatetime(rs.getDate("cont_datetime"));
list.add(lwContent);
}
} catch (SQLException ex) {
Logger.getLogger(LwContentDao.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
rs.close();
} catch (SQLException ex) {
Logger.getLogger(LwContentDao.class.getName()).log(Level.SEVERE, null, ex);
}
try {
pstmt.close();
} catch (SQLException ex) {
Logger.getLogger(LwContentDao.class.getName()).log(Level.SEVERE, null, ex);
}
try {
con.close();
} catch (SQLException ex) {
Logger.getLogger(LwContentDao.class.getName()).log(Level.SEVERE, null, ex);
}
}
return list;
}
@Override
public int findCount() {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
int count = 0;
try {
con = ConnectDB.connect();
String sql = "select count(*) as count from lw_content";
pstmt=con.prepareStatement(sql);
rs = pstmt.executeQuery(sql);
if (rs.next()) {
count = rs.getInt("count");
}
} catch (SQLException ex) {
Logger.getLogger(LwContentDao.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
rs.close();
} catch (SQLException ex) {
Logger.getLogger(LwContentDao.class.getName()).log(Level.SEVERE, null, ex);
}
try {
pstmt.close();
} catch (SQLException ex) {
Logger.getLogger(LwContentDao.class.getName()).log(Level.SEVERE, null, ex);
}
try {
con.close();
} catch (SQLException ex) {
Logger.getLogger(LwContentDao.class.getName()).log(Level.SEVERE, null, ex);
}
}
return count;
}
@Override
public void insert(LwContent obj) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectDB.connect();
String sql = "insert into lw_content(cont_title,cont_content) values(?,?)";
pstmt=con.prepareStatement(sql);
pstmt.setString(1, obj.getContTitle());
pstmt.setString(2, obj.getContContent());
pstmt.executeUpdate();
} catch (SQLException ex) {
Logger.getLogger(LwContentDao.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
pstmt.close();
} catch (SQLException ex) {
Logger.getLogger(LwContentDao.class.getName()).log(Level.SEVERE, null, ex);
}
try {
con.close();
} catch (SQLException ex) {
Logger.getLogger(LwContentDao.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
@Override
public void update(LwContent obj) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectDB.connect();
String sql = "update lw_content set cont_title=?,cont_content=? where cont_id=?";
pstmt=con.prepareStatement(sql);
pstmt.setString(1, obj.getContTitle());
pstmt.setString(2, obj.getContContent());
pstmt.setInt(3, obj.getContId());
pstmt.executeUpdate();
} catch (SQLException ex) {
Logger.getLogger(LwContentDao.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
pstmt.close();
} catch (SQLException ex) {
Logger.getLogger(LwContentDao.class.getName()).log(Level.SEVERE, null, ex);
}
try {
con.close();
} catch (SQLException ex) {
Logger.getLogger(LwContentDao.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
@Override
public void delete(Integer id) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectDB.connect();
String sql = "delete from lw_content where cont_id=?";
pstmt=con.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException ex) {
Logger.getLogger(LwContentDao.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
pstmt.close();
} catch (SQLException ex) {
Logger.getLogger(LwContentDao.class.getName()).log(Level.SEVERE, null, ex);
}
try {
con.close();
} catch (SQLException ex) {
Logger.getLogger(LwContentDao.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
@Override
public LwContent findById(Integer id) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
LwContent lwContent = null;
try {
con = ConnectDB.connect();
String sql = "select * from lw_content where cont_id=?";
pstmt=con.prepareStatement(sql);
pstmt.setInt(1, id);
System.out.println(pstmt==null);
rs = pstmt.executeQuery();
if (rs.next()) {
lwContent = new LwContent();
lwContent.setContId(rs.getInt("cont_id"));
lwContent.setContTitle(rs.getString("cont_title"));
lwContent.setContContent(rs.getString("cont_content"));
lwContent.setContDatetime(rs.getDate("cont_datetime"));
}
} catch (SQLException ex) {
ex.printStackTrace();
Logger.getLogger(LwContentDao.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
rs.close();
} catch (SQLException ex) {
Logger.getLogger(LwContentDao.class.getName()).log(Level.SEVERE, null, ex);
}
try {
pstmt.close();
} catch (SQLException ex) {
Logger.getLogger(LwContentDao.class.getName()).log(Level.SEVERE, null, ex);
}
try {
con.close();
} catch (SQLException ex) {
Logger.getLogger(LwContentDao.class.getName()).log(Level.SEVERE, null, ex);
}
}
return lwConten
- 1
- 2
- 3
前往页