package cn.mldn.lxh.note.dao.impl ;
import java.sql.* ;
import java.util.* ;
import cn.mldn.lxh.note.vo.* ;
import cn.mldn.lxh.note.dao.* ;
import cn.mldn.lxh.note.dbc.* ;
public class NoteDAOImpl implements NoteDAO
{
// 增加操作
public void insert(Note note) throws Exception
{
String suiji=(int) (Math.random()*10000)+"";
String sql = "INSERT INTO note(id,title,author,content) VALUES(?,?,?,?)" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
try
{
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,suiji);
pstmt.setString(2,note.getTitle()) ;
pstmt.setString(3,note.getAuthor()) ;
pstmt.setString(4,note.getContent()) ;
pstmt.executeUpdate() ;
pstmt.close() ;
}
catch (Exception e)
{
// System.out.println(e) ;
throw new Exception("操作中出现错误!!!") ;
}
finally
{
dbc.close() ;
}
}
// 修改操作
public void update(Note note) throws Exception
{
String sql = "UPDATE note SET title=?,author=?,content=? WHERE id=?" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
try
{
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,note.getTitle()) ;
pstmt.setString(2,note.getAuthor()) ;
pstmt.setString(3,note.getContent()) ;
pstmt.setInt(4,note.getId()) ;
pstmt.executeUpdate() ;
pstmt.close() ;
}
catch (Exception e)
{
throw new Exception("操作中出现错误!!!") ;
}
finally
{
dbc.close() ;
}
}
// 删除操作
public void delete(int id) throws Exception
{
String sql = "DELETE FROM note WHERE id=?" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
try
{
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setInt(1,id) ;
pstmt.executeUpdate() ;
pstmt.close() ;
}
catch (Exception e)
{
throw new Exception("操作中出现错误!!!") ;
}
finally
{
dbc.close() ;
}
}
// 按ID查询,主要为更新使用
public Note queryById(int id) throws Exception
{
Note note = null ;
String sql = "SELECT id,title,author,content FROM note WHERE id=?" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
try
{
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setInt(1,id) ;
ResultSet rs = pstmt.executeQuery() ;
if(rs.next())
{
note = new Note() ;
note.setId(rs.getInt(1)) ;
note.setTitle(rs.getString(2)) ;
note.setAuthor(rs.getString(3)) ;
note.setContent(rs.getString(4)) ;
}
rs.close() ;
pstmt.close() ;
}
catch (Exception e)
{
throw new Exception("操作中出现错误!!!") ;
}
finally
{
dbc.close() ;
}
return note ;
}
// 查询全部
public List queryAll() throws Exception
{
List all = new ArrayList() ;
String sql = "SELECT id,title,author,content FROM note" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
try
{
pstmt = dbc.getConnection().prepareStatement(sql) ;
ResultSet rs = pstmt.executeQuery() ;
while(rs.next())
{
Note note = new Note() ;
note.setId(rs.getInt(1)) ;
note.setTitle(rs.getString(2)) ;
note.setAuthor(rs.getString(3)) ;
note.setContent(rs.getString(4)) ;
all.add(note) ;
}
rs.close() ;
pstmt.close() ;
}
catch (Exception e)
{
System.out.println("查询全部出错了");
System.out.println(e) ;
throw new Exception("操作中出现错误!!!") ;
}
finally
{
dbc.close() ;
}
return all ;
}
// 模糊查询
public List queryByLike(String cond) throws Exception
{
List all = new ArrayList() ;
String sql = "SELECT id,title,author,content FROM note WHERE title LIKE ? or AUTHOR LIKE ? or CONTENT LIKE ?" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
try
{
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,"%"+cond+"%") ;
pstmt.setString(2,"%"+cond+"%") ;
pstmt.setString(3,"%"+cond+"%") ;
ResultSet rs = pstmt.executeQuery() ;
while(rs.next())
{
Note note = new Note() ;
note.setId(rs.getInt(1)) ;
note.setTitle(rs.getString(2)) ;
note.setAuthor(rs.getString(3)) ;
note.setContent(rs.getString(4)) ;
all.add(note) ;
}
rs.close() ;
pstmt.close() ;
}
catch (Exception e)
{
System.out.println(e) ;
throw new Exception("操作中出现错误!!!") ;
}
finally
{
dbc.close() ;
}
return all ;
}
};
没有合适的资源?快使用搜索试试~ 我知道了~
留言本源码(jsp)
共28个文件
jsp:9个
class:8个
java:8个
5星 · 超过95%的资源 需积分: 9 8 下载量 191 浏览量
2008-12-19
15:04:48
上传
评论
收藏 22KB RAR 举报
温馨提示
留言本源码(jsp),一个小项目,这是我在吉林大学时老师讲的一个例子,挺简单的,适合初学者。
资源推荐
资源详情
资源评论
收起资源包目录
DAO模式.rar (28个子文件)
留言本DAO模式
login.jsp 862B
login_conf.jsp 763B
list_notes.jsp 3KB
login_success.jsp 767B
update_do.jsp 1KB
WEB-INF
web.xml 1KB
classes
DAOFactory.java 315B
cn
mldn
lxh
note
dao
PersonDAO.class 218B
NoteDAO.class 457B
impl
NoteDAOImpl.class 4KB
PersonDAOImpl.class 2KB
vo
Note.class 843B
Person.class 688B
dbc
DataBaseConnection.class 1KB
factory
DAOFactory.class 525B
Note.java 660B
DataBaseConnection.java 735B
PersonDAOImpl.java.bak 1KB
Person.java 512B
NoteDAOImpl.java 5KB
NoteDAO.java 524B
PersonDAO.java 174B
NoteDAOImpl.java.bak 5KB
PersonDAOImpl.java 1KB
insert.jsp 1KB
update.jsp 2KB
delete_do.jsp 1KB
insert_do.jsp 1KB
共 28 条
- 1
资源评论
- zengliuding2012-03-10真是一个很好的,对我的启发很大,里面的东西写的也很详细,万分感谢分享
pthill
- 粉丝: 58
- 资源: 63
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功