Java DAO 示例:
import java.sql.* ;
public class DataBaseConnection
{
private String DBDRIVER = "com.mysql.jdbc.Driver" ;
private String DBURL = "jdbc:mysql://localhost:3306/test" ;
private String DBUSER = "root" ;
private String DBPASSWORD = "123456" ;
private Connection conn = null ;
public DataBaseConnection()
{
try
{
Class.forName(DBDRIVER) ;
this.conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
}
catch (Exception e)
{
}
}
public Connection getConnection()
{
return this.conn ;
}
public void close()
{
try
{
this.conn.close() ;
}
catch (Exception e)
{
}
}
};
demo.dao: UserDAO, DepartDAO, EmployeeDAO
demo.dao.impl: UserDAOImpl, DepartDAOImpl,EmployeeDAOImpl
demo.db: DatabaseConnection
DAOFactory
demo.vo: User,Depart,Employee
User(id, username, password)
depart(departID,departName,departDes)
Employee(employeeID,name,age,departID)
/*
功能:
· 判断是否是正确的用户名或密码
· 从数据库中取出用户的真实姓名
*/
public boolean login(Person person) throws Exception
{
boolean flag = false ;
String sql = "SELECT name FROM person WHERE id=? and password=?" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
try
{
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,person.getId()) ;
pstmt.setString(2,person.getPassword()) ;
ResultSet rs = pstmt.executeQuery() ;
if(rs.next())
{
flag = true ;
person.setName(rs.getString(1)) ;
}
rs.close() ;
pstmt.close() ;
}
catch (Exception e)
{
throw new Exception("操作出现错误!!!") ;
}
finally
{
dbc.close() ;
}
return flag ;
// 增加操作
public void insert(Note note) throws Exception
{
String sql = "INSERT INTO note(id,title,author,content) VALUES(note_sequ.nextVal,?,?,?)" ;
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.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 ;
}
// 查询全部
@SuppressWarnings("unchecked")
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(e) ;
throw new Exception("操作中出现错误!!!") ;
}
finally
{
dbc.close() ;
}
return all ;
}
/**
*
*/
private static final long serialVersionUID = 1458725077853621648L;
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
this.doPost(request,response) ;
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
String path = "login.jsp" ;
// 1、接收传递的参数
String id = request.getParameter("id") ;
String password = request.getParameter("password") ;
// 2、将请求的内容赋值给VO类
Person person = new Person() ;
person.setId(id) ;
person.setPassword(password) ;
System.out.println(id);
try
{
// 进行数据库验证
if(DAOFactory.getPersonDAOInstance().login(person))
{
// 如果为真,则表示用户ID和密码合法
// 设置用户姓名到session范围之中
request.getSession().setAttribute("uname",person.getName()) ;
// 修改跳转路径
path = "login_success.jsp" ;
}
else
{
// 登陆失败
// 设置错误信息
request.setAttribute("err","错误的用户ID及密码!!!") ;
}
}
catch(Exception e)
{}
// 进行跳转
request.getRequestDispatcher(path).forward(request,response) ;
}
/**
*
*/
private static final long serialVersionUID = 2264231254140044346L;
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
this.doPost(request,response) ;
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
request.setCharacterEncoding("GB2312") ;
String path = "errors.jsp" ;
// 接收要操作的参数值
String status = request.getParameter("status") ;
if(status!=null)
{
// 参数有内容,之后选择合适的方法
// 查询全部操作
if("selectall".equals(status))
{
try
{
request.setAttribute("all",DAOFactory.getNoteDAOInstance().queryAll()) ;
}
catch (Exception e)
{
}
path = "list_notes.jsp" ;
}
// 插入操作
if("insert".equals(status))
{
// 1、接收插入的信息
String title = request.getParameter("title") ;
String author = request.getParameter("author") ;
String content = request.getParameter("content") ;
// 2、实例化VO对象
Note note = new Note() ;
note.setTitle(title) ;
note.setAuthor(author) ;
note.setContent(content) ;
// 3、调用DAO完成数据库的插入操作
boolean flag = false ;
try
{
DAOFactory.getNoteDAOInstance().insert(note) ;
flag = true ;
}
catch (Exception e)
{}
request.setAttribute("flag",new Boolean(flag)) ;
path = "insert_do.jsp" ;
}
// 按ID查询操作,修改之前需要将数据先查询出来
if("selectid".equals(status))
{
// 接收参数
int id = 0 ;
try
{
id = Integer.parseInt(request.getParameter("id")) ;
}
catch(Exception e)
{}
try
{
request.setAttribute("note",DAOFactory.getNoteDAOInstance().queryById(id)) ;
}
catch (Exception e)
{
}
path = "update.jsp" ;
}
// 更新操作
if("update".equals(status))
{
int id = 0 ;
try
{
id = Integer.parseInt(request.getParameter("id")) ;
}
catch(Exception e)
{}
String title = request.getParameter("title") ;
String author = request.getParameter("author") ;
String content = request.getParameter("content") ;
Note note = new Note() ;
note.setId(id) ;
note.setTitle(title) ;
note.setAuthor(author) ;
note.setContent(content) ;
boolean flag = false ;
try
{
DAOFactory.getNoteDAOInstance().update(note) ;
flag = true ;
}
catch (Exception e)
{}
request.setAttribute("flag",new Boolean(flag)) ;
path = "update_do.jsp" ;
}
// 模糊查询
if("selectbylike".equals(status))
{
String keyword = request.getParameter("keyword") ;
try
{
request.setAttribute("all",DAOFactory.getNoteDAOInstance().queryByLike(keyword)) ;
}
catch (Exception e)
{
}
path = "list_notes.jsp" ;
}
// 删除操作
if("delete".equals(status))
{
// 接收参数
int id = 0 ;
try
{
id = Integer.parseInt(request.getParameter("id")) ;
}
catch(Exception e)
{}
boolean flag = false ;
try
{
DAOFactory.getNoteDAOInstance().delete(id) ;
flag = true ;
}
catch (Exception e)
{}
request.setAttribute("flag",new Boolean(flag)) ;
path = "delete_do.jsp" ;
}
}
else
{
// 则表示无参数,非法的客户请求
}
request.getRequestDispatcher(path).forward(request,response) ;
}